diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-07-07 14:17:11 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-07-07 14:17:11 -0700 |
commit | e2f8e49de26b5473d679adb9d8f91ed824a51fa9 (patch) | |
tree | 882301906db1b3d67c0c65f0532a1af69d76aaf7 | |
parent | a5a87e37c7ee522b36b1d43dc78a0f7a50d41efc (diff) | |
download | psych-e2f8e49de26b5473d679adb9d8f91ed824a51fa9.zip |
options are being used, :canonical and :indentation are supported
-rw-r--r-- | ext/psych/emitter.c | 3 | ||||
-rw-r--r-- | lib/psych.rb | 22 | ||||
-rw-r--r-- | lib/psych/core_ext.rb | 5 | ||||
-rw-r--r-- | lib/psych/nodes/node.rb | 4 | ||||
-rw-r--r-- | lib/psych/visitors/emitter.rb | 4 | ||||
-rw-r--r-- | test/psych/test_psych.rb | 10 | ||||
-rw-r--r-- | test/psych/visitors/test_emitter.rb | 20 |
7 files changed, 60 insertions, 8 deletions
diff --git a/ext/psych/emitter.c b/ext/psych/emitter.c index 0400f7e..189b865 100644 --- a/ext/psych/emitter.c +++ b/ext/psych/emitter.c @@ -435,7 +435,8 @@ static VALUE canonical(VALUE self) /* call-seq: emitter.indentation = level * - * Set the indentation level to +level+. + * Set the indentation level to +level+. The level must be less than 10 and + * greater than 1. */ static VALUE set_indentation(VALUE self, VALUE level) { diff --git a/lib/psych.rb b/lib/psych.rb index c1feff8..ba4a683 100644 --- a/lib/psych.rb +++ b/lib/psych.rb @@ -155,11 +155,29 @@ module Psych end ### - # Dump Ruby object +o+ to a YAML string using +options+. + # call-seq: + # Psych.dump(o) -> string of yaml + # Psych.dump(o, options) -> string of yaml + # Psych.dump(o, io) -> io object passed in + # Psych.dump(o, io, options) -> io object passed in + # + # Dump Ruby object +o+ to a YAML string. Optional +options+ may be passed in + # to control the output format. If an IO object is passed in, the YAML will + # be dumped to that IO object. # # Example: # + # # Dump an array, get back a YAML string # Psych.dump(['a', 'b']) # => "---\n- a\n- b\n" + # + # # Dump an array to an IO object + # Psych.dump(['a', 'b'], StringIO.new) # => #<StringIO:0x000001009d0890> + # + # # Dump an array with indentation set + # Psych.dump(['a', ['b']], :indentation => 3) # => "---\n- a\n- - b\n" + # + # # Dump an array to an IO with indentation set + # Psych.dump(['a', ['b']], StringIO.new, :indentation => 3) def self.dump o, io = nil, options = {} if Hash === io options = io @@ -168,7 +186,7 @@ module Psych visitor = Psych::Visitors::YAMLTree.new options visitor << o - visitor.tree.to_yaml io + visitor.tree.to_yaml io, options end ### diff --git a/lib/psych/core_ext.rb b/lib/psych/core_ext.rb index 8d3e8fb..be26b2f 100644 --- a/lib/psych/core_ext.rb +++ b/lib/psych/core_ext.rb @@ -6,9 +6,10 @@ class Object # FIXME: rename this to "to_yaml" when syck is removed ### - # call-seq: to_yaml + # call-seq: to_yaml(options = {}) # - # Convert an object to YAML + # Convert an object to YAML. See Psych.dump for more information on the + # available +options+. def psych_to_yaml options = {} Psych.dump self, options end diff --git a/lib/psych/nodes/node.rb b/lib/psych/nodes/node.rb index 3ab9aca..bfd91f9 100644 --- a/lib/psych/nodes/node.rb +++ b/lib/psych/nodes/node.rb @@ -30,10 +30,10 @@ module Psych # Convert this node to YAML. # # See also Psych::Visitors::Emitter - def to_yaml io = nil + def to_yaml io = nil, options = {} real_io = io || StringIO.new - Visitors::Emitter.new(real_io).accept self + Visitors::Emitter.new(real_io, options).accept self return real_io.string unless io io end diff --git a/lib/psych/visitors/emitter.rb b/lib/psych/visitors/emitter.rb index 0768fbb..c84629f 100644 --- a/lib/psych/visitors/emitter.rb +++ b/lib/psych/visitors/emitter.rb @@ -1,8 +1,10 @@ module Psych module Visitors class Emitter < Psych::Visitors::Visitor - def initialize io + def initialize io, options = {} @handler = Psych::Emitter.new io + @handler.indentation = options[:indentation] if options[:indentation] + @handler.canonical = options[:canonical] if options[:canonical] end def visit_Psych_Nodes_Stream o diff --git a/test/psych/test_psych.rb b/test/psych/test_psych.rb index 0d33cb1..f3f3a53 100644 --- a/test/psych/test_psych.rb +++ b/test/psych/test_psych.rb @@ -8,6 +8,16 @@ class TestPsych < Psych::TestCase Psych.domain_types.clear end + def test_indent + yml = Psych.dump({:a => {'b' => 'c'}}, {:indentation => 5}) + assert_match(/^[ ]{5}b/, yml) + end + + def test_canonical + yml = Psych.dump({:a => {'b' => 'c'}}, {:canonical => true}) + assert_match(/\? ! "b/, yml) + end + def test_load_argument_error assert_raises(TypeError) do Psych.load nil diff --git a/test/psych/visitors/test_emitter.rb b/test/psych/visitors/test_emitter.rb index bdae1bd..7847cea 100644 --- a/test/psych/visitors/test_emitter.rb +++ b/test/psych/visitors/test_emitter.rb @@ -9,6 +9,26 @@ module Psych @visitor = Visitors::Emitter.new @io end + def test_options + io = StringIO.new + visitor = Visitors::Emitter.new io, :indentation => 3 + + s = Nodes::Stream.new + doc = Nodes::Document.new + mapping = Nodes::Mapping.new + m2 = Nodes::Mapping.new + m2.children << Nodes::Scalar.new('a') + m2.children << Nodes::Scalar.new('b') + + mapping.children << Nodes::Scalar.new('key') + mapping.children << m2 + doc.children << mapping + s.children << doc + + visitor.accept s + assert_match(/^[ ]{3}a/, io.string) + end + def test_stream s = Nodes::Stream.new @visitor.accept s |