Class: AMA::Entity::Mapper::Handler::Entity::Denormalizer

Inherits:
Object
  • Object
show all
Extended by:
Mixin::Reflection
Includes:
Mixin::Errors, Mixin::Reflection
Defined in:
lib/ama-entity-mapper/handler/entity/denormalizer.rb

Overview

Default denormalization processor

Constant Summary collapse

INSTANCE =
new

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::Reflection

install_object_method, method_object, object_variable, object_variable_exists, object_variables, set_object_attribute

Methods included from Mixin::Errors

#compliance_error, #mapping_error, #raise_if_internal, #validation_error

Class Method Details

.wrap(implementation) ⇒ Denormalizer

Parameters:

Returns:



60
61
62
63
64
65
66
67
68
# File 'lib/ama-entity-mapper/handler/entity/denormalizer.rb', line 60

def wrap(implementation)
  handler = handler_factory(implementation, INSTANCE)
  depiction = "Safety wrapper for #{implementation}"
  wrapper = method_object(:denormalize, to_s: depiction, &handler)
  wrapper.singleton_class.instance_eval do
    include Mixin::Errors
  end
  wrapper
end

Instance Method Details

#denormalize(source, type, context = nil) ⇒ Object

Parameters:



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ama-entity-mapper/handler/entity/denormalizer.rb', line 21

def denormalize(source, type, context = nil)
  validate_source!(source, type, context)
  entity = type.factory.create(type, source, context)
  type.attributes.values.each do |attribute|
    next if attribute.virtual
    candidate_names(attribute).each do |name|
      next unless source.key?(name)
      value = source[name]
      break set_object_attribute(entity, attribute.name, value)
    end
  end
  entity
end