Class: Scjson::Types::ParallelProps

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

Instantiate a new ParallelProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
# File 'lib/scjson/types.rb', line 1379

def initialize(**kwargs)
  @onentry = kwargs.fetch(:onentry, [])
  @onexit = kwargs.fetch(:onexit, [])
  @transition = kwargs.fetch(:transition, [])
  @state = kwargs.fetch(:state, [])
  @parallel = kwargs.fetch(:parallel, [])
  @history = kwargs.fetch(:history, [])
  @datamodel = kwargs.fetch(:datamodel, [])
  @invoke = kwargs.fetch(:invoke, [])
  @other_element = kwargs.fetch(:other_element, [])
  @id = kwargs.fetch(:id, nil)
  @other_attributes = kwargs.fetch(:other_attributes, {})
end

Instance Attribute Details

#datamodelObject

Returns the value of attribute datamodel.



1376
1377
1378
# File 'lib/scjson/types.rb', line 1376

def datamodel
  @datamodel
end

#historyObject

Returns the value of attribute history.



1376
1377
1378
# File 'lib/scjson/types.rb', line 1376

def history
  @history
end

#idObject

Returns the value of attribute id.



1376
1377
1378
# File 'lib/scjson/types.rb', line 1376

def id
  @id
end

#invokeObject

Returns the value of attribute invoke.



1376
1377
1378
# File 'lib/scjson/types.rb', line 1376

def invoke
  @invoke
end

#onentryObject

Returns the value of attribute onentry.



1376
1377
1378
# File 'lib/scjson/types.rb', line 1376

def onentry
  @onentry
end

#onexitObject

Returns the value of attribute onexit.



1376
1377
1378
# File 'lib/scjson/types.rb', line 1376

def onexit
  @onexit
end

#other_attributesObject

Returns the value of attribute other_attributes.



1376
1377
1378
# File 'lib/scjson/types.rb', line 1376

def other_attributes
  @other_attributes
end

#other_elementObject

Returns the value of attribute other_element.



1376
1377
1378
# File 'lib/scjson/types.rb', line 1376

def other_element
  @other_element
end

#parallelObject

Returns the value of attribute parallel.



1376
1377
1378
# File 'lib/scjson/types.rb', line 1376

def parallel
  @parallel
end

#stateObject

Returns the value of attribute state.



1376
1377
1378
# File 'lib/scjson/types.rb', line 1376

def state
  @state
end

#transitionObject

Returns the value of attribute transition.



1376
1377
1378
# File 'lib/scjson/types.rb', line 1376

def transition
  @transition
end

Class Method Details

.from_hash(data) ⇒ ParallelProps

Build an instance from a Hash representation.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
# File 'lib/scjson/types.rb', line 1396

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

  normalized = data.transform_keys(&:to_s)
  kwargs = {}
  kwargs[:onentry] = Array(normalized.fetch('onentry', [])).map { |item| OnentryProps.from_hash(item) }
  kwargs[:onexit] = Array(normalized.fetch('onexit', [])).map { |item| OnexitProps.from_hash(item) }
  kwargs[:transition] = Array(normalized.fetch('transition', [])).map { |item| TransitionProps.from_hash(item) }
  kwargs[:state] = Array(normalized.fetch('state', [])).map { |item| StateProps.from_hash(item) }
  kwargs[:parallel] = Array(normalized.fetch('parallel', [])).map { |item| ParallelProps.from_hash(item) }
  kwargs[:history] = Array(normalized.fetch('history', [])).map { |item| HistoryProps.from_hash(item) }
  kwargs[:datamodel] = Array(normalized.fetch('datamodel', [])).map { |item| DatamodelProps.from_hash(item) }
  kwargs[:invoke] = Array(normalized.fetch('invoke', [])).map { |item| InvokeProps.from_hash(item) }
  kwargs[:other_element] = Array(normalized.fetch('other_element', []))
  kwargs[:id] = normalized.fetch('id', nil)
  kwargs[:other_attributes] = normalized.fetch('other_attributes', {})
  new(**kwargs)
end

.from_json(json) ⇒ ParallelProps

Deserialize an instance from a JSON payload.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



1418
1419
1420
1421
# File 'lib/scjson/types.rb', line 1418

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)


1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
# File 'lib/scjson/types.rb', line 1425

def to_hash
  {
    'onentry' => (@onentry || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'onexit' => (@onexit || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'transition' => (@transition || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'state' => (@state || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'parallel' => (@parallel || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'history' => (@history || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'datamodel' => (@datamodel || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'invoke' => (@invoke || []).map { |item| item.respond_to?(:to_hash) ? item.to_hash : item },
    'other_element' => @other_element,
    'id' => @id,
    'other_attributes' => @other_attributes
  }
end

#to_json(*opts) ⇒ String

Serialize the object to JSON.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


1444
1445
1446
# File 'lib/scjson/types.rb', line 1444

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