Class: Scjson::Types::TransitionProps

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

Instantiate a new TransitionProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
# File 'lib/scjson/types.rb', line 1880

def initialize(**kwargs)
  @other_element = kwargs.fetch(:other_element, [])
  @raise_value = kwargs.fetch(:raise_value, [])
  @if_value = kwargs.fetch(:if_value, [])
  @foreach = kwargs.fetch(:foreach, [])
  @send = kwargs.fetch(:send, [])
  @script = kwargs.fetch(:script, [])
  @assign = kwargs.fetch(:assign, [])
  @log = kwargs.fetch(:log, [])
  @cancel = kwargs.fetch(:cancel, [])
  @event = kwargs.fetch(:event, nil)
  @cond = kwargs.fetch(:cond, nil)
  @target = kwargs.fetch(:target, [])
  @type_value = kwargs.fetch(:type_value, nil)
  @other_attributes = kwargs.fetch(:other_attributes, {})
end

Instance Attribute Details

#assignObject

Returns the value of attribute assign.



1877
1878
1879
# File 'lib/scjson/types.rb', line 1877

def assign
  @assign
end

#cancelObject

Returns the value of attribute cancel.



1877
1878
1879
# File 'lib/scjson/types.rb', line 1877

def cancel
  @cancel
end

#condObject

Returns the value of attribute cond.



1877
1878
1879
# File 'lib/scjson/types.rb', line 1877

def cond
  @cond
end

#eventObject

Returns the value of attribute event.



1877
1878
1879
# File 'lib/scjson/types.rb', line 1877

def event
  @event
end

#foreachObject

Returns the value of attribute foreach.



1877
1878
1879
# File 'lib/scjson/types.rb', line 1877

def foreach
  @foreach
end

#if_valueObject

Returns the value of attribute if_value.



1877
1878
1879
# File 'lib/scjson/types.rb', line 1877

def if_value
  @if_value
end

#logObject

Returns the value of attribute log.



1877
1878
1879
# File 'lib/scjson/types.rb', line 1877

def log
  @log
end

#other_attributesObject

Returns the value of attribute other_attributes.



1877
1878
1879
# File 'lib/scjson/types.rb', line 1877

def other_attributes
  @other_attributes
end

#other_elementObject

Returns the value of attribute other_element.



1877
1878
1879
# File 'lib/scjson/types.rb', line 1877

def other_element
  @other_element
end

#raise_valueObject

Returns the value of attribute raise_value.



1877
1878
1879
# File 'lib/scjson/types.rb', line 1877

def raise_value
  @raise_value
end

#scriptObject

Returns the value of attribute script.



1877
1878
1879
# File 'lib/scjson/types.rb', line 1877

def script
  @script
end

#sendObject

Returns the value of attribute send.



1877
1878
1879
# File 'lib/scjson/types.rb', line 1877

def send
  @send
end

#targetObject

Returns the value of attribute target.



1877
1878
1879
# File 'lib/scjson/types.rb', line 1877

def target
  @target
end

#type_valueObject

Returns the value of attribute type_value.



1877
1878
1879
# File 'lib/scjson/types.rb', line 1877

def type_value
  @type_value
end

Class Method Details

.from_hash(data) ⇒ TransitionProps

Build an instance from a Hash representation.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
# File 'lib/scjson/types.rb', line 1900

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

  normalized = data.transform_keys(&:to_s)
  kwargs = {}
  kwargs[:other_element] = Array(normalized.fetch('other_element', []))
  kwargs[:raise_value] = Array(normalized.fetch('raise_value', [])).map { |item| RaiseProps.from_hash(item) }
  kwargs[:if_value] = Array(normalized.fetch('if_value', [])).map { |item| IfProps.from_hash(item) }
  kwargs[:foreach] = Array(normalized.fetch('foreach', [])).map { |item| ForeachProps.from_hash(item) }
  kwargs[:send] = Array(normalized.fetch('send', [])).map { |item| SendProps.from_hash(item) }
  kwargs[:script] = Array(normalized.fetch('script', [])).map { |item| ScriptProps.from_hash(item) }
  kwargs[:assign] = Array(normalized.fetch('assign', [])).map { |item| AssignProps.from_hash(item) }
  kwargs[:log] = Array(normalized.fetch('log', [])).map { |item| LogProps.from_hash(item) }
  kwargs[:cancel] = Array(normalized.fetch('cancel', [])).map { |item| CancelProps.from_hash(item) }
  kwargs[:event] = normalized.fetch('event', nil)
  kwargs[:cond] = normalized.fetch('cond', nil)
  kwargs[:target] = Array(normalized.fetch('target', []))
  kwargs[:type_value] = normalized.key?('type_value') ? TransitionTypeDatatypeProps.coerce(normalized['type_value'], allow_nil: true) : nil
  kwargs[:other_attributes] = normalized.fetch('other_attributes', {})
  new(**kwargs)
end

.from_json(json) ⇒ TransitionProps

Deserialize an instance from a JSON payload.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



1925
1926
1927
1928
# File 'lib/scjson/types.rb', line 1925

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)


1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
# File 'lib/scjson/types.rb', line 1932

def to_hash
  {
    'other_element' => @other_element,
    'raise_value' => (@raise_value || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'if_value' => (@if_value || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'foreach' => (@foreach || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'send' => (@send || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'script' => (@script || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'assign' => (@assign || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'log' => (@log || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'cancel' => (@cancel || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'event' => @event,
    'cond' => @cond,
    'target' => @target,
    'type_value' => @type_value,
    'other_attributes' => @other_attributes
  }
end

#to_json(*opts) ⇒ String

Serialize the object to JSON.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


1954
1955
1956
# File 'lib/scjson/types.rb', line 1954

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