class AMA::Entity::Mapper::Engine

Main, user-unfriendly, library-entrypoint class. Provides interface for mapping one type into another.

Attributes

registry[R]

@!attribute [r] registry

@return [Type::Registry]
resolver[R]

@!attribute [r] resolver

@return [Type::Resolver]

Public Class Methods

new(registry = nil) click to toggle source

@param [Type::Registry] registry

# File lib/ama-entity-mapper/engine.rb, line 31
def initialize(registry = nil)
  @registry = registry || Type::Registry.new
  @resolver = Type::Resolver.new(@registry)
  @normalizer = RecursiveNormalizer.new(@registry)
  @mapper = RecursiveMapper.new(@registry)
end

Public Instance Methods

[](klass) click to toggle source

@param [Class, Module] klass

# File lib/ama-entity-mapper/engine.rb, line 66
def [](klass)
  @registry[klass]
end
map(source, *types, **context_options) click to toggle source

@param [Object] source @param [Array<AMA::Entity::Mapper::Type>] types @param [Hash] context_options

# File lib/ama-entity-mapper/engine.rb, line 41
def map(source, *types, **context_options)
  context = create_context(context_options)
  types = normalize_types(types, context)
  @mapper.map(source, types, context)
end
normalize(object, **context_options) click to toggle source

Normalizes object to primitive data structures. @param [Object] object @param [Hash] context_options

# File lib/ama-entity-mapper/engine.rb, line 56
def normalize(object, **context_options)
  @normalizer.normalize(object, create_context(context_options))
end
register(klass) click to toggle source

@param [Class, Module] klass

# File lib/ama-entity-mapper/engine.rb, line 61
def register(klass)
  @registry[klass] || @registry.register(Type::Analyzer.analyze(klass))
end
resolve(definition) click to toggle source

Resolves provided definition, creating type hierarchy. @param [Array<Class, Module, Type, Array>] definition

# File lib/ama-entity-mapper/engine.rb, line 49
def resolve(definition)
  @resolver.resolve(definition)
end

Private Instance Methods

create_context(options) click to toggle source

@param [Hash] options @return [AMA::Entity::Mapper::Engine::Context]

# File lib/ama-entity-mapper/engine.rb, line 74
def create_context(options)
  options = options.merge(path: Path.new)
  Mapper::Context.new(**options)
end
normalize_types(types, context) click to toggle source

@param [Array<AMA::Entity::Mapper::Type>] types

# File lib/ama-entity-mapper/engine.rb, line 80
def normalize_types(types, context)
  compliance_error('Called #map() with no types') if types.empty?
  types = types.map { |type| @resolver.resolve(type) }
  types.each { |type| type.resolved!(context) }
end