Class: Scjson::Types::StateProps
- Inherits:
-
Object
- Object
- Scjson::Types::StateProps
- Defined in:
- lib/scjson/types.rb
Overview
Structured type for scjson elements.
Instance Attribute Summary collapse
-
#datamodel ⇒ Object
Returns the value of attribute datamodel.
-
#final ⇒ Object
Returns the value of attribute final.
-
#history ⇒ Object
Returns the value of attribute history.
-
#id ⇒ Object
Returns the value of attribute id.
-
#initial ⇒ Object
Returns the value of attribute initial.
-
#initial_attribute ⇒ Object
Returns the value of attribute initial_attribute.
-
#invoke ⇒ Object
Returns the value of attribute invoke.
-
#onentry ⇒ Object
Returns the value of attribute onentry.
-
#onexit ⇒ Object
Returns the value of attribute onexit.
-
#other_attributes ⇒ Object
Returns the value of attribute other_attributes.
-
#other_element ⇒ Object
Returns the value of attribute other_element.
-
#parallel ⇒ Object
Returns the value of attribute parallel.
-
#state ⇒ Object
Returns the value of attribute state.
-
#transition ⇒ Object
Returns the value of attribute transition.
Class Method Summary collapse
-
.from_hash(data) ⇒ StateProps
Build an instance from a Hash representation.
-
.from_json(json) ⇒ StateProps
Deserialize an instance from a JSON payload.
Instance Method Summary collapse
-
#initialize(**kwargs) ⇒ StateProps
constructor
Instantiate a new StateProps object.
-
#to_hash ⇒ Hash
Convert the object to a Hash suitable for JSON serialization.
-
#to_json(*opts) ⇒ String
Serialize the object to JSON.
Constructor Details
#initialize(**kwargs) ⇒ StateProps
Instantiate a new StateProps object.
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
#datamodel ⇒ Object
Returns the value of attribute datamodel.
1790 1791 1792 |
# File 'lib/scjson/types.rb', line 1790 def datamodel @datamodel end |
#final ⇒ Object
Returns the value of attribute final.
1790 1791 1792 |
# File 'lib/scjson/types.rb', line 1790 def final @final end |
#history ⇒ Object
Returns the value of attribute history.
1790 1791 1792 |
# File 'lib/scjson/types.rb', line 1790 def history @history end |
#id ⇒ Object
Returns the value of attribute id.
1790 1791 1792 |
# File 'lib/scjson/types.rb', line 1790 def id @id end |
#initial ⇒ Object
Returns the value of attribute initial.
1790 1791 1792 |
# File 'lib/scjson/types.rb', line 1790 def initial @initial end |
#initial_attribute ⇒ Object
Returns the value of attribute initial_attribute.
1790 1791 1792 |
# File 'lib/scjson/types.rb', line 1790 def initial_attribute @initial_attribute end |
#invoke ⇒ Object
Returns the value of attribute invoke.
1790 1791 1792 |
# File 'lib/scjson/types.rb', line 1790 def invoke @invoke end |
#onentry ⇒ Object
Returns the value of attribute onentry.
1790 1791 1792 |
# File 'lib/scjson/types.rb', line 1790 def onentry @onentry end |
#onexit ⇒ Object
Returns the value of attribute onexit.
1790 1791 1792 |
# File 'lib/scjson/types.rb', line 1790 def onexit @onexit end |
#other_attributes ⇒ Object
Returns the value of attribute other_attributes.
1790 1791 1792 |
# File 'lib/scjson/types.rb', line 1790 def other_attributes @other_attributes end |
#other_element ⇒ Object
Returns the value of attribute other_element.
1790 1791 1792 |
# File 'lib/scjson/types.rb', line 1790 def other_element @other_element end |
#parallel ⇒ Object
Returns the value of attribute parallel.
1790 1791 1792 |
# File 'lib/scjson/types.rb', line 1790 def parallel @parallel end |
#state ⇒ Object
Returns the value of attribute state.
1790 1791 1792 |
# File 'lib/scjson/types.rb', line 1790 def state @state end |
#transition ⇒ Object
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.
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.
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_hash ⇒ Hash
Convert the object to a Hash suitable for JSON serialization.
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.
1867 1868 1869 |
# File 'lib/scjson/types.rb', line 1867 def to_json(*opts) JSON.generate(to_hash, *opts) end |