Class: Scjson::Types::FinalProps
- Inherits:
-
Object
- Object
- Scjson::Types::FinalProps
- Defined in:
- lib/scjson/types.rb
Overview
Structured type for scjson elements.
Instance Attribute Summary collapse
-
#donedata ⇒ Object
Returns the value of attribute donedata.
-
#id ⇒ Object
Returns the value of attribute id.
-
#onentry ⇒ Object
Returns the value of attribute onentry.
-
#onexit ⇒ Object
Returns the value of attribute onexit.
-
#other_attributes ⇒ Object
Returns the value of attribute other_attributes.
-
#other_element ⇒ Object
Returns the value of attribute other_element.
Class Method Summary collapse
-
.from_hash(data) ⇒ FinalProps
Build an instance from a Hash representation.
-
.from_json(json) ⇒ FinalProps
Deserialize an instance from a JSON payload.
Instance Method Summary collapse
-
#initialize(**kwargs) ⇒ FinalProps
constructor
Instantiate a new FinalProps 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) ⇒ FinalProps
Instantiate a new FinalProps object.
668 669 670 671 672 673 674 675 |
# File 'lib/scjson/types.rb', line 668 def initialize(**kwargs) @onentry = kwargs.fetch(:onentry, []) @onexit = kwargs.fetch(:onexit, []) @donedata = kwargs.fetch(:donedata, []) @other_element = kwargs.fetch(:other_element, []) @id = kwargs.fetch(:id, nil) @other_attributes = kwargs.fetch(:other_attributes, {}) end |
Instance Attribute Details
#donedata ⇒ Object
Returns the value of attribute donedata.
665 666 667 |
# File 'lib/scjson/types.rb', line 665 def donedata @donedata end |
#id ⇒ Object
Returns the value of attribute id.
665 666 667 |
# File 'lib/scjson/types.rb', line 665 def id @id end |
#onentry ⇒ Object
Returns the value of attribute onentry.
665 666 667 |
# File 'lib/scjson/types.rb', line 665 def onentry @onentry end |
#onexit ⇒ Object
Returns the value of attribute onexit.
665 666 667 |
# File 'lib/scjson/types.rb', line 665 def onexit @onexit end |
#other_attributes ⇒ Object
Returns the value of attribute other_attributes.
665 666 667 |
# File 'lib/scjson/types.rb', line 665 def other_attributes @other_attributes end |
#other_element ⇒ Object
Returns the value of attribute other_element.
665 666 667 |
# File 'lib/scjson/types.rb', line 665 def other_element @other_element end |
Class Method Details
.from_hash(data) ⇒ FinalProps
Build an instance from a Hash representation.
680 681 682 683 684 685 686 687 688 689 690 691 692 |
# File 'lib/scjson/types.rb', line 680 def self.from_hash(data) raise ArgumentError, 'Expected Hash' unless data.is_a?(Hash) normalized = data.transform_keys(&:to_s) kwargs = {} kwargs[:onentry] = Array(normalized.fetch('onentry', [])).map { |item| OnentryProps.from_hash(item) } kwargs[:onexit] = Array(normalized.fetch('onexit', [])).map { |item| OnexitProps.from_hash(item) } kwargs[:donedata] = Array(normalized.fetch('donedata', [])).map { |item| DonedataProps.from_hash(item) } kwargs[:other_element] = Array(normalized.fetch('other_element', [])) kwargs[:id] = normalized.fetch('id', nil) kwargs[:other_attributes] = normalized.fetch('other_attributes', {}) new(**kwargs) end |
.from_json(json) ⇒ FinalProps
Deserialize an instance from a JSON payload.
697 698 699 700 |
# File 'lib/scjson/types.rb', line 697 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.
704 705 706 707 708 709 710 711 712 713 |
# File 'lib/scjson/types.rb', line 704 def to_hash { 'onentry' => (@onentry || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item }, 'onexit' => (@onexit || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item }, 'donedata' => (@donedata || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item }, 'other_element' => @other_element, 'id' => @id, 'other_attributes' => @other_attributes } end |
#to_json(*opts) ⇒ String
Serialize the object to JSON.
718 719 720 |
# File 'lib/scjson/types.rb', line 718 def to_json(*opts) JSON.generate(to_hash, *opts) end |