Completed
Push — dev ( ff1c91...ab1075 )
by Fike
01:06
created

RationalType   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
dl 43
loc 43
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A define_factory() 8 8 1
A initialize() 16 16 1
A define_denormalizer() 11 11 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 '../../type'
4
require_relative '../../mixin/errors'
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 BuiltIn
11
          # Rational type description
12
          class RationalType < Type
13
            def initialize
14
              super(Rational)
15
16
              normalizer_block do |entity, *|
17
                entity.to_s
18
              end
19
20
              define_denormalizer
21
              define_factory
22
23
              enumerator_block do |*|
24
                ::Enumerator.new { |*| }
25
              end
26
27
              injector_block { |*| }
28
            end
29
30
            private
31
32
            def define_denormalizer
33
              denormalizer_block do |input, _, ctx|
34
                break input if input.is_a?(Rational)
35
                input = input.to_s if input.is_a?(Symbol)
36
                break Rational(input) if input.is_a?(String)
37
                singleton_class.send(:include, Mixin::Errors)
38
                message = "String input expected (like '2.3'), " \
39
                  "#{input.class} received: #{input}"
40
                mapping_error(message, context: ctx)
41
              end
42
            end
43
44
            def define_factory
45
              factory_block do |_, _, ctx|
46
                singleton_class.send(:include, Mixin::Errors)
47
                message = 'Rational type could not be instantiated directly, ' \
48
                  'it only supports normalization and denormalization'
49
                compliance_error(message, context: ctx)
50
              end
51
            end
52
53
            INSTANCE = new
54
          end
55
        end
56
      end
57
    end
58
  end
59
end
60