Class: Scjson::Types::FinalProps

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

Overview

Structured type for scjson elements.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ FinalProps

Instantiate a new FinalProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



668
669
670
671
672
673
674
675
# File 'lib/scjson/types.rb', line 668

def initialize(**kwargs)
  @onentry = kwargs.fetch(:onentry, [])
  @onexit = kwargs.fetch(:onexit, [])
  @donedata = kwargs.fetch(:donedata, [])
  @other_element = kwargs.fetch(:other_element, [])
  @id = kwargs.fetch(:id, nil)
  @other_attributes = kwargs.fetch(:other_attributes, {})
end

Instance Attribute Details

#donedataObject

Returns the value of attribute donedata.



665
666
667
# File 'lib/scjson/types.rb', line 665

def donedata
  @donedata
end

#idObject

Returns the value of attribute id.



665
666
667
# File 'lib/scjson/types.rb', line 665

def id
  @id
end

#onentryObject

Returns the value of attribute onentry.



665
666
667
# File 'lib/scjson/types.rb', line 665

def onentry
  @onentry
end

#onexitObject

Returns the value of attribute onexit.



665
666
667
# File 'lib/scjson/types.rb', line 665

def onexit
  @onexit
end

#other_attributesObject

Returns the value of attribute other_attributes.



665
666
667
# File 'lib/scjson/types.rb', line 665

def other_attributes
  @other_attributes
end

#other_elementObject

Returns the value of attribute other_element.



665
666
667
# File 'lib/scjson/types.rb', line 665

def other_element
  @other_element
end

Class Method Details

.from_hash(data) ⇒ FinalProps

Build an instance from a Hash representation.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'lib/scjson/types.rb', line 680

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

  normalized = data.transform_keys(&:to_s)
  kwargs = {}
  kwargs[:onentry] = Array(normalized.fetch('onentry', [])).map { |item| OnentryProps.from_hash(item) }
  kwargs[:onexit] = Array(normalized.fetch('onexit', [])).map { |item| OnexitProps.from_hash(item) }
  kwargs[:donedata] = Array(normalized.fetch('donedata', [])).map { |item| DonedataProps.from_hash(item) }
  kwargs[:other_element] = Array(normalized.fetch('other_element', []))
  kwargs[:id] = normalized.fetch('id', nil)
  kwargs[:other_attributes] = normalized.fetch('other_attributes', {})
  new(**kwargs)
end

.from_json(json) ⇒ FinalProps

Deserialize an instance from a JSON payload.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



697
698
699
700
# File 'lib/scjson/types.rb', line 697

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)


704
705
706
707
708
709
710
711
712
713
# File 'lib/scjson/types.rb', line 704

def to_hash
  {
    'onentry' => (@onentry || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'onexit' => (@onexit || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'donedata' => (@donedata || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'other_element' => @other_element,
    'id' => @id,
    'other_attributes' => @other_attributes
  }
end

#to_json(*opts) ⇒ String

Serialize the object to JSON.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


718
719
720
# File 'lib/scjson/types.rb', line 718

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