diff options
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 10 | ||||
-rw-r--r-- | test/visitors/test_to_ruby.rb | 10 |
2 files changed, 20 insertions, 0 deletions
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index 27b7778..8848c16 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -3,7 +3,13 @@ module Psych ### # This class walks a YAML AST, converting each node to ruby class ToRuby < Psych::Visitors::Visitor + def initialize + super + @st = {} + end + visitor_for(Nodes::Scalar) do |o| + @st[o.anchor] = o.value if o.anchor o.value end @@ -22,6 +28,10 @@ module Psych visitor_for(Nodes::Stream) do |o| o.children.map { |c| c.accept self } end + + visitor_for(Nodes::Alias) do |o| + @st[o.anchor] + end end end end diff --git a/test/visitors/test_to_ruby.rb b/test/visitors/test_to_ruby.rb index c7caf47..cc499b7 100644 --- a/test/visitors/test_to_ruby.rb +++ b/test/visitors/test_to_ruby.rb @@ -48,6 +48,16 @@ module Psych assert_equal %w{ foo bar }, stream.to_ruby end + + def test_alias + seq = Nodes::Sequence.new + seq.children << Nodes::Scalar.new('foo', 'A') + seq.children << Nodes::Alias.new('A') + + list = seq.to_ruby + assert_equal %w{ foo foo }, list + assert_equal list[0].object_id, list[1].object_id + end end end end |