Completed
Push — dev ( 36eb97...6cb2fc )
by Fike
01:01
created

Normalizer.normalize()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
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 'interface'
6
7
module AMA
8
  module Entity
9
    class Mapper
10
      module API
11
        # This interface depicts class normalizer - processor, responsible
12
        # for converting entity into low-level primitives
13
        class Normalizer < Interface
14
          # :nocov:
15
          # This method takes in provided entity and it's specific type and
16
          # should return low-level representation of this entity (most
17
          # commonly, hash of attributes). This normalizer has an option to
18
          # fall back on default normalization process (using `block.call()`
19
          # with same signature). Such feature allows to use normalizer as
20
          # pre- or post-processor, either altering incoming entity or polishing
21
          # result:
22
          #
23
          # ```ruby
24
          # data = block.call(entity, type, context)
25
          # data[:parent] = entity.parent.id # replacing parent with it's id
26
          # data
27
          # ```
28
          #
29
          # It is implied that normalizers *work only on single level*, which
30
          # means that they should not process underlying attributes. In the
31
          # example above, that would mean that without additional processing,
32
          # `data[:parent]` would have parent entity unchanged.
33
          #
34
          # @param [Object] entity
35
          # @param [AMA::Entity::Mapper::Type::Concrete] type
36
          # @param [AMA::Entity::Mapper::Context] context
37
          # @param [Proc] block
38
          # @return [Object]
39
          def normalize(entity, type, context = nil, &block)
40
            abstract_method
41
          end
42
          # :nocov:
43
        end
44
      end
45
    end
46
  end
47
end
48