Class: Scjson::Types::AssignProps

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

Overview

update a datamodel location with an expression or value.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ AssignProps

Instantiate a new AssignProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



230
231
232
233
234
235
236
237
# File 'lib/scjson/types.rb', line 230

def initialize(**kwargs)
  @location = kwargs.fetch(:location, '')
  @expr = kwargs.fetch(:expr, nil)
  @type_value = kwargs.fetch(:type_value, AssignTypeDatatypeProps::REPLACECHILDREN)
  @attr = kwargs.fetch(:attr, nil)
  @other_attributes = kwargs.fetch(:other_attributes, {})
  @content = kwargs.fetch(:content, [])
end

Instance Attribute Details

#attrObject

Returns the value of attribute attr.



227
228
229
# File 'lib/scjson/types.rb', line 227

def attr
  @attr
end

#contentObject

Returns the value of attribute content.



227
228
229
# File 'lib/scjson/types.rb', line 227

def content
  @content
end

#exprObject

Returns the value of attribute expr.



227
228
229
# File 'lib/scjson/types.rb', line 227

def expr
  @expr
end

#locationObject

Returns the value of attribute location.



227
228
229
# File 'lib/scjson/types.rb', line 227

def location
  @location
end

#other_attributesObject

Returns the value of attribute other_attributes.



227
228
229
# File 'lib/scjson/types.rb', line 227

def other_attributes
  @other_attributes
end

#type_valueObject

Returns the value of attribute type_value.



227
228
229
# File 'lib/scjson/types.rb', line 227

def type_value
  @type_value
end

Class Method Details

.from_hash(data) ⇒ AssignProps

Build an instance from a Hash representation.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/scjson/types.rb', line 242

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

  normalized = data.transform_keys(&:to_s)
  kwargs = {}
  kwargs[:location] = normalized.fetch('location', '')
  kwargs[:expr] = normalized.fetch('expr', nil)
  kwargs[:type_value] = AssignTypeDatatypeProps.coerce(normalized.fetch('type_value', AssignTypeDatatypeProps::REPLACECHILDREN))
  kwargs[:attr] = normalized.fetch('attr', nil)
  kwargs[:other_attributes] = normalized.fetch('other_attributes', {})
  kwargs[:content] = Array(normalized.fetch('content', []))
  new(**kwargs)
end

.from_json(json) ⇒ AssignProps

Deserialize an instance from a JSON payload.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



259
260
261
262
# File 'lib/scjson/types.rb', line 259

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)


266
267
268
269
270
271
272
273
274
275
# File 'lib/scjson/types.rb', line 266

def to_hash
  {
    'location' => @location,
    'expr' => @expr,
    'type_value' => @type_value,
    'attr' => @attr,
    'other_attributes' => @other_attributes,
    'content' => @content
  }
end

#to_json(*opts) ⇒ String

Serialize the object to JSON.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


280
281
282
# File 'lib/scjson/types.rb', line 280

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