Class: Scjson::Types::IfProps
- Inherits:
-
Object
- Object
- Scjson::Types::IfProps
- Defined in:
- lib/scjson/types.rb
Overview
Structured type for scjson elements.
Instance Attribute Summary collapse
-
#assign ⇒ Object
Returns the value of attribute assign.
-
#cancel ⇒ Object
Returns the value of attribute cancel.
-
#cond ⇒ Object
Returns the value of attribute cond.
-
#else_value ⇒ Object
Returns the value of attribute else_value.
-
#elseif ⇒ Object
Returns the value of attribute elseif.
-
#foreach ⇒ Object
Returns the value of attribute foreach.
-
#if_value ⇒ Object
Returns the value of attribute if_value.
-
#log ⇒ Object
Returns the value of attribute log.
-
#other_attributes ⇒ Object
Returns the value of attribute other_attributes.
-
#other_element ⇒ Object
Returns the value of attribute other_element.
-
#raise_value ⇒ Object
Returns the value of attribute raise_value.
-
#script ⇒ Object
Returns the value of attribute script.
-
#send ⇒ Object
Returns the value of attribute send.
Class Method Summary collapse
-
.from_hash(data) ⇒ IfProps
Build an instance from a Hash representation.
-
.from_json(json) ⇒ IfProps
Deserialize an instance from a JSON payload.
Instance Method Summary collapse
-
#initialize(**kwargs) ⇒ IfProps
constructor
Instantiate a new IfProps 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) ⇒ IfProps
Instantiate a new IfProps object.
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
#assign ⇒ Object
Returns the value of attribute assign.
947 948 949 |
# File 'lib/scjson/types.rb', line 947 def assign @assign end |
#cancel ⇒ Object
Returns the value of attribute cancel.
947 948 949 |
# File 'lib/scjson/types.rb', line 947 def cancel @cancel end |
#cond ⇒ Object
Returns the value of attribute cond.
947 948 949 |
# File 'lib/scjson/types.rb', line 947 def cond @cond end |
#else_value ⇒ Object
Returns the value of attribute else_value.
947 948 949 |
# File 'lib/scjson/types.rb', line 947 def else_value @else_value end |
#elseif ⇒ Object
Returns the value of attribute elseif.
947 948 949 |
# File 'lib/scjson/types.rb', line 947 def elseif @elseif end |
#foreach ⇒ Object
Returns the value of attribute foreach.
947 948 949 |
# File 'lib/scjson/types.rb', line 947 def foreach @foreach end |
#if_value ⇒ Object
Returns the value of attribute if_value.
947 948 949 |
# File 'lib/scjson/types.rb', line 947 def if_value @if_value end |
#log ⇒ Object
Returns the value of attribute log.
947 948 949 |
# File 'lib/scjson/types.rb', line 947 def log @log end |
#other_attributes ⇒ Object
Returns the value of attribute other_attributes.
947 948 949 |
# File 'lib/scjson/types.rb', line 947 def other_attributes @other_attributes end |
#other_element ⇒ Object
Returns the value of attribute other_element.
947 948 949 |
# File 'lib/scjson/types.rb', line 947 def other_element @other_element end |
#raise_value ⇒ Object
Returns the value of attribute raise_value.
947 948 949 |
# File 'lib/scjson/types.rb', line 947 def raise_value @raise_value end |
#script ⇒ Object
Returns the value of attribute script.
947 948 949 |
# File 'lib/scjson/types.rb', line 947 def script @script end |
#send ⇒ Object
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.
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.
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_hash ⇒ Hash
Convert the object to a Hash suitable for JSON serialization.
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.
1021 1022 1023 |
# File 'lib/scjson/types.rb', line 1021 def to_json(*opts) JSON.generate(to_hash, *opts) end |