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



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

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



1059
1060
1061
# File 'lib/schema.rb', line 1059

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

Instance Method Details

#to_dynamicObject



1063
1064
1065
1066
1067
1068
1069
# File 'lib/schema.rb', line 1063

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



1071
1072
1073
# File 'lib/schema.rb', line 1071

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