Class: Scjson::Types::ContentProps
- Inherits:
-
Object
- Object
- Scjson::Types::ContentProps
- Defined in:
- lib/scjson/types.rb
Overview
Structured type for scjson elements.
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#expr ⇒ Object
Returns the value of attribute expr.
-
#other_attributes ⇒ Object
Returns the value of attribute other_attributes.
Class Method Summary collapse
-
.from_hash(data) ⇒ ContentProps
Build an instance from a Hash representation.
-
.from_json(json) ⇒ ContentProps
Deserialize an instance from a JSON payload.
Instance Method Summary collapse
-
#initialize(**kwargs) ⇒ ContentProps
constructor
Instantiate a new ContentProps 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) ⇒ ContentProps
Instantiate a new ContentProps object.
350 351 352 353 354 |
# File 'lib/scjson/types.rb', line 350 def initialize(**kwargs) @content = kwargs.fetch(:content, nil) @expr = kwargs.fetch(:expr, nil) @other_attributes = kwargs.fetch(:other_attributes, {}) end |
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
347 348 349 |
# File 'lib/scjson/types.rb', line 347 def content @content end |
#expr ⇒ Object
Returns the value of attribute expr.
347 348 349 |
# File 'lib/scjson/types.rb', line 347 def expr @expr end |
#other_attributes ⇒ Object
Returns the value of attribute other_attributes.
347 348 349 |
# File 'lib/scjson/types.rb', line 347 def other_attributes @other_attributes end |
Class Method Details
.from_hash(data) ⇒ ContentProps
Build an instance from a Hash representation.
359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/scjson/types.rb', line 359 def self.from_hash(data) raise ArgumentError, 'Expected Hash' unless data.is_a?(Hash) normalized = data.transform_keys(&:to_s) kwargs = {} kwargs[:content] = begin value = normalized.fetch('content', nil) value.nil? ? nil : Array(value).map { |item| ScxmlProps.from_hash(item) } end kwargs[:expr] = normalized.fetch('expr', nil) kwargs[:other_attributes] = normalized.fetch('other_attributes', {}) new(**kwargs) end |
.from_json(json) ⇒ ContentProps
Deserialize an instance from a JSON payload.
376 377 378 379 |
# File 'lib/scjson/types.rb', line 376 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.
383 384 385 386 387 388 389 |
# File 'lib/scjson/types.rb', line 383 def to_hash { 'content' => (@content || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item }, 'expr' => @expr, 'other_attributes' => @other_attributes } end |
#to_json(*opts) ⇒ String
Serialize the object to JSON.
394 395 396 |
# File 'lib/scjson/types.rb', line 394 def to_json(*opts) JSON.generate(to_hash, *opts) end |