Class: Scjson::Types::ForeachProps

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

Instantiate a new ForeachProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
# File 'lib/scjson/types.rb', line 806

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, [])
  @array = kwargs.fetch(:array, '')
  @item = kwargs.fetch(:item, '')
  @index = kwargs.fetch(:index, nil)
  @other_attributes = kwargs.fetch(:other_attributes, {})
end

Instance Attribute Details

#arrayObject

Returns the value of attribute array.



803
804
805
# File 'lib/scjson/types.rb', line 803

def array
  @array
end

#assignObject

Returns the value of attribute assign.



803
804
805
# File 'lib/scjson/types.rb', line 803

def assign
  @assign
end

#cancelObject

Returns the value of attribute cancel.



803
804
805
# File 'lib/scjson/types.rb', line 803

def cancel
  @cancel
end

#foreachObject

Returns the value of attribute foreach.



803
804
805
# File 'lib/scjson/types.rb', line 803

def foreach
  @foreach
end

#if_valueObject

Returns the value of attribute if_value.



803
804
805
# File 'lib/scjson/types.rb', line 803

def if_value
  @if_value
end

#indexObject

Returns the value of attribute index.



803
804
805
# File 'lib/scjson/types.rb', line 803

def index
  @index
end

#itemObject

Returns the value of attribute item.



803
804
805
# File 'lib/scjson/types.rb', line 803

def item
  @item
end

#logObject

Returns the value of attribute log.



803
804
805
# File 'lib/scjson/types.rb', line 803

def log
  @log
end

#other_attributesObject

Returns the value of attribute other_attributes.



803
804
805
# File 'lib/scjson/types.rb', line 803

def other_attributes
  @other_attributes
end

#other_elementObject

Returns the value of attribute other_element.



803
804
805
# File 'lib/scjson/types.rb', line 803

def other_element
  @other_element
end

#raise_valueObject

Returns the value of attribute raise_value.



803
804
805
# File 'lib/scjson/types.rb', line 803

def raise_value
  @raise_value
end

#scriptObject

Returns the value of attribute script.



803
804
805
# File 'lib/scjson/types.rb', line 803

def script
  @script
end

#sendObject

Returns the value of attribute send.



803
804
805
# File 'lib/scjson/types.rb', line 803

def send
  @send
end

Class Method Details

.from_hash(data) ⇒ ForeachProps

Build an instance from a Hash representation.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
# File 'lib/scjson/types.rb', line 825

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[:array] = normalized.fetch('array', '')
  kwargs[:item] = normalized.fetch('item', '')
  kwargs[:index] = normalized.fetch('index', nil)
  kwargs[:other_attributes] = normalized.fetch('other_attributes', {})
  new(**kwargs)
end

.from_json(json) ⇒ ForeachProps

Deserialize an instance from a JSON payload.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



849
850
851
852
# File 'lib/scjson/types.rb', line 849

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)


856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
# File 'lib/scjson/types.rb', line 856

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 },
    'array' => @array,
    'item' => @item,
    'index' => @index,
    'other_attributes' => @other_attributes
  }
end

#to_json(*opts) ⇒ String

Serialize the object to JSON.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


877
878
879
# File 'lib/scjson/types.rb', line 877

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