Class: Scjson::Types::ContentProps

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) ⇒ ContentProps

Instantiate a new ContentProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



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

#contentObject

Returns the value of attribute content.



347
348
349
# File 'lib/scjson/types.rb', line 347

def content
  @content
end

#exprObject

Returns the value of attribute expr.



347
348
349
# File 'lib/scjson/types.rb', line 347

def expr
  @expr
end

#other_attributesObject

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.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


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.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



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_hashHash

Convert the object to a Hash suitable for JSON serialization.

Returns:

  • (Hash)


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.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


394
395
396
# File 'lib/scjson/types.rb', line 394

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