summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2011-06-08 17:06:46 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2011-06-08 18:58:57 -0700
commite28d3dbca422250d291e98d39d9c4ef2317e3b8c (patch)
treeb6d40bf3220201dff4d935027b51a2243f2179f6
parent2040a6df75d7c598572c823d002774dc08e189db (diff)
downloadpsych-e28d3dbca422250d291e98d39d9c4ef2317e3b8c.zip
ruby classes can round trip
-rw-r--r--lib/psych/visitors/to_ruby.rb2
-rw-r--r--lib/psych/visitors/yaml_tree.rb3
-rw-r--r--test/psych/test_class.rb12
3 files changed, 11 insertions, 6 deletions
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb
index f8b1585..cb0afd2 100644
--- a/lib/psych/visitors/to_ruby.rb
+++ b/lib/psych/visitors/to_ruby.rb
@@ -57,6 +57,8 @@ module Psych
Complex(o.value)
when "!ruby/object:Rational"
Rational(o.value)
+ when "!ruby/class"
+ resolve_class o.value
when "tag:yaml.org,2002:float", "!float"
Float(@ss.tokenize(o.value))
when "!ruby/regexp"
diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb
index 6f90377..de35c76 100644
--- a/lib/psych/visitors/yaml_tree.rb
+++ b/lib/psych/visitors/yaml_tree.rb
@@ -247,7 +247,8 @@ module Psych
end
def visit_Class o
- raise TypeError, "can't dump anonymous class #{o.class}"
+ raise TypeError, "can't dump anonymous class: #{o}" unless o.name
+ @emitter.scalar o.name, nil, '!ruby/class', false, false, Nodes::Scalar::SINGLE_QUOTED
end
def visit_Range o
diff --git a/test/psych/test_class.rb b/test/psych/test_class.rb
index cba88ba..8fd4ad0 100644
--- a/test/psych/test_class.rb
+++ b/test/psych/test_class.rb
@@ -2,16 +2,18 @@ require 'psych/helper'
module Psych
class TestClass < TestCase
- def test_cycle
+ def test_cycle_anonymous_class
assert_raises(::TypeError) do
- assert_cycle(TestClass)
+ assert_cycle(Class.new)
end
end
+ def test_cycle
+ assert_cycle(TestClass)
+ end
+
def test_dump
- assert_raises(::TypeError) do
- Psych.dump TestClass
- end
+ Psych.dump TestClass
end
end
end