Class: Scjson::Types::DonedataProps

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

Overview

Structured type for scjson elements.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ DonedataProps

Instantiate a new DonedataProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



521
522
523
524
525
# File 'lib/scjson/types.rb', line 521

def initialize(**kwargs)
  @content = kwargs.fetch(:content, nil)
  @param = kwargs.fetch(:param, [])
  @other_attributes = kwargs.fetch(:other_attributes, {})
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



518
519
520
# File 'lib/scjson/types.rb', line 518

def content
  @content
end

#other_attributesObject

Returns the value of attribute other_attributes.



518
519
520
# File 'lib/scjson/types.rb', line 518

def other_attributes
  @other_attributes
end

#paramObject

Returns the value of attribute param.



518
519
520
# File 'lib/scjson/types.rb', line 518

def param
  @param
end

Class Method Details

.from_hash(data) ⇒ DonedataProps

Build an instance from a Hash representation.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


530
531
532
533
534
535
536
537
538
539
# File 'lib/scjson/types.rb', line 530

def self.from_hash(data)
  raise ArgumentError, 'Expected Hash' unless data.is_a?(Hash)

  normalized = data.transform_keys(&:to_s)
  kwargs = {}
  kwargs[:content] = normalized.key?('content') && normalized['content'] ? ContentProps.from_hash(normalized['content']) : nil
  kwargs[:param] = Array(normalized.fetch('param', [])).map { |item| ParamProps.from_hash(item) }
  kwargs[:other_attributes] = normalized.fetch('other_attributes', {})
  new(**kwargs)
end

.from_json(json) ⇒ DonedataProps

Deserialize an instance from a JSON payload.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



544
545
546
547
# File 'lib/scjson/types.rb', line 544

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)


551
552
553
554
555
556
557
# File 'lib/scjson/types.rb', line 551

def to_hash
  {
    'content' => @content&.to_hash,
    'param' => (@param || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'other_attributes' => @other_attributes
  }
end

#to_json(*opts) ⇒ String

Serialize the object to JSON.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


562
563
564
# File 'lib/scjson/types.rb', line 562

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