summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2009-09-29 20:07:27 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2009-09-29 20:07:27 -0700
commit1bb355c1935530f9d9a797c53708b0ac5d1898fa (patch)
tree3089d7c78a2053351f8bc9c60c47ba90e72002c5
parentf7a4949ef5ba75348843ecc0564f284675c7be2e (diff)
downloadpsych-1bb355c1935530f9d9a797c53708b0ac5d1898fa.zip
hash round trip
-rw-r--r--lib/psych/ruby.rb2
-rw-r--r--lib/psych/visitors/yast_builder.rb15
-rw-r--r--test/visitors/test_yast_builder.rb10
3 files changed, 25 insertions, 2 deletions
diff --git a/lib/psych/ruby.rb b/lib/psych/ruby.rb
index 896c185..6802391 100644
--- a/lib/psych/ruby.rb
+++ b/lib/psych/ruby.rb
@@ -1,4 +1,4 @@
-[Object, String, Class].each do |klass|
+[Object, String, Class, Hash].each do |klass|
klass.send(:remove_method, :to_yaml)
end
diff --git a/lib/psych/visitors/yast_builder.rb b/lib/psych/visitors/yast_builder.rb
index df6d390..00f533f 100644
--- a/lib/psych/visitors/yast_builder.rb
+++ b/lib/psych/visitors/yast_builder.rb
@@ -17,7 +17,7 @@ module Psych
return send(method_name, target)
end
end
- raise "Can't handle #{target.class}"
+ raise TypeError, "Can't dump #{target.class}"
end
visitor_for(::String) do |o|
@@ -27,6 +27,19 @@ module Psych
visitor_for(::Class) do |o|
raise TypeError, "can't dump anonymous class #{o.class}"
end
+
+ visitor_for(::Hash) do |o|
+ map = Nodes::Mapping.new
+ @stack.last.children << map
+ @stack.push map
+
+ o.each do |k,v|
+ k.accept self
+ v.accept self
+ end
+
+ @stack.pop
+ end
end
end
end
diff --git a/test/visitors/test_yast_builder.rb b/test/visitors/test_yast_builder.rb
index e73a57b..027764a 100644
--- a/test/visitors/test_yast_builder.rb
+++ b/test/visitors/test_yast_builder.rb
@@ -28,6 +28,16 @@ module Psych
Class.new.to_yaml
end
end
+
+ def test_hash
+ assert_round_trip('a' => 'b')
+ end
+
+ def assert_round_trip obj
+ @v.accept(obj)
+ assert_equal(obj, Psych.load(@v.tree.to_yaml))
+ assert_equal(obj, Psych.load(obj.to_yaml))
+ end
end
end
end