Completed
Push — dev ( ffd8f7...ec8098 )
by Fike
51s
created

Object   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
dl 0
loc 31
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 3 1
A denormalize() 0 12 3
A compute_handler() 0 4 3
1
# frozen_string_literal: true
2
3
require_relative '../../exception/mapping_error'
4
require_relative '../../../../lib/mapper/mixin/reflection'
5
6
module AMA
7
  module Entity
8
    class Mapper
9
      class Engine
10
        class Denormalizer
11
          # Denormalizer for non-standard objects not registered as entities
12
          class Object
13
            include ::AMA::Entity::Mapper::Mixin::Reflection
14
            include ::AMA::Entity::Mapper::Mixin::Errors
15
16
            def supports(*)
17
              true
18
            end
19
20
            # @param [Object] source
21
            # @param [AMA::Entity::Mapper::Engine::Context] context
22
            # @param [AMA::Entity::Mapper::Type::Concrete] target_type
23
            def denormalize(source, context, target_type)
24
              return source if source.is_a?(target_type.type)
25
              handler = compute_handler(target_type, context)
26
              return target_type.type.send(handler, source, context) if handler
27
              entity = target_type.factory.create(context, source)
28
              extractor = target_type.extractor(source)
29
              acceptor = target_type.acceptor(entity, context)
30
              extractor.each do |attribute, value, segment = nil|
31
                acceptor.accept(attribute, value, segment)
32
              end
33
              entity
34
            end
35
36
            private
37
38
            def compute_handler(target_type, context)
39
              handler = context.denormalization_method
40
              handler && target_type.type.respond_to?(handler) ? handler : nil
41
            end
42
          end
43
        end
44
      end
45
    end
46
  end
47
end
48