Class: Scjson::Types::InitialProps

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

Instantiate a new InitialProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



1034
1035
1036
1037
1038
# File 'lib/scjson/types.rb', line 1034

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

Instance Attribute Details

#other_attributesObject

Returns the value of attribute other_attributes.



1031
1032
1033
# File 'lib/scjson/types.rb', line 1031

def other_attributes
  @other_attributes
end

#other_elementObject

Returns the value of attribute other_element.



1031
1032
1033
# File 'lib/scjson/types.rb', line 1031

def other_element
  @other_element
end

#transitionObject

Returns the value of attribute transition.



1031
1032
1033
# File 'lib/scjson/types.rb', line 1031

def transition
  @transition
end

Class Method Details

.from_hash(data) ⇒ InitialProps

Build an instance from a Hash representation.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
# File 'lib/scjson/types.rb', line 1043

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[:other_attributes] = normalized.fetch('other_attributes', {})
  new(**kwargs)
end

.from_json(json) ⇒ InitialProps

Deserialize an instance from a JSON payload.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



1057
1058
1059
1060
# File 'lib/scjson/types.rb', line 1057

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)


1064
1065
1066
1067
1068
1069
1070
# File 'lib/scjson/types.rb', line 1064

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

#to_json(*opts) ⇒ String

Serialize the object to JSON.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


1075
1076
1077
# File 'lib/scjson/types.rb', line 1075

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