Class: Scjson::Types::SendProps

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

Instantiate a new SendProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
# File 'lib/scjson/types.rb', line 1703

def initialize(**kwargs)
  @content = kwargs.fetch(:content, [])
  @param = kwargs.fetch(:param, [])
  @other_element = kwargs.fetch(:other_element, [])
  @event = kwargs.fetch(:event, nil)
  @eventexpr = kwargs.fetch(:eventexpr, nil)
  @target = kwargs.fetch(:target, nil)
  @targetexpr = kwargs.fetch(:targetexpr, nil)
  @type_value = kwargs.fetch(:type_value, 'scxml')
  @typeexpr = kwargs.fetch(:typeexpr, nil)
  @id = kwargs.fetch(:id, nil)
  @idlocation = kwargs.fetch(:idlocation, nil)
  @delay = kwargs.fetch(:delay, '0s')
  @delayexpr = kwargs.fetch(:delayexpr, nil)
  @namelist = kwargs.fetch(:namelist, nil)
  @other_attributes = kwargs.fetch(:other_attributes, {})
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



1700
1701
1702
# File 'lib/scjson/types.rb', line 1700

def content
  @content
end

#delayObject

Returns the value of attribute delay.



1700
1701
1702
# File 'lib/scjson/types.rb', line 1700

def delay
  @delay
end

#delayexprObject

Returns the value of attribute delayexpr.



1700
1701
1702
# File 'lib/scjson/types.rb', line 1700

def delayexpr
  @delayexpr
end

#eventObject

Returns the value of attribute event.



1700
1701
1702
# File 'lib/scjson/types.rb', line 1700

def event
  @event
end

#eventexprObject

Returns the value of attribute eventexpr.



1700
1701
1702
# File 'lib/scjson/types.rb', line 1700

def eventexpr
  @eventexpr
end

#idObject

Returns the value of attribute id.



1700
1701
1702
# File 'lib/scjson/types.rb', line 1700

def id
  @id
end

#idlocationObject

Returns the value of attribute idlocation.



1700
1701
1702
# File 'lib/scjson/types.rb', line 1700

def idlocation
  @idlocation
end

#namelistObject

Returns the value of attribute namelist.



1700
1701
1702
# File 'lib/scjson/types.rb', line 1700

def namelist
  @namelist
end

#other_attributesObject

Returns the value of attribute other_attributes.



1700
1701
1702
# File 'lib/scjson/types.rb', line 1700

def other_attributes
  @other_attributes
end

#other_elementObject

Returns the value of attribute other_element.



1700
1701
1702
# File 'lib/scjson/types.rb', line 1700

def other_element
  @other_element
end

#paramObject

Returns the value of attribute param.



1700
1701
1702
# File 'lib/scjson/types.rb', line 1700

def param
  @param
end

#targetObject

Returns the value of attribute target.



1700
1701
1702
# File 'lib/scjson/types.rb', line 1700

def target
  @target
end

#targetexprObject

Returns the value of attribute targetexpr.



1700
1701
1702
# File 'lib/scjson/types.rb', line 1700

def targetexpr
  @targetexpr
end

#type_valueObject

Returns the value of attribute type_value.



1700
1701
1702
# File 'lib/scjson/types.rb', line 1700

def type_value
  @type_value
end

#typeexprObject

Returns the value of attribute typeexpr.



1700
1701
1702
# File 'lib/scjson/types.rb', line 1700

def typeexpr
  @typeexpr
end

Class Method Details

.from_hash(data) ⇒ SendProps

Build an instance from a Hash representation.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
# File 'lib/scjson/types.rb', line 1724

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

  normalized = data.transform_keys(&:to_s)
  kwargs = {}
  kwargs[:content] = Array(normalized.fetch('content', [])).map { |item| ContentProps.from_hash(item) }
  kwargs[:param] = Array(normalized.fetch('param', [])).map { |item| ParamProps.from_hash(item) }
  kwargs[:other_element] = Array(normalized.fetch('other_element', []))
  kwargs[:event] = normalized.fetch('event', nil)
  kwargs[:eventexpr] = normalized.fetch('eventexpr', nil)
  kwargs[:target] = normalized.fetch('target', nil)
  kwargs[:targetexpr] = normalized.fetch('targetexpr', nil)
  kwargs[:type_value] = normalized.fetch('type_value', 'scxml')
  kwargs[:typeexpr] = normalized.fetch('typeexpr', nil)
  kwargs[:id] = normalized.fetch('id', nil)
  kwargs[:idlocation] = normalized.fetch('idlocation', nil)
  kwargs[:delay] = normalized.fetch('delay', '0s')
  kwargs[:delayexpr] = normalized.fetch('delayexpr', nil)
  kwargs[:namelist] = normalized.fetch('namelist', nil)
  kwargs[:other_attributes] = normalized.fetch('other_attributes', {})
  new(**kwargs)
end

.from_json(json) ⇒ SendProps

Deserialize an instance from a JSON payload.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



1750
1751
1752
1753
# File 'lib/scjson/types.rb', line 1750

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)


1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
# File 'lib/scjson/types.rb', line 1757

def to_hash
  {
    'content' => (@content || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'param' => (@param || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'other_element' => @other_element,
    'event' => @event,
    'eventexpr' => @eventexpr,
    'target' => @target,
    'targetexpr' => @targetexpr,
    'type_value' => @type_value,
    'typeexpr' => @typeexpr,
    'id' => @id,
    'idlocation' => @idlocation,
    'delay' => @delay,
    'delayexpr' => @delayexpr,
    'namelist' => @namelist,
    'other_attributes' => @other_attributes
  }
end

#to_json(*opts) ⇒ String

Serialize the object to JSON.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


1780
1781
1782
# File 'lib/scjson/types.rb', line 1780

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