Class: Scjson::Types::HistoryProps
- Inherits:
-
Object
- Object
- Scjson::Types::HistoryProps
- Defined in:
- lib/scjson/types.rb
Overview
Structured type for scjson elements.
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#other_attributes ⇒ Object
Returns the value of attribute other_attributes.
-
#other_element ⇒ Object
Returns the value of attribute other_element.
-
#transition ⇒ Object
Returns the value of attribute transition.
-
#type_value ⇒ Object
Returns the value of attribute type_value.
Class Method Summary collapse
-
.from_hash(data) ⇒ HistoryProps
Build an instance from a Hash representation.
-
.from_json(json) ⇒ HistoryProps
Deserialize an instance from a JSON payload.
Instance Method Summary collapse
-
#initialize(**kwargs) ⇒ HistoryProps
constructor
Instantiate a new HistoryProps 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) ⇒ HistoryProps
Instantiate a new HistoryProps object.
890 891 892 893 894 895 896 |
# File 'lib/scjson/types.rb', line 890 def initialize(**kwargs) @other_element = kwargs.fetch(:other_element, []) @transition = kwargs.fetch(:transition, TransitionProps.new) @id = kwargs.fetch(:id, nil) @type_value = kwargs.fetch(:type_value, nil) @other_attributes = kwargs.fetch(:other_attributes, {}) end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
887 888 889 |
# File 'lib/scjson/types.rb', line 887 def id @id end |
#other_attributes ⇒ Object
Returns the value of attribute other_attributes.
887 888 889 |
# File 'lib/scjson/types.rb', line 887 def other_attributes @other_attributes end |
#other_element ⇒ Object
Returns the value of attribute other_element.
887 888 889 |
# File 'lib/scjson/types.rb', line 887 def other_element @other_element end |
#transition ⇒ Object
Returns the value of attribute transition.
887 888 889 |
# File 'lib/scjson/types.rb', line 887 def transition @transition end |
#type_value ⇒ Object
Returns the value of attribute type_value.
887 888 889 |
# File 'lib/scjson/types.rb', line 887 def type_value @type_value end |
Class Method Details
.from_hash(data) ⇒ HistoryProps
Build an instance from a Hash representation.
901 902 903 904 905 906 907 908 909 910 911 912 |
# File 'lib/scjson/types.rb', line 901 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[:transition] = normalized.key?('transition') && normalized['transition'] ? TransitionProps.from_hash(normalized['transition']) : TransitionProps.new kwargs[:id] = normalized.fetch('id', nil) kwargs[:type_value] = normalized.key?('type_value') ? HistoryTypeDatatypeProps.coerce(normalized['type_value'], allow_nil: true) : nil kwargs[:other_attributes] = normalized.fetch('other_attributes', {}) new(**kwargs) end |
.from_json(json) ⇒ HistoryProps
Deserialize an instance from a JSON payload.
917 918 919 920 |
# File 'lib/scjson/types.rb', line 917 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.
924 925 926 927 928 929 930 931 932 |
# File 'lib/scjson/types.rb', line 924 def to_hash { 'other_element' => @other_element, 'transition' => @transition&.to_hash, 'id' => @id, 'type_value' => @type_value, 'other_attributes' => @other_attributes } end |
#to_json(*opts) ⇒ String
Serialize the object to JSON.
937 938 939 |
# File 'lib/scjson/types.rb', line 937 def to_json(*opts) JSON.generate(to_hash, *opts) end |