class AMA::Entity::Mapper::Handler::Entity::Normalizer

Default normalization handler

Constants

INSTANCE

Public Class Methods

wrap(implementation) click to toggle source

@param [Normalizer] implementation @return [Normalizer]

# File lib/ama-entity-mapper/handler/entity/normalizer.rb, line 34
def wrap(implementation)
  handler = handler_factory(implementation, INSTANCE)
  description = "Safety wrapper for #{implementation}"
  wrapper = method_object(:normalize, to_s: description, &handler)
  wrapper.singleton_class.instance_eval do
    include Mixin::Errors
  end
  wrapper
end

Private Class Methods

handler_factory(impl, fallback) click to toggle source

@param [Normalizer] impl @param [Normalizer] fallback @return [Proc]

# File lib/ama-entity-mapper/handler/entity/normalizer.rb, line 49
def handler_factory(impl, fallback)
  lambda do |entity, type, ctx|
    begin
      impl.normalize(entity, type, ctx) do |e, t, c|
        fallback.normalize(e, t, c)
      end
    rescue StandardError => e
      raise_if_internal(e)
      message = "Unexpected error from normalizer #{impl}"
      signature = '(entity, type, context)'
      options = { parent: e, context: ctx, signature: signature }
      compliance_error(message, **options)
    end
  end
end

Public Instance Methods

normalize(entity, type, context) click to toggle source

@param [Object] entity @param [AMA::Entity::Mapper::Type] type @param [AMA::Entity::Mapper::Context] context

# File lib/ama-entity-mapper/handler/entity/normalizer.rb, line 20
def normalize(entity, type, context)
  type.attributes.values.each_with_object({}) do |attribute, data|
    next if attribute.virtual
    condition = context.include_sensitive_attributes
    next if attribute.sensitive && !condition
    data[attribute.name] = object_variable(entity, attribute.name)
  end
end