Completed
Push — master ( 308a2c...1aebdd )
by Fike
01:00
created

DateTimeType   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 100 %

Importance

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

3 Methods

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