Skip to content

Instantly share code, notes, and snippets.

@mtsmfm
Created December 28, 2018 14:43
Show Gist options
  • Save mtsmfm/5ba5083dd0efd60217471f12ac4344d5 to your computer and use it in GitHub Desktop.
Save mtsmfm/5ba5083dd0efd60217471f12ac4344d5 to your computer and use it in GitHub Desktop.
class YAMLTreeBuilder < YAML::TreeBuilder
def initialize
super
@path_stack = []
end
def scalar(*)
super.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
def alias(*)
super.tap do
if YAML::Nodes::Mapping === @last
if @last.children.size.odd?
# noop
else
@path_stack.pop
end
end
end
end
%w(end_mapping end_sequence).each do |m|
define_method(m) do |*args|
brothers = @stack[-2].children
if Psych::Nodes::Scalar === brothers[brothers.index(@last) - 1]
@path_stack.pop
end
super(*args)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment