Class: ParamsReady::Marshaller::PolymorphMarshallers::StructMarshaller

Inherits:
Object
  • Object
show all
Defined in:
lib/params_ready/marshaller/polymorph_marshallers.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_identifier) ⇒ StructMarshaller

Returns a new instance of StructMarshaller.



15
16
17
# File 'lib/params_ready/marshaller/polymorph_marshallers.rb', line 15

def initialize(type_identifier)
  @type_identifier = type_identifier.to_sym
end

Instance Attribute Details

#type_identifierObject (readonly)

Returns the value of attribute type_identifier.



7
8
9
# File 'lib/params_ready/marshaller/polymorph_marshallers.rb', line 7

def type_identifier
  @type_identifier
end

Class Method Details

.instance(type_identifier:) ⇒ Object



9
10
11
12
13
# File 'lib/params_ready/marshaller/polymorph_marshallers.rb', line 9

def self.instance(type_identifier:)
  marshaller = new type_identifier
  marshaller.freeze
  [Hash, marshaller]
end

Instance Method Details

#canonicalize(definition, hash, context, validator) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
# File 'lib/params_ready/marshaller/polymorph_marshallers.rb', line 23

def canonicalize(definition, hash, context, validator)
  raise ParamsReadyError, "Type key can't be retrieved" unless hash.length == 1
  key = hash.keys.first
  value = hash.values.first
  value = type(definition, key, value, context, validator)


  [value, validator]
end

#marshal(parameter, intent) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/params_ready/marshaller/polymorph_marshallers.rb', line 44

def marshal(parameter, intent)
  type = parameter.send(:bare_value)

  hash = type.to_hash_if_eligible(intent)
  return hash unless hash.nil?

  value = type.hash_key(intent)
  { type_identifier => value }
end

#reserved?(name) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/params_ready/marshaller/polymorph_marshallers.rb', line 19

def reserved?(name)
  name == type_identifier
end

#type(definition, key, value, context, validator) ⇒ Object

Raises:



33
34
35
36
37
38
39
40
41
42
# File 'lib/params_ready/marshaller/polymorph_marshallers.rb', line 33

def type(definition, key, value, context, validator)
  type_key = key.to_sym == type_identifier ? value : key
  prototype = definition.type(type_key, context)
  raise ParamsReadyError, "Unexpected type for #{definition.name}: #{type_key}" if prototype.nil?

  type = prototype.create
  return type if type_key == value
  type.set_from_input(value, context, validator)
  type
end