Class: Scjson::Types::ScxmlProps

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

Instantiate a new ScxmlProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
# File 'lib/scjson/types.rb', line 1622

def initialize(**kwargs)
  @state = kwargs.fetch(:state, [])
  @parallel = kwargs.fetch(:parallel, [])
  @final = kwargs.fetch(:final, [])
  @datamodel = kwargs.fetch(:datamodel, [])
  @script = kwargs.fetch(:script, [])
  @other_element = kwargs.fetch(:other_element, [])
  @initial = kwargs.fetch(:initial, [])
  @name = kwargs.fetch(:name, nil)
  @version = kwargs.fetch(:version, '1.0')
  @datamodel_attribute = kwargs.fetch(:datamodel_attribute, 'null')
  @binding = kwargs.fetch(:binding, nil)
  @exmode = kwargs.fetch(:exmode, nil)
  @other_attributes = kwargs.fetch(:other_attributes, {})
end

Instance Attribute Details

#bindingObject

Returns the value of attribute binding.



1619
1620
1621
# File 'lib/scjson/types.rb', line 1619

def binding
  @binding
end

#datamodelObject

Returns the value of attribute datamodel.



1619
1620
1621
# File 'lib/scjson/types.rb', line 1619

def datamodel
  @datamodel
end

#datamodel_attributeObject

Returns the value of attribute datamodel_attribute.



1619
1620
1621
# File 'lib/scjson/types.rb', line 1619

def datamodel_attribute
  @datamodel_attribute
end

#exmodeObject

Returns the value of attribute exmode.



1619
1620
1621
# File 'lib/scjson/types.rb', line 1619

def exmode
  @exmode
end

#finalObject

Returns the value of attribute final.



1619
1620
1621
# File 'lib/scjson/types.rb', line 1619

def final
  @final
end

#initialObject

Returns the value of attribute initial.



1619
1620
1621
# File 'lib/scjson/types.rb', line 1619

def initial
  @initial
end

#nameObject

Returns the value of attribute name.



1619
1620
1621
# File 'lib/scjson/types.rb', line 1619

def name
  @name
end

#other_attributesObject

Returns the value of attribute other_attributes.



1619
1620
1621
# File 'lib/scjson/types.rb', line 1619

def other_attributes
  @other_attributes
end

#other_elementObject

Returns the value of attribute other_element.



1619
1620
1621
# File 'lib/scjson/types.rb', line 1619

def other_element
  @other_element
end

#parallelObject

Returns the value of attribute parallel.



1619
1620
1621
# File 'lib/scjson/types.rb', line 1619

def parallel
  @parallel
end

#scriptObject

Returns the value of attribute script.



1619
1620
1621
# File 'lib/scjson/types.rb', line 1619

def script
  @script
end

#stateObject

Returns the value of attribute state.



1619
1620
1621
# File 'lib/scjson/types.rb', line 1619

def state
  @state
end

#versionObject

Returns the value of attribute version.



1619
1620
1621
# File 'lib/scjson/types.rb', line 1619

def version
  @version
end

Class Method Details

.from_hash(data) ⇒ ScxmlProps

Build an instance from a Hash representation.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
# File 'lib/scjson/types.rb', line 1641

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

  normalized = data.transform_keys(&:to_s)
  kwargs = {}
  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[:datamodel] = Array(normalized.fetch('datamodel', [])).map { |item| DatamodelProps.from_hash(item) }
  kwargs[:script] = Array(normalized.fetch('script', [])).map { |item| ScriptProps.from_hash(item) }
  kwargs[:other_element] = Array(normalized.fetch('other_element', []))
  kwargs[:initial] = Array(normalized.fetch('initial', []))
  kwargs[:name] = normalized.fetch('name', nil)
  kwargs[:version] = normalized.fetch('version', nil)
  kwargs[:datamodel_attribute] = normalized.fetch('datamodel_attribute', 'null')
  kwargs[:binding] = normalized.key?('binding') ? BindingDatatypeProps.coerce(normalized['binding'], allow_nil: true) : nil
  kwargs[:exmode] = normalized.key?('exmode') ? ExmodeDatatypeProps.coerce(normalized['exmode'], allow_nil: true) : nil
  kwargs[:other_attributes] = normalized.fetch('other_attributes', {})
  new(**kwargs)
end

.from_json(json) ⇒ ScxmlProps

Deserialize an instance from a JSON payload.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



1665
1666
1667
1668
# File 'lib/scjson/types.rb', line 1665

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)


1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
# File 'lib/scjson/types.rb', line 1672

def to_hash
  {
    '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 },
    'datamodel' => (@datamodel || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'script' => (@script || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'other_element' => @other_element,
    'initial' => @initial,
    'name' => @name,
    'version' => @version,
    'datamodel_attribute' => @datamodel_attribute,
    'binding' => @binding,
    'exmode' => @exmode,
    'other_attributes' => @other_attributes
  }
end

#to_json(*opts) ⇒ String

Serialize the object to JSON.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


1693
1694
1695
# File 'lib/scjson/types.rb', line 1693

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