Class: YamlPath::TreeBuilder

Inherits:
YAML::TreeBuilder
  • Object
show all
Defined in:
lib/yaml_path/tree_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(yaml_path:, replace_proc:) ⇒ TreeBuilder

Returns a new instance of TreeBuilder.



23
24
25
26
27
28
29
# File 'lib/yaml_path/tree_builder.rb', line 23

def initialize(yaml_path:, replace_proc:)
  super()

  @yaml_path = yaml_path
  @replace_proc = replace_proc
  @path_stack = PathStack.new
end

Instance Method Details

#aliasObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/yaml_path/tree_builder.rb', line 47

def alias(*)
  super.tap do
    if YAML::Nodes::Mapping === @last
      if @last.children.size.odd?
        # noop
      else
        @path_stack.pop
      end
    end
  end
end

#scalar(value, anchor, tag, plain, quoted, style) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/yaml_path/tree_builder.rb', line 31

def scalar(value, anchor, tag, plain, quoted, style)
  if @path_stack.match?(@yaml_path)
    value = @replace_proc.call(value)
  end

  super(value, anchor, tag, plain, quoted, style).tap do
    if YAML::Nodes::Mapping === @last
      if @last.children.size.odd?
        @path_stack.push(@last.children.last.value)
      else
        @path_stack.pop
      end
    end
  end
end