Class: AMA::Entity::Mapper::Engine::RecursiveMapper

Inherits:
Object
  • Object
show all
Includes:
Mixin::Errors, Mixin::SuppressionSupport
Defined in:
lib/ama-entity-mapper/engine/recursive_mapper.rb

Overview

Recursively maps object to one of specified types

Instance Method Summary collapse

Methods included from Mixin::Errors

#compliance_error, #mapping_error, #raise_if_internal, #validation_error

Methods included from Mixin::SuppressionSupport

#successful

Constructor Details

#initialize(registry) ⇒ RecursiveMapper

Returns a new instance of RecursiveMapper.

Parameters:



19
20
21
# File 'lib/ama-entity-mapper/engine/recursive_mapper.rb', line 19

def initialize(registry)
  @registry = registry
end

Instance Method Details

#map(source, types, context) ⇒ Object

Parameters:



26
27
28
29
30
31
32
33
# File 'lib/ama-entity-mapper/engine/recursive_mapper.rb', line 26

def map(source, types, context)
  map_unsafe(source, types, context)
rescue StandardError => e
  message = "Failed to map #{source.class} " \
    "to any of provided types (#{types.map(&:to_def).join(', ')}). " \
    "Last error: #{e.message} in #{e.backtrace_locations[0]}"
  mapping_error(message)
end

#map_type(source, type, ctx) ⇒ Object

Parameters:

Returns:

  • (Object)


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ama-entity-mapper/engine/recursive_mapper.rb', line 39

def map_type(source, type, ctx)
  ctx.logger.debug("Mapping #{source.class} to type #{type.to_def}")
  source, reassembled = request_reassembly(source, type, ctx)
  epithet = reassembled ? 'reassembled' : 'source'
  if type.attributes.empty?
    message = "#{type.to_def} has no attributes, " \
      "returning #{epithet} instance"
    ctx.logger.debug(message)
    return source
  end
  process_attributes(source, type, ctx)
end