diff options
-rw-r--r-- | lib/psych/handler.rb | 2 | ||||
-rw-r--r-- | lib/psych/nodes/document.rb | 9 | ||||
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 4 | ||||
-rw-r--r-- | test/visitors/test_to_ruby.rb | 6 |
4 files changed, 19 insertions, 2 deletions
diff --git a/lib/psych/handler.rb b/lib/psych/handler.rb index 84ce4c2..f67a554 100644 --- a/lib/psych/handler.rb +++ b/lib/psych/handler.rb @@ -10,7 +10,7 @@ module Psych ### # Called when the document starts with the declared +version+, # +tag_directives+, if the document is +implicit+ - def start_document version = [], tag_directives = [], implicit = true + def start_document version, tag_directives, implicit end ### diff --git a/lib/psych/nodes/document.rb b/lib/psych/nodes/document.rb index 05eca82..0bd58b2 100644 --- a/lib/psych/nodes/document.rb +++ b/lib/psych/nodes/document.rb @@ -10,12 +10,19 @@ module Psych # Was this document implicitly created? attr_accessor :implicit - def initialize version, tag_directives, implicit + def initialize version = [], tag_directives = [], implicit = true super() @version = version @tag_directives = tag_directives @implicit = implicit end + + ### + # Returns the root node. A Document may only have one root node: + # http://yaml.org/spec/1.1/#id898031 + def root + children.first + end end end end diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index b82fc3c..a4e6152 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -14,6 +14,10 @@ module Psych visitor_for(Nodes::Mapping) do |o| Hash[*o.children.map { |c| c.accept self }] end + + visitor_for(Nodes::Document) do |o| + o.root.accept self + end end end end diff --git a/test/visitors/test_to_ruby.rb b/test/visitors/test_to_ruby.rb index db748ca..c295caf 100644 --- a/test/visitors/test_to_ruby.rb +++ b/test/visitors/test_to_ruby.rb @@ -28,6 +28,12 @@ module Psych mapping.children << Nodes::Scalar.new('bar') assert_equal({'foo' => 'bar'}, mapping.to_ruby) end + + def test_document + doc = Nodes::Document.new + doc.children << Nodes::Scalar.new('foo') + assert_equal 'foo', doc.to_ruby + end end end end |