Module: Kumi::Core::Compiler::AccessEmit::Materialize

Extended by:
Base
Defined in:
lib/kumi/core/compiler/access_emit/materialize.rb

Class Method Summary collapse

Methods included from Base

array_guard_code, build_array_miss_action, build_miss_action, fetch_hash_code, segment_ops

Class Method Details

.build(plan) ⇒ Object



9
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kumi/core/compiler/access_emit/materialize.rb', line 9

def build(plan)
  policy     = plan.on_missing || :error
  key_policy = plan.key_policy || :indifferent
  path_key   = plan.path
  segs       = segment_ops(plan.operations)

  code = +"lambda do |data|\n"
  nodev = "node0"
  depth = 0
  map_depth = 0
  code << "  #{nodev} = data\n"

  segs.each do |seg|
    if seg == :array
      code << "  #{array_guard_code(node_var: nodev, mode: :materialize, policy: policy, path_key: path_key, map_depth: map_depth)}\n"
      child = "node#{depth + 1}"
      code << "  #{nodev} = #{nodev}.map do |__e#{depth}|\n"
      code << "    #{child} = __e#{depth}\n"
      nodev = child
      depth += 1
      map_depth += 1
    else
      seg.each do |(_, key, preview)|
        code << "  "
        code << fetch_hash_code(node_var: nodev, key: key, key_policy: key_policy,
                                preview_array: preview, mode: :materialize, policy: policy,
                                path_key: path_key, map_depth: map_depth)
        code << "\n"
      end
    end
  end

  while map_depth.positive?
    code << (("  " * map_depth) + "#{nodev}\n")
    code << (("  " * (map_depth - 1)) + "end\n")
    nodev = "node#{depth - 1}"
    depth -= 1
    map_depth -= 1
  end
  code << "  #{nodev}\nend\n"
  code
end