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

Factory.create()   A

Complexity

Conditions 3

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 15
loc 15
rs 9.4285
cc 3
1
# frozen_string_literal: true
2
3
require_relative '../factory'
4
require_relative '../default/factory'
5
require_relative '../../mixin/errors'
6
require_relative '../../context'
7
8
module AMA
9
  module Entity
10 View Code Duplication
    class Mapper
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
11
      module API
12
        module Wrapper
13
          # Factory safety wrapper
14
          class Factory < API::Factory
15
            include Mixin::Errors
16
17
            # @param [AMA::Entity::Mapper::API::Factory] processor
18
            def initialize(processor)
19
              @processor = processor
20
              @fallback = Default::Factory::INSTANCE
21
            end
22
23
            # @param [AMA::Entity::Mapper::Type] type
24
            # @param [Object] data
25
            # @param [AMA::Entity::Mapper::Context] context
26
            def create(type, data = nil, context = nil)
27
              context ||= Context.new
28
              @processor.create(type, data, context) do |t, d, c|
29
                @fallback.create(t, d, c)
30
              end
31
            rescue StandardError => e
32
              raise_if_internal(e)
33
              message = "Error while creatin #{type} instance " \
34
                "(type: #{type}) attributes using #{@processor}"
35
              if e.is_a?(ArgumentError)
36
                message += "Does #{@processor}#create have signature " \
37
                  '(entity, type, context = nil)?'
38
              end
39
              mapping_error(message, parent: e, context: context)
40
            end
41
          end
42
        end
43
      end
44
    end
45
  end
46
end
47