diff options
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 1 | ||||
-rw-r--r-- | lib/psych/visitors/yaml_tree.rb | 10 | ||||
-rw-r--r-- | test/yaml/test_omap.rb | 7 |
3 files changed, 17 insertions, 1 deletions
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index 15e7d06..aed56ed 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -72,6 +72,7 @@ module Psych case o.tag when '!omap', 'tag:yaml.org,2002:omap' map = Psych::Omap.new + @st[o.anchor] = map if o.anchor o.children.each { |a| map[accept(a.children.first)] = accept a.children.last } diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb index 60d54ad..995a890 100644 --- a/lib/psych/visitors/yaml_tree.rb +++ b/lib/psych/visitors/yaml_tree.rb @@ -40,7 +40,15 @@ module Psych end def visit_Psych_Omap o - @stack.push append Nodes::Sequence.new(nil, '!omap', false) + if node = @st[o.object_id] + node.anchor = o.object_id.to_s + return append Nodes::Alias.new o.object_id.to_s + end + + seq = Nodes::Sequence.new(nil, '!omap', false) + @st[o.object_id] = seq + + @stack.push append seq o.each do |k,v| accept k => v end diff --git a/test/yaml/test_omap.rb b/test/yaml/test_omap.rb index d3d7fde..8186f69 100644 --- a/test/yaml/test_omap.rb +++ b/test/yaml/test_omap.rb @@ -2,6 +2,13 @@ require 'helper' module YAML class TestOmap < Test::Unit::TestCase + def test_self_referential + map = YAML::Omap.new + map['foo'] = 'bar' + map['self'] = map + assert_equal(map, YAML.load(YAML.dump(map))) + end + def test_keys map = YAML::Omap.new map['foo'] = 'bar' |