diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2009-09-29 20:48:42 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2009-09-29 20:48:42 -0700 |
commit | 5ae604004516bc5944c38eee539b022bd7cf4820 (patch) | |
tree | 465fd16b50eb7df403c97708c347efad7ae7cf52 | |
parent | 8328623efd2889ffcd513454d914385d480cce57 (diff) | |
download | psych-5ae604004516bc5944c38eee539b022bd7cf4820.zip |
round trip nil class
-rw-r--r-- | lib/psych.rb | 16 | ||||
-rw-r--r-- | lib/psych/ruby.rb | 2 | ||||
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 7 | ||||
-rw-r--r-- | lib/psych/visitors/yast_builder.rb | 4 | ||||
-rw-r--r-- | test/visitors/test_yast_builder.rb | 5 |
5 files changed, 31 insertions, 3 deletions
diff --git a/lib/psych.rb b/lib/psych.rb index 3ebc1fa..2d0cf0c 100644 --- a/lib/psych.rb +++ b/lib/psych.rb @@ -26,9 +26,23 @@ module Psych parse(yaml).to_ruby end + ### + # Parse a YAML string. Returns the first object of a YAML parse tree def self.parse yaml + yaml_ast(yaml).children.first.children.first + end + + ### + # Parse a YAML string in +yaml+. Returns the AST for the YAML parse tree. + def self.yaml_ast yaml parser = Psych::Parser.new(TreeBuilder.new) parser.parse yaml - parser.handler.root.children.first.children.first + parser.handler.root + end + + ### + # Dump object +o+ to a YAML string + def self.dump o + o.to_yaml end end diff --git a/lib/psych/ruby.rb b/lib/psych/ruby.rb index f63fee1..ae007c4 100644 --- a/lib/psych/ruby.rb +++ b/lib/psych/ruby.rb @@ -1,4 +1,4 @@ -[Object, String, Class, Hash, Array].each do |klass| +[Object, String, Class, Hash, Array, NilClass].each do |klass| klass.send(:remove_method, :to_yaml) end diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index 8848c16..a37c294 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -10,7 +10,12 @@ module Psych visitor_for(Nodes::Scalar) do |o| @st[o.anchor] = o.value if o.anchor - o.value + case o.tag + when 'tag:yaml.org,2002:null' + nil + else + o.value + end end visitor_for(Nodes::Sequence) do |o| diff --git a/lib/psych/visitors/yast_builder.rb b/lib/psych/visitors/yast_builder.rb index 620a2bb..8508601 100644 --- a/lib/psych/visitors/yast_builder.rb +++ b/lib/psych/visitors/yast_builder.rb @@ -45,6 +45,10 @@ module Psych @stack.pop end + def visit_NilClass o + append Nodes::Scalar.new('', nil, 'tag:yaml.org,2002:null', false) + end + private def append o @stack.last.children << o diff --git a/test/visitors/test_yast_builder.rb b/test/visitors/test_yast_builder.rb index 3979566..36feb21 100644 --- a/test/visitors/test_yast_builder.rb +++ b/test/visitors/test_yast_builder.rb @@ -37,10 +37,15 @@ module Psych assert_round_trip(%w{ a b }) end + def test_nil + assert_round_trip nil + 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)) + assert_equal(obj, Psych.load(Psych.dump(obj))) end end end |