diff options
-rw-r--r-- | lib/psych/visitors/yaml_tree.rb | 42 | ||||
-rw-r--r-- | test/yaml/test_boolean.rb | 2 |
2 files changed, 18 insertions, 26 deletions
diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb index e4e6947..3e67473 100644 --- a/lib/psych/visitors/yaml_tree.rb +++ b/lib/psych/visitors/yaml_tree.rb @@ -12,20 +12,17 @@ module Psych end def accept target + # return any aliases we find + if node = @st[target.object_id] + node.anchor = target.object_id.to_s + return append Nodes::Alias.new target.object_id.to_s + end + target.class.ancestors.each do |klass| next unless klass.name method_name = :"visit_#{klass.name.split('::').join('_')}" - if respond_to?(method_name) - - # return any aliases we find - if node = @st[target.object_id] - node.anchor = target.object_id.to_s - return append Nodes::Alias.new target.object_id.to_s - end - - return send(method_name, target) - end + return send(method_name, target) if respond_to?(method_name) end raise TypeError, "Can't dump #{target.class}" @@ -59,7 +56,10 @@ module Psych def visit_Object o klass = o.class == Object ? nil : o.class.name tag = ['!ruby/object', klass].compact.join(':') - @stack.push append Nodes::Mapping.new(nil, tag, false) + + mapping = Nodes::Mapping.new(nil, tag, false) + @stack.push append mapping + if o.respond_to? :to_yaml_properties ivars = o.to_yaml_properties else @@ -67,7 +67,7 @@ module Psych end ivars.each do |iv| - accept iv.to_s.sub(/^@/, '') + mapping.children << Nodes::Scalar.new(":#{iv.to_s.sub(/^@/, '')}") accept o.instance_variable_get(iv) end @stack.pop @@ -92,7 +92,7 @@ module Psych end ivars.each do |iv| - accept iv.to_s.sub(/^@/, '') + map.children << Nodes::Scalar.new(":#{iv.to_s.sub(/^@/, '')}") accept o.instance_variable_get(iv) end @stack.pop @@ -100,7 +100,9 @@ module Psych def visit_Exception o tag = ['!ruby/exception', o.class.name].join ':' - @stack.push append Nodes::Mapping.new(nil, tag, false) + + mapping = Nodes::Mapping.new(nil, tag, false) + @stack.push append mapping { 'message' => private_iv_get(o, 'mesg'), @@ -118,7 +120,7 @@ module Psych end ivars.each do |iv| - accept iv.to_s.sub(/^@/, '') + mapping.children << Nodes::Scalar.new(":#{iv.to_s.sub(/^@/, '')}") accept o.instance_variable_get(iv) end @@ -235,11 +237,6 @@ module Psych end def visit_Hash o - if node = @st[o.object_id] - node.anchor = o.object_id.to_s - return append Nodes::Alias.new o.object_id.to_s - end - map = Nodes::Mapping.new @st[o.object_id] = map @@ -254,11 +251,6 @@ module Psych end def visit_Array o - 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 @st[o.object_id] = seq diff --git a/test/yaml/test_boolean.rb b/test/yaml/test_boolean.rb index 7bc474e..8113348 100644 --- a/test/yaml/test_boolean.rb +++ b/test/yaml/test_boolean.rb @@ -28,7 +28,7 @@ module YAML ### # YAML spec says "n" and "N" may be used as false, but Syck treats them # as literal strings - def test_y + def test_n assert_equal "n", YAML.load("--- n") assert_equal "N", YAML.load("--- N") end |