Created
December 28, 2018 14:43
-
-
Save mtsmfm/5ba5083dd0efd60217471f12ac4344d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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