Class: Scjson::Types::IfProps

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

Instantiate a new IfProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
# File 'lib/scjson/types.rb', line 950

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

Instance Attribute Details

#assignObject

Returns the value of attribute assign.



947
948
949
# File 'lib/scjson/types.rb', line 947

def assign
  @assign
end

#cancelObject

Returns the value of attribute cancel.



947
948
949
# File 'lib/scjson/types.rb', line 947

def cancel
  @cancel
end

#condObject

Returns the value of attribute cond.



947
948
949
# File 'lib/scjson/types.rb', line 947

def cond
  @cond
end

#else_valueObject

Returns the value of attribute else_value.



947
948
949
# File 'lib/scjson/types.rb', line 947

def else_value
  @else_value
end

#elseifObject

Returns the value of attribute elseif.



947
948
949
# File 'lib/scjson/types.rb', line 947

def elseif
  @elseif
end

#foreachObject

Returns the value of attribute foreach.



947
948
949
# File 'lib/scjson/types.rb', line 947

def foreach
  @foreach
end

#if_valueObject

Returns the value of attribute if_value.



947
948
949
# File 'lib/scjson/types.rb', line 947

def if_value
  @if_value
end

#logObject

Returns the value of attribute log.



947
948
949
# File 'lib/scjson/types.rb', line 947

def log
  @log
end

#other_attributesObject

Returns the value of attribute other_attributes.



947
948
949
# File 'lib/scjson/types.rb', line 947

def other_attributes
  @other_attributes
end

#other_elementObject

Returns the value of attribute other_element.



947
948
949
# File 'lib/scjson/types.rb', line 947

def other_element
  @other_element
end

#raise_valueObject

Returns the value of attribute raise_value.



947
948
949
# File 'lib/scjson/types.rb', line 947

def raise_value
  @raise_value
end

#scriptObject

Returns the value of attribute script.



947
948
949
# File 'lib/scjson/types.rb', line 947

def script
  @script
end

#sendObject

Returns the value of attribute send.



947
948
949
# File 'lib/scjson/types.rb', line 947

def send
  @send
end

Class Method Details

.from_hash(data) ⇒ IfProps

Build an instance from a Hash representation.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
# File 'lib/scjson/types.rb', line 969

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[:elseif] = normalized.key?('elseif') && normalized['elseif'] ? ElseifProps.from_hash(normalized['elseif']) : nil
  kwargs[:else_value] = normalized.key?('else_value') && normalized['else_value'] ? ElseProps.from_hash(normalized['else_value']) : nil
  kwargs[:cond] = normalized.fetch('cond', '')
  kwargs[:other_attributes] = normalized.fetch('other_attributes', {})
  new(**kwargs)
end

.from_json(json) ⇒ IfProps

Deserialize an instance from a JSON payload.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



993
994
995
996
# File 'lib/scjson/types.rb', line 993

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)


1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
# File 'lib/scjson/types.rb', line 1000

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 },
    'elseif' => @elseif&.to_hash,
    'else_value' => @else_value&.to_hash,
    'cond' => @cond,
    'other_attributes' => @other_attributes
  }
end

#to_json(*opts) ⇒ String

Serialize the object to JSON.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


1021
1022
1023
# File 'lib/scjson/types.rb', line 1021

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