class AMA::Entity::Mapper::Handler::Entity::Denormalizer
Default denormalization processor
Constants
- INSTANCE
Private Class Methods
handler_factory(implementation, fallback)
click to toggle source
@param [Denormalizer] implementation @param [Denormalizer] fallback @return [Denormalizer]
# File lib/ama-entity-mapper/handler/entity/denormalizer.rb, line 75 def handler_factory(implementation, fallback) lambda do |source, type, ctx| begin implementation.denormalize(source, type, ctx) do |s, t, c| fallback.denormalize(s, t, c) end rescue StandardError => e raise_if_internal(e) message = 'Unexpected error from denormalizer ' \ "#{implementation}" signature = '(source, type, context)' options = { parent: e, context: ctx, signature: signature } compliance_error(message, **options) end end end
wrap(implementation)
click to toggle source
@param [Denormalizer] implementation @return [Denormalizer]
# 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
Public Instance Methods
denormalize(source, type, context = nil)
click to toggle source
@param [Hash] source @param [AMA::Entity::Mapper::Type] type @param [AMA::Entity::Mapper::Context] context
# 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
Private Instance Methods
candidate_names(attribute)
click to toggle source
@param [AMA::Entity::Mapper::Type::Attribute] attribute @return [Array<Symbol, String>]
# File lib/ama-entity-mapper/handler/entity/denormalizer.rb, line 39 def candidate_names(attribute) [attribute.name, *attribute.aliases].flat_map do |candidate| [candidate, candidate.to_s] end end
validate_source!(source, type, context)
click to toggle source
@param [Hash] source @param [AMA::Entity::Mapper::Type] type @param [AMA::Entity::Mapper::Context] context
# File lib/ama-entity-mapper/handler/entity/denormalizer.rb, line 48 def validate_source!(source, type, context) return if source.is_a?(Hash) message = "Expected Hash, #{source.class} provided " \ "(while denormalizing #{type})" mapping_error(message, context: context) end