Class: Plugins

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



1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
# File 'lib/schema.rb', line 1523

def self.from_dynamic!(d)
  begin
    value = Types::Hash[d].transform_values { |v| Types::Any[v] }
    return new(anything_map: value, union_array: nil) if schema[:anything_map].right.valid? value
  rescue StandardError
  end
  begin
    value = d.map { |x| Plugin.from_dynamic!(x) }
    return new(union_array: value, anything_map: nil) if schema[:union_array].right.valid? value
  rescue StandardError
  end
  raise "Invalid union"
end

.from_json!(json) ⇒ Object



1537
1538
1539
# File 'lib/schema.rb', line 1537

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

Instance Method Details

#to_dynamicObject



1541
1542
1543
1544
1545
1546
1547
# File 'lib/schema.rb', line 1541

def to_dynamic
  if !anything_map.nil?
    anything_map
  elsif !union_array.nil?
    union_array.map(&:to_dynamic)
  end
end

#to_json(options = nil) ⇒ Object



1549
1550
1551
# File 'lib/schema.rb', line 1549

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