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

ruby-changes:25301

From: kou <ko1@a...>
Date: Sun, 28 Oct 2012 15:00:16 +0900 (JST)
Subject: [ruby-changes:25301] kou:r37353 (trunk): * lib/rexml/document.rb (REXML::Document#write): Accept options

kou	2012-10-28 14:59:59 +0900 (Sun, 28 Oct 2012)

  New Revision: 37353

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

  Log:
    * lib/rexml/document.rb (REXML::Document#write): Accept options
      Hash as argument.
    * test/rexml/test_document.rb: Add tests for the above change.

  Modified files:
    trunk/ChangeLog
    trunk/lib/rexml/document.rb
    trunk/test/rexml/test_document.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 37352)
+++ ChangeLog	(revision 37353)
@@ -1,3 +1,9 @@
+Sun Oct 28 14:59:14 2012  Kouhei Sutou  <kou@c...>
+
+	* lib/rexml/document.rb (REXML::Document#write): Accept options
+	  Hash as argument.
+	* test/rexml/test_document.rb: Add tests for the above change.
+
 Sun Oct 28 14:09:44 2012  Kouhei Sutou  <kou@c...>
 
 	* lib/rexml/document.rb (REXML::Document#write): Fix wrong usage
Index: lib/rexml/document.rb
===================================================================
--- lib/rexml/document.rb	(revision 37352)
+++ lib/rexml/document.rb	(revision 37353)
@@ -144,6 +144,10 @@
       xml_decl().stand_alone?
     end
 
+    # :call-seq:
+    #    doc.write(output=$stdout, indent=-1, transtive=false, ie_hack=false)
+    #    doc.write(options={:output => $stdout, :indent => -1, :transtive => false, :ie_hack => false})
+    #
     # Write the XML tree out, optionally with indent.  This writes out the
     # entire XML document, including XML declarations, doctype declarations,
     # and processing instructions (if any are given).
@@ -154,15 +158,27 @@
     # specified, because it adds unnecessary bandwidth to applications such
     # as XML-RPC.
     #
-    # See also the classes in the rexml/formatters package for the proper way
-    # to change the default formatting of XML output
+    # Accept Nth argument style and options Hash style as argument.
+    # The recommended style is options Hash style for one or more
+    # arguments case.
     #
     # _Examples_
     #   Document.new("<a><b/></a>").write
     #
-    #   output_string = ""
+    #   output = ""
+    #   Document.new("<a><b/></a>").write(output)
+    #
+    #   output = ""
+    #   Document.new("<a><b/></a>").write(:output => output, :indent => 2)
+    #
+    # See also the classes in the rexml/formatters package for the proper way
+    # to change the default formatting of XML output.
+    #
+    # _Examples_
+    #
+    #   output = ""
     #   tr = Transitive.new
-    #   tr.write(Document.new("<a><b/></a>"), output_string)
+    #   tr.write(Document.new("<a><b/></a>"), output)
     #
     # output::
     #     output an object which supports '<< string'; this is where the
@@ -183,7 +199,23 @@
     #   unable to parse proper XML, we have to provide a hack to generate XML
     #   that IE's limited abilities can handle.  This hack inserts a space
     #   before the /> on empty tags.  Defaults to false
-    def write( output=$stdout, indent=-1, transitive=false, ie_hack=false )
+    def write(*arguments)
+      if arguments.size == 1 and arguments[0].class == Hash
+        options = arguments[0]
+
+        output     = options[:output]
+        indent     = options[:indent]
+        transitive = options[:transitive]
+        ie_hack    = options[:ie_hack]
+      else
+        output, indent, transitive, ie_hack, = *arguments
+      end
+
+      output ||= $stdout
+      indent ||= -1
+      transitive = false if transitive.nil?
+      ie_hack    = false if ie_hack.nil?
+
       if xml_decl.encoding != 'UTF-8' && !output.kind_of?(Output)
         output = Output.new( output, xml_decl.encoding )
       end
Index: test/rexml/test_document.rb
===================================================================
--- test/rexml/test_document.rb	(revision 37352)
+++ test/rexml/test_document.rb	(revision 37353)
@@ -106,4 +106,99 @@
     doc = REXML::Document.new('<?xml version="1.0" standalone=  "no" ?>')
     assert_equal('no', doc.stand_alone?, bug2539)
   end
+
+  class WriteTest < Test::Unit::TestCase
+    def setup
+      @document = REXML::Document.new(<<-EOX)
+<?xml version="1.0" encoding="UTF-8"?>
+<message>Hello world!</message>
+EOX
+    end
+
+    class ArgumentsTest < self
+      def test_output
+        output = ""
+        @document.write(output)
+        assert_equal(<<-EOX, output)
+<?xml version='1.0' encoding='UTF-8'?>
+<message>Hello world!</message>
+EOX
+      end
+
+      def test_indent
+        output = ""
+        indent = 2
+        @document.write(output, indent)
+        assert_equal(<<-EOX.chomp, output)
+<?xml version='1.0' encoding='UTF-8'?>
+<message>
+  Hello world!
+</message>
+EOX
+      end
+
+      def test_transitive
+        output = ""
+        indent = 2
+        transitive = true
+        @document.write(output, indent, transitive)
+        assert_equal(<<-EOX, output)
+<?xml version='1.0' encoding='UTF-8'?>
+<message
+>Hello world!</message
+>
+EOX
+      end
+
+      def test_ie_hack
+        output = ""
+        indent = -1
+        transitive = false
+        ie_hack = true
+        document = REXML::Document.new("<empty/>")
+        document.write(output, indent, transitive, ie_hack)
+        assert_equal("<empty />", output)
+      end
+    end
+
+    class OptionsTest < self
+      def test_output
+        output = ""
+        @document.write(:output => output)
+        assert_equal(<<-EOX, output)
+<?xml version='1.0' encoding='UTF-8'?>
+<message>Hello world!</message>
+EOX
+      end
+
+      def test_indent
+        output = ""
+        @document.write(:output => output, :indent => 2)
+        assert_equal(<<-EOX.chomp, output)
+<?xml version='1.0' encoding='UTF-8'?>
+<message>
+  Hello world!
+</message>
+EOX
+      end
+
+      def test_transitive
+        output = ""
+        @document.write(:output => output, :indent => 2, :transitive => true)
+        assert_equal(<<-EOX, output)
+<?xml version='1.0' encoding='UTF-8'?>
+<message
+>Hello world!</message
+>
+EOX
+      end
+
+      def test_ie_hack
+        output = ""
+        document = REXML::Document.new("<empty/>")
+        document.write(:output => output, :ie_hack => true)
+        assert_equal("<empty />", output)
+      end
+    end
+  end
 end

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

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