Class: CommandUnion

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



1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
# File 'lib/schema.rb', line 1740

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

  raise "Invalid union"
end

.from_json!(json) ⇒ Object



1752
1753
1754
# File 'lib/schema.rb', line 1752

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

Instance Method Details

#to_dynamicObject



1756
1757
1758
1759
1760
1761
1762
1763
1764
# File 'lib/schema.rb', line 1756

def to_dynamic
  if !command_step.nil?
    command_step.to_dynamic
  elsif !string.nil?
    string
  elsif !string_array.nil?
    string_array
  end
end

#to_json(options = nil) ⇒ Object



1766
1767
1768
# File 'lib/schema.rb', line 1766

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