Completed
Push — dev ( 36eb97...6cb2fc )
by Fike
01:01
created

Injector   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
dl 0
loc 32
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 4 1
A inject() 0 17 3
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