class ParamsReady::Marshaller::PolymorphMarshallers::HashMarshaller

Attributes

type_identifier[R]

Public Class Methods

instance(type_identifier:) click to toggle source
# File lib/params_ready/marshaller/polymorph_marshallers.rb, line 9
def self.instance(type_identifier:)
  marshaller = new type_identifier
  marshaller.freeze
  [Hash, marshaller]
end
new(type_identifier) click to toggle source
# File lib/params_ready/marshaller/polymorph_marshallers.rb, line 15
def initialize(type_identifier)
  @type_identifier = type_identifier.to_sym
end

Public Instance Methods

canonicalize(definition, hash, context, validator) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/params_ready/marshaller/polymorph_marshallers.rb, line 19
def reserved?(name)
  name == type_identifier
end
type(definition, key, value, context, validator) click to toggle source
# 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