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

Type.validate!   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
1
# frozen_string_literal: true
2
3
# rubocop:disable Lint/UnusedMethodArgument
4
5
require_relative 'mixin/errors'
6
require_relative 'context'
7
8
module AMA
9
  module Entity
10
    class Mapper
11
      # Base abstract class for all other types
12
      class Type
13
        include Mixin::Errors
14
15
        # :nocov:
16
        def initialize
17
          message = "#{self.class} is an abstract class " \
18
            'and can\'t be isntantiated directly'
19
          compliance_error(message, nil)
20
        end
21
        # :nocov:
22
23
        # @return [Hash{Symbol, AMA::Entity::Mapper::Type::Attribute}]
24
        def attributes
25
          {}
26
        end
27
28
        # @return [Hash{Symbol, AMA::Entity::Mapper::Type}]
29
        def parameters
30
          {}
31
        end
32
33
        # @param [Symbol] id
34
        # @return [TrueClass, FalseClass]
35
        def attribute?(id)
36
          attributes.key?(id.to_sym)
37
        end
38
39
        # @param [Symbol] id
40
        # @return [TrueClass, FalseClass]
41
        def parameter?(id)
42
          parameters.key?(id.to_sym)
43
        end
44
45
        # :nocov:
46
        # Creates parameter if it doesn't yet exist and returns it
47
        #
48
        # @param [Symbol] id
49
        def parameter!(id)
50
          abstract_method
51
        end
52
53
        # @param [AMA::Entity::Mapper::Type] parameter
54
        # @param [AMA::Entity::Mapper::Type] substitution
55
        # @return [AMA::Entity::Mapper::Type]
56
        def resolve_parameter(parameter, substitution)
57
          abstract_method
58
        end
59
        # :nocov:
60
61
        # rubocop:disable Metrics/LineLength
62
63
        # @param [Hash<AMA::Entity::Mapper::Type, AMA::Entity::Mapper::Type>] parameters
64
        # @return [AMA::Entity::Mapper::Type]
65
        def resolve(parameters)
66
          parameters.reduce(self) do |carrier, tuple|
67
            carrier.resolve_parameter(*tuple)
68
          end
69
        end
70
71
        # rubocop:enable Metrics/LineLength
72
73
        # @return [TrueClass, FalseClass]
74
        def resolved?
75
          attributes.values.all?(&:resolved?)
76
        end
77
78
        # Validates that type is fully resolved, otherwise raises an error
79
        # @param [AMA::Entity::Mapper::Context] context
80
        def resolved!(context = nil)
81
          context ||= Context.new
82
          unless parameters.empty?
83
            compliance_error("Type #{self} is not resolved", context: context)
84
          end
85
          attributes.values.each do |attribute|
86
            attribute.resolved!(context)
87
          end
88
        end
89
90
        # :nocov:
91
        # @param [Object] object
92
        def instance?(object)
93
          abstract_method
94
        end
95
        # :nocov:
96
97
        # @param [Object] object
98
        # @param [AMA::Entity::Mapper::Context] context
99
        def instance!(object, context = nil)
100
          return if instance?(object)
101
          message = "Expected to receive instance of #{self}, got " \
102
            "#{object.class}"
103
          mapping_error(message, context: context)
104
        end
105
106
        # :nocov:
107
        def satisfied_by?(object)
108
          abstract_method
109
        end
110
111
        def hash
112
          abstract_method
113
        end
114
115
        def eql?(other)
116
          abstract_method
117
        end
118
        # :nocov:
119
120
        def ==(other)
121
          eql?(other)
122
        end
123
124
        # :nocov:
125
        def to_s
126
          abstract_method
127
        end
128
        # :nocov:
129
130
        protected
131
132
        # :nocov:
133
        # rubocop:disable Performance/Caller
134
        def abstract_method
135
          message = "Abstract method #{caller[1]} hasn't been implemented " \
136
            "in class #{self.class}"
137
          raise message
138
        end
139
        # rubocop:enable Performance/Caller
140
        # :nocov:
141
      end
142
    end
143
  end
144
end
145