diff options
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 24 | ||||
-rw-r--r-- | lib/psych/visitors/yast_builder.rb | 3 | ||||
-rw-r--r-- | test/visitors/test_to_ruby.rb | 9 | ||||
-rw-r--r-- | test/visitors/test_yast_builder.rb | 5 |
4 files changed, 27 insertions, 14 deletions
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index 39c9194..fec2614 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -97,23 +97,12 @@ module Psych def visit_Psych_Nodes_Mapping o case o.tag - when /!ruby\/object(:.*)?$/ - name = $1.sub(/^:/, '') - h = Hash[*o.children.map { |c| accept c }] - s = name.split('::').inject(Object) { |k,sub| - k.const_get sub - }.allocate - h.each do |k,v| - s.instance_variable_set(:"@#{k}", v) - end - s - - when /!ruby\/struct(:.*)?$/ + when /!ruby\/struct:?(.*)?$/ klassname = $1 h = Hash[*o.children.map { |c| accept c }].to_a if klassname && klassname.length > 1 - name = klassname.sub(/^:/, '') + name = klassname s = nil retried = false @@ -151,6 +140,15 @@ module Psych h = Hash[*o.children.map { |c| accept c }] Rational(h['numerator'], h['denominator']) + when /!ruby\/object:?(.*)?$/ + name = $1.nil? ? 'Object' : $1 + h = Hash[*o.children.map { |c| accept c }] + s = name.split('::').inject(Object) { |k,sub| + k.const_get sub + }.allocate + h.each { |k,v| s.instance_variable_set(:"@#{k}", v) } + s + else hash = {} @st[o.anchor] = hash if o.anchor diff --git a/lib/psych/visitors/yast_builder.rb b/lib/psych/visitors/yast_builder.rb index 11e3b34..e8f9891 100644 --- a/lib/psych/visitors/yast_builder.rb +++ b/lib/psych/visitors/yast_builder.rb @@ -23,7 +23,8 @@ module Psych end def visit_Object o - tag = ['!ruby/object', o.class.name].join(':') + klass = o.class == Object ? nil : o.class.name + tag = ['!ruby/object', klass].compact.join(':') @stack.push append Nodes::Mapping.new(nil, tag, false) o.instance_variables.each do |iv| accept iv.to_s.sub(/^@/, '') diff --git a/test/visitors/test_to_ruby.rb b/test/visitors/test_to_ruby.rb index f993c49..dcd4738 100644 --- a/test/visitors/test_to_ruby.rb +++ b/test/visitors/test_to_ruby.rb @@ -11,6 +11,15 @@ module Psych @visitor = ToRuby.new end + def test_object + mapping = Nodes::Mapping.new nil, "!ruby/object" + mapping.children << Nodes::Scalar.new('foo') + mapping.children << Nodes::Scalar.new('bar') + + o = mapping.to_ruby + assert_equal 'bar', o.instance_variable_get(:@foo) + end + def test_awesome Psych.load('1900-01-01T00:00:00+00:00') end diff --git a/test/visitors/test_yast_builder.rb b/test/visitors/test_yast_builder.rb index 532ab09..8645a4d 100644 --- a/test/visitors/test_yast_builder.rb +++ b/test/visitors/test_yast_builder.rb @@ -8,6 +8,11 @@ module Psych @v = Visitors::YASTBuilder.new end + def test_object_has_no_class + yaml = Psych.dump(Object.new) + assert(Psych.dump(Object.new) !~ /Object/, yaml) + end + def test_struct_const foo = Struct.new("Foo", :bar) assert_round_trip foo.new('bar') |