Class: MatrixUnion

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



1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
# File 'lib/schema.rb', line 1300

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

.from_json!(json) ⇒ Object



1314
1315
1316
# File 'lib/schema.rb', line 1314

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

Instance Method Details

#to_dynamicObject



1318
1319
1320
1321
1322
1323
1324
# File 'lib/schema.rb', line 1318

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

#to_json(options = nil) ⇒ Object



1326
1327
1328
# File 'lib/schema.rb', line 1326

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