Class: DependsOnElement

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/schema.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_dynamic!(d) ⇒ Object



353
354
355
356
357
358
359
360
361
362
# File 'lib/schema.rb', line 353

def self.from_dynamic!(d)
  begin
    value = DependsOnClass.from_dynamic!(d)
    return new(depends_on_class: value, string: nil) if schema[:depends_on_class].right.valid? value
  rescue StandardError
  end
  return new(string: d, depends_on_class: nil) if schema[:string].right.valid? d

  raise "Invalid union"
end

.from_json!(json) ⇒ Object



364
365
366
# File 'lib/schema.rb', line 364

def self.from_json!(json)
  from_dynamic!(JSON.parse(json))
end

Instance Method Details

#to_dynamicObject



368
369
370
371
372
373
374
# File 'lib/schema.rb', line 368

def to_dynamic
  if !depends_on_class.nil?
    depends_on_class.to_dynamic
  elsif !string.nil?
    string
  end
end

#to_json(options = nil) ⇒ Object



376
377
378
# File 'lib/schema.rb', line 376

def to_json(options = nil)
  JSON.generate(to_dynamic, options)
end