Class: With

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



1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
# File 'lib/schema.rb', line 1043

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

.from_json!(json) ⇒ Object



1057
1058
1059
# File 'lib/schema.rb', line 1057

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

Instance Method Details

#to_dynamicObject



1061
1062
1063
1064
1065
1066
1067
# File 'lib/schema.rb', line 1061

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

#to_json(options = nil) ⇒ Object



1069
1070
1071
# File 'lib/schema.rb', line 1069

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