Class: Scjson::Types::ElseifProps

Inherits:
Object
  • Object
show all
Defined in:
lib/scjson/types.rb

Overview

conditional branch following an ‘<if>`.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ ElseifProps

Instantiate a new ElseifProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



620
621
622
623
# File 'lib/scjson/types.rb', line 620

def initialize(**kwargs)
  @cond = kwargs.fetch(:cond, '')
  @other_attributes = kwargs.fetch(:other_attributes, {})
end

Instance Attribute Details

#condObject

Returns the value of attribute cond.



617
618
619
# File 'lib/scjson/types.rb', line 617

def cond
  @cond
end

#other_attributesObject

Returns the value of attribute other_attributes.



617
618
619
# File 'lib/scjson/types.rb', line 617

def other_attributes
  @other_attributes
end

Class Method Details

.from_hash(data) ⇒ ElseifProps

Build an instance from a Hash representation.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


628
629
630
631
632
633
634
635
636
# File 'lib/scjson/types.rb', line 628

def self.from_hash(data)
  raise ArgumentError, 'Expected Hash' unless data.is_a?(Hash)

  normalized = data.transform_keys(&:to_s)
  kwargs = {}
  kwargs[:cond] = normalized.fetch('cond', '')
  kwargs[:other_attributes] = normalized.fetch('other_attributes', {})
  new(**kwargs)
end

.from_json(json) ⇒ ElseifProps

Deserialize an instance from a JSON payload.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



641
642
643
644
# File 'lib/scjson/types.rb', line 641

def self.from_json(json)
  parsed = JSON.parse(json)
  from_hash(parsed)
end

Instance Method Details

#to_hashHash

Convert the object to a Hash suitable for JSON serialization.

Returns:

  • (Hash)


648
649
650
651
652
653
# File 'lib/scjson/types.rb', line 648

def to_hash
  {
    'cond' => @cond,
    'other_attributes' => @other_attributes
  }
end

#to_json(*opts) ⇒ String

Serialize the object to JSON.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


658
659
660
# File 'lib/scjson/types.rb', line 658

def to_json(*opts)
  JSON.generate(to_hash, *opts)
end