diff options
-rw-r--r-- | lib/psych/visitors/yaml_tree.rb | 15 | ||||
-rw-r--r-- | test/psych/test_psych.rb | 20 |
2 files changed, 34 insertions, 1 deletions
diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb index 5f757e9..8a12086 100644 --- a/lib/psych/visitors/yaml_tree.rb +++ b/lib/psych/visitors/yaml_tree.rb @@ -13,6 +13,7 @@ module Psych @emitter = emitter @st = {} @ss = ScalarScanner.new + @options = options @dispatch_cache = Hash.new do |h,klass| method = "visit_#{(klass.name || '').split('::').join('_')}" @@ -43,7 +44,19 @@ module Psych def push object start unless started? - @emitter.start_document [], [], false + version = [] + version = [1,1] if @options[:header] + + case @options[:version] + when Array + version = @options[:version] + when String + version = @options[:version].split('.').map { |x| x.to_i } + else + version = [1,1] + end if @options[:version] + + @emitter.start_document version, [], false accept object @emitter.end_document end diff --git a/test/psych/test_psych.rb b/test/psych/test_psych.rb index f3f3a53..42b30a0 100644 --- a/test/psych/test_psych.rb +++ b/test/psych/test_psych.rb @@ -18,6 +18,26 @@ class TestPsych < Psych::TestCase assert_match(/\? ! "b/, yml) end + def test_header + yml = Psych.dump({:a => {'b' => 'c'}}, {:header => true}) + assert_match(/YAML/, yml) + end + + def test_version_array + yml = Psych.dump({:a => {'b' => 'c'}}, {:version => [1,1]}) + assert_match(/1.1/, yml) + end + + def test_version_string + yml = Psych.dump({:a => {'b' => 'c'}}, {:version => '1.1'}) + assert_match(/1.1/, yml) + end + + def test_version_bool + yml = Psych.dump({:a => {'b' => 'c'}}, {:version => true}) + assert_match(/1.1/, yml) + end + def test_load_argument_error assert_raises(TypeError) do Psych.load nil |