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

Enumerator.enumerate()   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
1
# frozen_string_literal: true
2
3
require_relative '../injector'
4
require_relative '../../context'
5
require_relative '../../mixin/reflection'
6
require_relative '../../path/segment'
7
8
module AMA
9
  module Entity
10
    class Mapper
11
      module API
12
        module Default
13
          # Default attribute enumerator
14
          class Enumerator < API::Injector
15
            include Mixin::Reflection
16
17
            INSTANCE = new
18
19
            # @param [Object] entity
20
            # @param [AMA::Entity::Mapper::Type] type
21
            # @param [AMA::Entity::Mapper::Context] _context
22
            def enumerate(entity, type, _context = nil)
23
              ::Enumerator.new do |yielder|
24
                type.attributes.values.reject(&:virtual).each do |attribute|
25
                  next unless object_variable_exists(entity, attribute.name)
26
                  value = object_variable(entity, attribute.name)
27
                  segment = Path::Segment.attribute(attribute.name)
28
                  yielder << [attribute, value, segment]
29
                end
30
              end
31
            end
32
          end
33
        end
34
      end
35
    end
36
  end
37
end
38