From cbf2a14ec5fc5b6fe0b576c2df13cf5e29cefbb3 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 28 Sep 2009 15:44:37 -0700 Subject: aliases starting to work --- lib/psych/visitors/to_ruby.rb | 10 ++++++++++ test/visitors/test_to_ruby.rb | 10 ++++++++++ 2 files changed, 20 insertions(+) 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 -- cgit v1.2.3