Class: Scjson::Types::DataProps

Inherits:
Object
  • Object
show all
Defined in:
lib/scjson/types.rb

Overview

represents a single datamodel variable.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ DataProps

Instantiate a new DataProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



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

#contentObject

Returns the value of attribute content.



404
405
406
# File 'lib/scjson/types.rb', line 404

def content
  @content
end

#exprObject

Returns the value of attribute expr.



404
405
406
# File 'lib/scjson/types.rb', line 404

def expr
  @expr
end

#idObject

Returns the value of attribute id.



404
405
406
# File 'lib/scjson/types.rb', line 404

def id
  @id
end

#other_attributesObject

Returns the value of attribute other_attributes.



404
405
406
# File 'lib/scjson/types.rb', line 404

def other_attributes
  @other_attributes
end

#srcObject

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.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


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.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



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_hashHash

Convert the object to a Hash suitable for JSON serialization.

Returns:

  • (Hash)


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.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


454
455
456
# File 'lib/scjson/types.rb', line 454

def to_json(*opts)
  JSON.generate(to_hash, *opts)
end