Class: Scjson::Types::StateProps

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) ⇒ StateProps

Instantiate a new StateProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
# File 'lib/scjson/types.rb', line 1793

def initialize(**kwargs)
  @onentry = kwargs.fetch(:onentry, [])
  @onexit = kwargs.fetch(:onexit, [])
  @transition = kwargs.fetch(:transition, [])
  @initial = kwargs.fetch(:initial, [])
  @state = kwargs.fetch(:state, [])
  @parallel = kwargs.fetch(:parallel, [])
  @final = kwargs.fetch(:final, [])
  @history = kwargs.fetch(:history, [])
  @datamodel = kwargs.fetch(:datamodel, [])
  @invoke = kwargs.fetch(:invoke, [])
  @other_element = kwargs.fetch(:other_element, [])
  @id = kwargs.fetch(:id, nil)
  @initial_attribute = kwargs.fetch(:initial_attribute, [])
  @other_attributes = kwargs.fetch(:other_attributes, {})
end

Instance Attribute Details

#datamodelObject

Returns the value of attribute datamodel.



1790
1791
1792
# File 'lib/scjson/types.rb', line 1790

def datamodel
  @datamodel
end

#finalObject

Returns the value of attribute final.



1790
1791
1792
# File 'lib/scjson/types.rb', line 1790

def final
  @final
end

#historyObject

Returns the value of attribute history.



1790
1791
1792
# File 'lib/scjson/types.rb', line 1790

def history
  @history
end

#idObject

Returns the value of attribute id.



1790
1791
1792
# File 'lib/scjson/types.rb', line 1790

def id
  @id
end

#initialObject

Returns the value of attribute initial.



1790
1791
1792
# File 'lib/scjson/types.rb', line 1790

def initial
  @initial
end

#initial_attributeObject

Returns the value of attribute initial_attribute.



1790
1791
1792
# File 'lib/scjson/types.rb', line 1790

def initial_attribute
  @initial_attribute
end

#invokeObject

Returns the value of attribute invoke.



1790
1791
1792
# File 'lib/scjson/types.rb', line 1790

def invoke
  @invoke
end

#onentryObject

Returns the value of attribute onentry.



1790
1791
1792
# File 'lib/scjson/types.rb', line 1790

def onentry
  @onentry
end

#onexitObject

Returns the value of attribute onexit.



1790
1791
1792
# File 'lib/scjson/types.rb', line 1790

def onexit
  @onexit
end

#other_attributesObject

Returns the value of attribute other_attributes.



1790
1791
1792
# File 'lib/scjson/types.rb', line 1790

def other_attributes
  @other_attributes
end

#other_elementObject

Returns the value of attribute other_element.



1790
1791
1792
# File 'lib/scjson/types.rb', line 1790

def other_element
  @other_element
end

#parallelObject

Returns the value of attribute parallel.



1790
1791
1792
# File 'lib/scjson/types.rb', line 1790

def parallel
  @parallel
end

#stateObject

Returns the value of attribute state.



1790
1791
1792
# File 'lib/scjson/types.rb', line 1790

def state
  @state
end

#transitionObject

Returns the value of attribute transition.



1790
1791
1792
# File 'lib/scjson/types.rb', line 1790

def transition
  @transition
end

Class Method Details

.from_hash(data) ⇒ StateProps

Build an instance from a Hash representation.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
# File 'lib/scjson/types.rb', line 1813

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[:transition] = Array(normalized.fetch('transition', [])).map { |item| TransitionProps.from_hash(item) }
  kwargs[:initial] = Array(normalized.fetch('initial', [])).map { |item| InitialProps.from_hash(item) }
  kwargs[:state] = Array(normalized.fetch('state', [])).map { |item| StateProps.from_hash(item) }
  kwargs[:parallel] = Array(normalized.fetch('parallel', [])).map { |item| ParallelProps.from_hash(item) }
  kwargs[:final] = Array(normalized.fetch('final', [])).map { |item| FinalProps.from_hash(item) }
  kwargs[:history] = Array(normalized.fetch('history', [])).map { |item| HistoryProps.from_hash(item) }
  kwargs[:datamodel] = Array(normalized.fetch('datamodel', [])).map { |item| DatamodelProps.from_hash(item) }
  kwargs[:invoke] = Array(normalized.fetch('invoke', [])).map { |item| InvokeProps.from_hash(item) }
  kwargs[:other_element] = Array(normalized.fetch('other_element', []))
  kwargs[:id] = normalized.fetch('id', nil)
  kwargs[:initial_attribute] = Array(normalized.fetch('initial_attribute', []))
  kwargs[:other_attributes] = normalized.fetch('other_attributes', {})
  new(**kwargs)
end

.from_json(json) ⇒ StateProps

Deserialize an instance from a JSON payload.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



1838
1839
1840
1841
# File 'lib/scjson/types.rb', line 1838

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)


1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
# File 'lib/scjson/types.rb', line 1845

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 },
    'transition' => (@transition || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'initial' => (@initial || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'state' => (@state || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'parallel' => (@parallel || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'final' => (@final || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'history' => (@history || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'datamodel' => (@datamodel || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'invoke' => (@invoke || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'other_element' => @other_element,
    'id' => @id,
    'initial_attribute' => @initial_attribute,
    'other_attributes' => @other_attributes
  }
end

#to_json(*opts) ⇒ String

Serialize the object to JSON.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


1867
1868
1869
# File 'lib/scjson/types.rb', line 1867

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