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

Engine

Complexity

Total Complexity 0

Size/Duplication

Total Lines 36
Duplicated Lines 25 %

Importance

Changes 11
Bugs 0 Features 0
Metric Value
wmc 0
c 11
b 0
f 0
dl 9
loc 36

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Denormalizer.denormalize() 0 8 2
A Denormalizer.denormalize_internal() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# frozen_string_literal: true
2
3
require_relative '../mixin/errors'
4
require_relative '../context'
5
6
module AMA
7
  module Entity
8
    class Mapper
9
      class Engine
10
        # Thin denormalization master. Delegates processing to type
11
        # denormalizer and adds security wrap.
12
        class Denormalizer
13
          include ::AMA::Entity::Mapper::Mixin::Errors
14
15
          # @param [Object] source
16
          # @param [AMA::Entity::Mapper::Type] target_type
17
          # @param [AMA::Entity::Mapper::Context] context
18
          # @return [Object] Object of target type
19
          def denormalize(source, target_type, context)
20
            result = denormalize_internal(source, target_type, context)
21
            return result if target_type.instance?(result)
22
            message = "Denormalizer for type #{target_type} has returned " \
23
              "something that is not an instance of #{target_type}: " \
24
              "#{result} (#{result.class})"
25
            compliance_error(message, context: context)
26
          end
27
28
          private
29
30
          # @param [Object] source
31
          # @param [AMA::Entity::Mapper::Type] target_type
32
          # @param [AMA::Entity::Mapper::Context] context
33
          # @return [Object] Object of target type
34 View Code Duplication
          def denormalize_internal(source, target_type, context)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
35
            denormalizer = target_type.denormalizer
36
            denormalizer.denormalize(source, target_type, context)
37
          rescue StandardError => e
38
            raise_if_internal(e)
39
            message = "Error while denormalizing #{target_type} " \
40
              "out of #{source.class}"
41
            mapping_error(message, parent: e, context: context)
42
          end
43
        end
44
      end
45
    end
46
  end
47
end
48