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

PrimitiveType.initialize()   A

Complexity

Conditions 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
1
# frozen_string_literal: true
2
3
require_relative '../../type'
4
require_relative '../../mixin/errors'
5
require_relative 'primitive_type/denormalizer'
6
7
module AMA
8
  module Entity
9
    class Mapper
10
      class Type
11
        module BuiltIn
12
          # Predefined type for Set class
13
          class PrimitiveType < Type
14
            include Mixin::Errors
15
16
            def initialize(type, method_map)
17
              super(type)
18
              this = self
19
20
              factory_block do |*|
21
                this.compliance_error("#{this} factory should never be called")
22
              end
23
              normalizer_block do |entity, *|
24
                entity
25
              end
26
              self.denormalizer = Denormalizer.new(method_map)
27
              enumerator_block do |*|
28
                Enumerator.new { |*| }
29
              end
30
              injector_block { |*| }
31
            end
32
33
            # This hash describes which helper methods may be used for which
34
            # type to extract target primitive. During the run inheritance chain
35
            # is unwrapped, and first matching entry (topmost one) is used.
36
            primitives = {
37
              Symbol => { Object => [], String => %i[to_sym] },
38
              String => { Object => [], Symbol => %i[to_s] },
39
              Numeric => { Object => %i[to_i to_f], String => [] },
40
              Integer => { Object => %i[to_i], String => [] },
41
              Float => { Object => %i[to_f], String => [] },
42
              TrueClass => { Object => %i[to_b to_bool], String => [] },
43
              FalseClass => { Object => %i[to_b to_bool], String => [] },
44
              NilClass => { Object => [] }
45
            }
46
47
            # rubocop:disable Lint/UnifiedInteger
48
            if defined?(Fixnum)
49
              primitives[Fixnum] = { Object => %i[to_i], String => [] }
50
            end
51
            # rubocop:enable Lint/UnifiedInteger
52
53
            ALL = primitives.map do |klass, method_map|
54
              const_set(klass.to_s.upcase, new(klass, method_map))
55
            end
56
          end
57
        end
58
      end
59
    end
60
  end
61
end
62