1
|
|
|
# frozen_string_literal: true |
2
|
|
|
|
3
|
|
|
require_relative '../injector' |
4
|
|
|
require_relative '../default/injector' |
5
|
|
|
require_relative '../../context' |
6
|
|
|
require_relative '../../mixin/errors' |
7
|
|
|
|
8
|
|
|
module AMA |
9
|
|
|
module Entity |
10
|
|
|
class Mapper |
11
|
|
|
module API |
12
|
|
|
module Wrapper |
13
|
|
|
# Default attribute injector |
14
|
|
|
class Injector < API::Injector |
15
|
|
|
include Mixin::Errors |
16
|
|
|
|
17
|
|
|
# @param [AMA::Entity::Mapper::API::Injector] processor |
18
|
|
|
def initialize(processor) |
19
|
|
|
@processor = processor |
20
|
|
|
@fallback = Default::Injector::INSTANCE |
21
|
|
|
end |
22
|
|
|
|
23
|
|
|
# @param [Object] entity |
24
|
|
|
# @param [AMA::Entity::Mapper::Type] type |
25
|
|
|
# @param [AMA::Entity::Mapper::Type::Attribute] attribute |
26
|
|
|
# @param [Object] value |
27
|
|
|
# @param [AMA::Entity::Mapper::Context] context |
28
|
|
|
def inject(entity, type, attribute, value, context = nil) |
29
|
|
|
ctx = context || Context.new |
30
|
|
|
attr = attribute |
31
|
|
|
val = value |
32
|
|
|
@processor.inject(entity, type, attr, val, ctx) do |e, t, a, v, c| |
33
|
|
|
@fallback.inject(e, t, a, v, c) |
34
|
|
|
end |
35
|
|
|
rescue StandardError => e |
36
|
|
|
raise_if_internal(e) |
37
|
|
|
message = "Error while injecting #{attr} into #{entity} " \ |
38
|
|
|
"(type: #{type}) using #{@processor}" |
39
|
|
|
if e.is_a?(ArgumentError) |
40
|
|
|
message += "Does #{@processor}#inject have signature " \ |
41
|
|
|
'(entity, type, attribute, value, context = nil)?' |
42
|
|
|
end |
43
|
|
|
mapping_error(message, parent: e, context: ctx) |
44
|
|
|
end |
45
|
|
|
end |
46
|
|
|
end |
47
|
|
|
end |
48
|
|
|
end |
49
|
|
|
end |
50
|
|
|
end |
51
|
|
|
|