Class: Scjson::Types::ParamProps

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

Overview

parameter passed to ‘<invoke>` or `<send>`.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ ParamProps

Instantiate a new ParamProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



1457
1458
1459
1460
1461
1462
1463
# File 'lib/scjson/types.rb', line 1457

def initialize(**kwargs)
  @other_element = kwargs.fetch(:other_element, [])
  @name = kwargs.fetch(:name, '')
  @expr = kwargs.fetch(:expr, nil)
  @location = kwargs.fetch(:location, nil)
  @other_attributes = kwargs.fetch(:other_attributes, {})
end

Instance Attribute Details

#exprObject

Returns the value of attribute expr.



1454
1455
1456
# File 'lib/scjson/types.rb', line 1454

def expr
  @expr
end

#locationObject

Returns the value of attribute location.



1454
1455
1456
# File 'lib/scjson/types.rb', line 1454

def location
  @location
end

#nameObject

Returns the value of attribute name.



1454
1455
1456
# File 'lib/scjson/types.rb', line 1454

def name
  @name
end

#other_attributesObject

Returns the value of attribute other_attributes.



1454
1455
1456
# File 'lib/scjson/types.rb', line 1454

def other_attributes
  @other_attributes
end

#other_elementObject

Returns the value of attribute other_element.



1454
1455
1456
# File 'lib/scjson/types.rb', line 1454

def other_element
  @other_element
end

Class Method Details

.from_hash(data) ⇒ ParamProps

Build an instance from a Hash representation.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
# File 'lib/scjson/types.rb', line 1468

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

  normalized = data.transform_keys(&:to_s)
  kwargs = {}
  kwargs[:other_element] = Array(normalized.fetch('other_element', []))
  kwargs[:name] = normalized.fetch('name', '')
  kwargs[:expr] = normalized.fetch('expr', nil)
  kwargs[:location] = normalized.fetch('location', nil)
  kwargs[:other_attributes] = normalized.fetch('other_attributes', {})
  new(**kwargs)
end

.from_json(json) ⇒ ParamProps

Deserialize an instance from a JSON payload.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



1484
1485
1486
1487
# File 'lib/scjson/types.rb', line 1484

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)


1491
1492
1493
1494
1495
1496
1497
1498
1499
# File 'lib/scjson/types.rb', line 1491

def to_hash
  {
    'other_element' => @other_element,
    'name' => @name,
    'expr' => @expr,
    'location' => @location,
    'other_attributes' => @other_attributes
  }
end

#to_json(*opts) ⇒ String

Serialize the object to JSON.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


1504
1505
1506
# File 'lib/scjson/types.rb', line 1504

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