Module: ParamsReady::Marshaller::StructMarshallers::StructMarshaller

Extended by:
AbstractMarshaller
Defined in:
lib/params_ready/marshaller/struct_marshallers.rb

Class Method Summary collapse

Methods included from AbstractMarshaller

extract_bare_value

Class Method Details

.canonicalize(definition, hash, context, validator, freeze: false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/params_ready/marshaller/struct_marshallers.rb', line 45

def self.canonicalize(definition, hash, context, validator, freeze: false)
  hash = if definition.respond_to?(:remap?) && definition.remap?(context)
    definition.key_map.to_standard(hash)
  else
    hash
  end

  value = definition.names.each_with_object({}) do |(name, child_def), result|
    child = child_def.create
    child.set_from_hash(hash, validator: validator&.for_child(name), context: context)
    child.freeze if freeze
    result[name] = child
  end
  [value, validator]
end

.marshal(parameter, intent) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/params_ready/marshaller/struct_marshallers.rb', line 61

def self.marshal(parameter, intent)
  value = extract_bare_value(parameter, intent)

  definition = parameter.definition

  if value == {}
    if intent.marshal?(definition.name_for_formatter)
      if definition.optional? || definition.default_defined?
        parameter.class::EMPTY_HASH
      elsif intent.omit?(parameter)
        nil
      else
        value
      end
    else
      value
    end
  elsif definition.respond_to?(:remap?) && definition.remap?(intent)
    definition.key_map.to_alternative(value)
  else
    value
  end
end