Class: Scjson::Types::DataProps
- Inherits:
-
Object
- Object
- Scjson::Types::DataProps
- Defined in:
- lib/scjson/types.rb
Overview
represents a single datamodel variable.
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#expr ⇒ Object
Returns the value of attribute expr.
-
#id ⇒ Object
Returns the value of attribute id.
-
#other_attributes ⇒ Object
Returns the value of attribute other_attributes.
-
#src ⇒ Object
Returns the value of attribute src.
Class Method Summary collapse
-
.from_hash(data) ⇒ DataProps
Build an instance from a Hash representation.
-
.from_json(json) ⇒ DataProps
Deserialize an instance from a JSON payload.
Instance Method Summary collapse
-
#initialize(**kwargs) ⇒ DataProps
constructor
Instantiate a new DataProps 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) ⇒ DataProps
Instantiate a new DataProps object.
407 408 409 410 411 412 413 |
# File 'lib/scjson/types.rb', line 407 def initialize(**kwargs) @id = kwargs.fetch(:id, '') @src = kwargs.fetch(:src, nil) @expr = kwargs.fetch(:expr, nil) @other_attributes = kwargs.fetch(:other_attributes, {}) @content = kwargs.fetch(:content, []) end |
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
404 405 406 |
# File 'lib/scjson/types.rb', line 404 def content @content end |
#expr ⇒ Object
Returns the value of attribute expr.
404 405 406 |
# File 'lib/scjson/types.rb', line 404 def expr @expr end |
#id ⇒ Object
Returns the value of attribute id.
404 405 406 |
# File 'lib/scjson/types.rb', line 404 def id @id end |
#other_attributes ⇒ Object
Returns the value of attribute other_attributes.
404 405 406 |
# File 'lib/scjson/types.rb', line 404 def other_attributes @other_attributes end |
#src ⇒ Object
Returns the value of attribute src.
404 405 406 |
# File 'lib/scjson/types.rb', line 404 def src @src end |
Class Method Details
.from_hash(data) ⇒ DataProps
Build an instance from a Hash representation.
418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/scjson/types.rb', line 418 def self.from_hash(data) raise ArgumentError, 'Expected Hash' unless data.is_a?(Hash) normalized = data.transform_keys(&:to_s) kwargs = {} kwargs[:id] = normalized.fetch('id', '') kwargs[:src] = normalized.fetch('src', nil) kwargs[:expr] = normalized.fetch('expr', nil) kwargs[:other_attributes] = normalized.fetch('other_attributes', {}) kwargs[:content] = Array(normalized.fetch('content', [])) new(**kwargs) end |
.from_json(json) ⇒ DataProps
Deserialize an instance from a JSON payload.
434 435 436 437 |
# File 'lib/scjson/types.rb', line 434 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.
441 442 443 444 445 446 447 448 449 |
# File 'lib/scjson/types.rb', line 441 def to_hash { 'id' => @id, 'src' => @src, 'expr' => @expr, 'other_attributes' => @other_attributes, 'content' => @content } end |
#to_json(*opts) ⇒ String
Serialize the object to JSON.
454 455 456 |
# File 'lib/scjson/types.rb', line 454 def to_json(*opts) JSON.generate(to_hash, *opts) end |