class AMA::Entity::Mapper::Engine::RecursiveNormalizer

Helper and self-explanatory engine class

Public Class Methods

new(registry) click to toggle source

@param [AMA::Entity::Mapper::Type::Registry] registry

# File lib/ama-entity-mapper/engine/recursive_normalizer.rb, line 13
def initialize(registry)
  @registry = registry
end

Public Instance Methods

normalize(entity, ctx, type = nil) click to toggle source

@param [Object] entity @param [AMA::Entity::Mapper::Context] ctx @param [AMA::Entity::Mapper::Type, NilClass] type

# File lib/ama-entity-mapper/engine/recursive_normalizer.rb, line 20
def normalize(entity, ctx, type = nil)
  type ||= find_type(entity.class)
  target = entity
  ctx.logger.debug("Normalizing #{entity.class} as #{type.type}")
  if type.virtual
    message = "Type #{type.type} is virtual, skipping to attributes"
    ctx.logger.debug(message)
  else
    target = type.normalizer.normalize(entity, type, ctx)
  end
  target_type = find_type(target.class)
  process_attributes(target, target_type, ctx)
end

Private Instance Methods

find_type(klass) click to toggle source

@param [Class, Module] klass @return [AMA::Entity::Mapper::Type]

# File lib/ama-entity-mapper/engine/recursive_normalizer.rb, line 67
def find_type(klass)
  @registry.find(klass) || Type::Analyzer.analyze(klass)
end
normalize_attributes(entity, type, ctx) click to toggle source

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

# File lib/ama-entity-mapper/engine/recursive_normalizer.rb, line 52
def normalize_attributes(entity, type, ctx)
  message = "Normalizing attributes of #{entity.class} " \
    "(as #{type.type})"
  ctx.logger.debug(message)
  enumerator = type.enumerator.enumerate(entity, type, ctx)
  enumerator.each do |attribute, value, segment|
    local_ctx = ctx.advance(segment)
    value = normalize(value, local_ctx)
    type.injector.inject(entity, type, attribute, value, local_ctx)
  end
  entity
end
process_attributes(entity, type, ctx) click to toggle source

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

# File lib/ama-entity-mapper/engine/recursive_normalizer.rb, line 39
def process_attributes(entity, type, ctx)
  if type.attributes.empty?
    message = "No attributes found on #{type.type}, returning " \
      "#{entity.class} as is"
    ctx.logger.debug(message)
    return entity
  end
  normalize_attributes(entity, type, ctx)
end