Class: Scjson::Types::HistoryProps

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

Instantiate a new HistoryProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



890
891
892
893
894
895
896
# File 'lib/scjson/types.rb', line 890

def initialize(**kwargs)
  @other_element = kwargs.fetch(:other_element, [])
  @transition = kwargs.fetch(:transition, TransitionProps.new)
  @id = kwargs.fetch(:id, nil)
  @type_value = kwargs.fetch(:type_value, nil)
  @other_attributes = kwargs.fetch(:other_attributes, {})
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



887
888
889
# File 'lib/scjson/types.rb', line 887

def id
  @id
end

#other_attributesObject

Returns the value of attribute other_attributes.



887
888
889
# File 'lib/scjson/types.rb', line 887

def other_attributes
  @other_attributes
end

#other_elementObject

Returns the value of attribute other_element.



887
888
889
# File 'lib/scjson/types.rb', line 887

def other_element
  @other_element
end

#transitionObject

Returns the value of attribute transition.



887
888
889
# File 'lib/scjson/types.rb', line 887

def transition
  @transition
end

#type_valueObject

Returns the value of attribute type_value.



887
888
889
# File 'lib/scjson/types.rb', line 887

def type_value
  @type_value
end

Class Method Details

.from_hash(data) ⇒ HistoryProps

Build an instance from a Hash representation.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


901
902
903
904
905
906
907
908
909
910
911
912
# File 'lib/scjson/types.rb', line 901

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[:transition] = normalized.key?('transition') && normalized['transition'] ? TransitionProps.from_hash(normalized['transition']) : TransitionProps.new
  kwargs[:id] = normalized.fetch('id', nil)
  kwargs[:type_value] = normalized.key?('type_value') ? HistoryTypeDatatypeProps.coerce(normalized['type_value'], allow_nil: true) : nil
  kwargs[:other_attributes] = normalized.fetch('other_attributes', {})
  new(**kwargs)
end

.from_json(json) ⇒ HistoryProps

Deserialize an instance from a JSON payload.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



917
918
919
920
# File 'lib/scjson/types.rb', line 917

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)


924
925
926
927
928
929
930
931
932
# File 'lib/scjson/types.rb', line 924

def to_hash
  {
    'other_element' => @other_element,
    'transition' => @transition&.to_hash,
    'id' => @id,
    'type_value' => @type_value,
    'other_attributes' => @other_attributes
  }
end

#to_json(*opts) ⇒ String

Serialize the object to JSON.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


937
938
939
# File 'lib/scjson/types.rb', line 937

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