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

EnumerableType   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 100 %

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A define_normalizer() 5 5 1
A define_acceptor() 9 9 1
A define_denormalizer() 5 5 1
A define_enumerator() 10 10 1
A initialize() 11 11 1
A define_factory() 7 7 1
A define_extractor() 14 14 1

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 '../concrete'
4
require_relative '../../path/segment'
5
6
module AMA
7
  module Entity
8 View Code Duplication
    class Mapper
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
9
      class Type
10
        module Hardwired
11
          # Default Enumerable handler
12
          class EnumerableType < Concrete
13
            def initialize
14
              super(::Enumerable)
15
              attribute!(:_value, parameter!(:T), virtual: true)
16
17
              define_factory
18
              define_normalizer
19
              define_denormalizer
20
              define_enumerator
21
              define_acceptor
22
              define_extractor
23
            end
24
25
            private
26
27
            def define_factory
28
              self.factory = Object.new.tap do |factory|
29
                factory.define_singleton_method(:create) do |*|
30
                  []
31
                end
32
              end
33
            end
34
35
            def define_normalizer
36
              self.normalizer = lambda do |input, *|
37
                input.map(&:itself)
38
              end
39
            end
40
41
            def define_denormalizer
42
              self.denormalizer = lambda do |entity, *|
43
                entity
44
              end
45
            end
46
47
            def define_enumerator
48
              self.enumerator = lambda do |entity, type, *|
49
                ::Enumerator.new do |yielder|
50
                  attribute = type.attributes[:_value]
51
                  entity.each_with_index do |value, index|
52
                    yielder << [attribute, value, Path::Segment.index(index)]
53
                  end
54
                end
55
              end
56
            end
57
58
            def define_acceptor
59
              self.acceptor = lambda do |entity, *|
60
                Object.new.tap do |acceptor|
61
                  acceptor.define_singleton_method(:accept) do |_, val, segment|
62
                    entity[segment.name] = val
63
                  end
64
                end
65
              end
66
            end
67
68
            def define_extractor
69
              self.extractor = lambda do |object, type, context = nil, *|
70
                unless object.is_a?(::Enumerable)
71
                  message = "Expected enumerable, got #{object.class}"
72
                  mapping_error(message, context: context)
73
                end
74
                ::Enumerator.new do |yielder|
75
                  object.each_with_index do |value, index|
76
                    attribute = type.attributes[:_value]
77
                    yielder << [attribute, value, Path::Segment.index(index)]
78
                  end
79
                end
80
              end
81
            end
82
83
            INSTANCE = new
84
          end
85
        end
86
      end
87
    end
88
  end
89
end
90