Class: Kumi::Core::Compiler::Accessors::ReadAccessor

Inherits:
Object
  • Object
show all
Extended by:
Base
Defined in:
lib/kumi/core/compiler/accessors/read_accessor.rb

Constant Summary

Constants included from Base

Base::MISSING

Class Method Summary collapse

Methods included from Base

assert_array!, assert_hash!, fetch_key, missing_array_action, missing_key_action, next_enters_array?, warn_mismatch

Class Method Details

.build(operations, path_key, policy, key_policy) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kumi/core/compiler/accessors/read_accessor.rb', line 10

def self.build(operations, path_key, policy, key_policy)
  mode = :read
  lambda do |data|
    node = data
    operations.each do |op|
      case op[:type]
      when :enter_hash
        assert_hash!(node, path_key, mode)
        next_node = fetch_key(node, op[:key], key_policy)
        if next_node == Base::MISSING
          case missing_key_action(policy)
          when :yield_nil then return nil
          when :skip      then return nil
          when :raise     then raise KeyError, "Missing key '#{op[:key]}' at '#{path_key}' (#{mode})"
          end
        end
        node = next_node
      when :enter_array
        # Should never be present for rank-0 plans
        raise TypeError, "Array encountered in :read accessor at '#{path_key}'"
      else
        raise "Unknown operation: #{op.inspect}"
      end
    end
    node
  end
end