[前][次][番号順一覧][スレッド一覧]

ruby-changes:16581

From: tenderlove <ko1@a...>
Date: Thu, 8 Jul 2010 08:06:03 +0900 (JST)
Subject: [ruby-changes:16581] Ruby:r28573 (trunk): * ext/psych/lib/psych/visitors/emitter.rb: sending emit options to

tenderlove	2010-07-08 08:05:45 +0900 (Thu, 08 Jul 2010)

  New Revision: 28573

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=28573

  Log:
    * ext/psych/lib/psych/visitors/emitter.rb: sending emit options to
      YAML emitter. [ruby-core:28318]
    * ext/psych/emitter.c: updating documentation about emit options
    * ext/psych/lib/psych/core_ext.rb: ditto
    * ext/psych/lib/psych.rb (dump): passing emit options to emitter.
    * ext/psych/lib/psych/nodes/node.rb: ditto

  Modified files:
    trunk/ChangeLog
    trunk/ext/psych/emitter.c
    trunk/ext/psych/lib/psych/core_ext.rb
    trunk/ext/psych/lib/psych/nodes/node.rb
    trunk/ext/psych/lib/psych/visitors/emitter.rb
    trunk/ext/psych/lib/psych.rb
    trunk/test/psych/test_psych.rb
    trunk/test/psych/visitors/test_emitter.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 28572)
+++ ChangeLog	(revision 28573)
@@ -1,3 +1,16 @@
+Thu Jul  8 08:01:03 2010  Aaron Patterson <aaron@t...>
+
+	* ext/psych/emitter.c: updating documentation about emit options
+
+	* ext/psych/lib/psych/core_ext.rb: ditto
+
+	* ext/psych/lib/psych.rb (dump): passing emit options to emitter.
+
+	* ext/psych/lib/psych/nodes/node.rb: ditto
+
+	* ext/psych/lib/psych/visitors/emitter.rb: sending emit options to
+	  YAML emitter. [ruby-core:28318]
+
 Thu Jul  8 06:05:58 2010  Tanaka Akira  <akr@f...>
 
 	* strftime.c (rb_strftime_with_timespec): support %:z and %::z.
Index: ext/psych/lib/psych/visitors/emitter.rb
===================================================================
--- ext/psych/lib/psych/visitors/emitter.rb	(revision 28572)
+++ ext/psych/lib/psych/visitors/emitter.rb	(revision 28573)
@@ -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
Index: ext/psych/lib/psych/nodes/node.rb
===================================================================
--- ext/psych/lib/psych/nodes/node.rb	(revision 28572)
+++ ext/psych/lib/psych/nodes/node.rb	(revision 28573)
@@ -30,10 +30,10 @@
       # 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
Index: ext/psych/lib/psych/core_ext.rb
===================================================================
--- ext/psych/lib/psych/core_ext.rb	(revision 28572)
+++ ext/psych/lib/psych/core_ext.rb	(revision 28573)
@@ -6,9 +6,10 @@
   # 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
Index: ext/psych/lib/psych.rb
===================================================================
--- ext/psych/lib/psych.rb	(revision 28572)
+++ ext/psych/lib/psych.rb	(revision 28573)
@@ -155,11 +155,29 @@
   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 @@
 
     visitor = Psych::Visitors::YAMLTree.new options
     visitor << o
-    visitor.tree.to_yaml io
+    visitor.tree.to_yaml io, options
   end
 
   ###
Index: ext/psych/emitter.c
===================================================================
--- ext/psych/emitter.c	(revision 28572)
+++ ext/psych/emitter.c	(revision 28573)
@@ -435,7 +435,8 @@
 
 /* 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)
 {
Index: test/psych/test_psych.rb
===================================================================
--- test/psych/test_psych.rb	(revision 28572)
+++ test/psych/test_psych.rb	(revision 28573)
@@ -8,6 +8,16 @@
     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
Index: test/psych/visitors/test_emitter.rb
===================================================================
--- test/psych/visitors/test_emitter.rb	(revision 28572)
+++ test/psych/visitors/test_emitter.rb	(revision 28573)
@@ -9,6 +9,26 @@
         @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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]