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

ruby-changes:24407

From: tenderlove <ko1@a...>
Date: Thu, 19 Jul 2012 09:36:55 +0900 (JST)
Subject: [ruby-changes:24407] tenderlove:r36458 (trunk): * ext/psych/emitter.c (initialize): allow a configuration object to be

tenderlove	2012-07-19 09:36:42 +0900 (Thu, 19 Jul 2012)

  New Revision: 36458

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

  Log:
    * ext/psych/emitter.c (initialize): allow a configuration object to be
      passed to the constructor so that mutation isn't required after
      instantiation.
    
    * ext/psych/lib/psych/handler.rb: add configuration object
    
    * ext/psych/lib/psych/visitors/emitter.rb: use configuration object if
      extra configuration is present.

  Modified files:
    trunk/ChangeLog
    trunk/ext/psych/emitter.c
    trunk/ext/psych/lib/psych/handler.rb
    trunk/ext/psych/lib/psych/visitors/emitter.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 36457)
+++ ChangeLog	(revision 36458)
@@ -1,3 +1,14 @@
+Thu Jul 19 09:33:46 2012  Aaron Patterson <aaron@t...>
+
+	* ext/psych/emitter.c (initialize): allow a configuration object to be
+	  passed to the constructor so that mutation isn't required after
+	  instantiation.
+
+	* ext/psych/lib/psych/handler.rb: add configuration object
+
+	* ext/psych/lib/psych/visitors/emitter.rb: use configuration object if
+	  extra configuration is present.
+
 Thu Jul 19 08:20:25 2012  Tanaka Akira  <akr@f...>
 
 	* test/ruby/test_file.rb: remove temporally files early.
Index: ext/psych/lib/psych/handler.rb
===================================================================
--- ext/psych/lib/psych/handler.rb	(revision 36457)
+++ ext/psych/lib/psych/handler.rb	(revision 36458)
@@ -11,6 +11,21 @@
   # See Psych::Parser for more details
   class Handler
     ###
+    # Configuration options for dumping YAML.
+    class DumperOptions
+      attr_accessor :line_width, :indentation, :canonical
+
+      def initialize
+        @line_width  = 0
+        @indentation = 2
+        @canonical   = false
+      end
+    end
+
+    # Default dumping options
+    OPTIONS = DumperOptions.new
+
+    ###
     # Called with +encoding+ when the YAML stream starts.  This method is
     # called once per stream.  A stream may contain multiple documents.
     #
Index: ext/psych/lib/psych/visitors/emitter.rb
===================================================================
--- ext/psych/lib/psych/visitors/emitter.rb	(revision 36457)
+++ ext/psych/lib/psych/visitors/emitter.rb	(revision 36458)
@@ -2,10 +2,17 @@
   module Visitors
     class Emitter < Psych::Visitors::Visitor
       def initialize io, options = {}
-        @handler = Psych::Emitter.new io
-        @handler.indentation = options[:indentation] if options[:indentation]
-        @handler.canonical = options[:canonical] if options[:canonical]
-        @handler.line_width = options[:line_width] if options[:line_width]
+        opts = [:indentation, :canonical, :line_width].find_all { |opt|
+          options.key?(opt)
+        }
+
+        if opts.empty?
+          @handler = Psych::Emitter.new io
+        else
+          du = Handler::DumperOptions.new
+          opts.each { |option| du.send :"#{option}=", options[option] }
+          @handler = Psych::Emitter.new io, du
+        end
       end
 
       def visit_Psych_Nodes_Stream o
Index: ext/psych/emitter.c
===================================================================
--- ext/psych/emitter.c	(revision 36457)
+++ ext/psych/emitter.c	(revision 36458)
@@ -2,6 +2,9 @@
 
 VALUE cPsychEmitter;
 static ID id_write;
+static ID id_line_width;
+static ID id_indentation;
+static ID id_canonical;
 
 static void emit(yaml_emitter_t * emitter, yaml_event_t * event)
 {
@@ -39,15 +42,30 @@
     return Data_Wrap_Struct(klass, 0, dealloc, emitter);
 }
 
-/* call-seq: Psych::Emitter.new(io)
+/* call-seq: Psych::Emitter.new(io, options = Psych::Emitter::OPTIONS)
  *
  * Create a new Psych::Emitter that writes to +io+.
  */
-static VALUE initialize(VALUE self, VALUE io)
+static VALUE initialize(int argc, VALUE *argv, VALUE self)
 {
     yaml_emitter_t * emitter;
+    VALUE io, options;
+    VALUE line_width;
+    VALUE indent;
+    VALUE canonical;
+
     Data_Get_Struct(self, yaml_emitter_t, emitter);
 
+    if (rb_scan_args(argc, argv, "11", &io, &options) == 2) {
+	line_width = rb_funcall(options, id_line_width, 0);
+	indent     = rb_funcall(options, id_indentation, 0);
+	canonical  = rb_funcall(options, id_canonical, 0);
+
+	yaml_emitter_set_width(emitter, NUM2INT(line_width));
+	yaml_emitter_set_indent(emitter, NUM2INT(indent));
+	yaml_emitter_set_canonical(emitter, Qtrue == canonical ? 1 : 0);
+    }
+
     yaml_emitter_set_output(emitter, writer, (void *)io);
 
     return self;
@@ -494,7 +512,7 @@
 
     rb_define_alloc_func(cPsychEmitter, allocate);
 
-    rb_define_method(cPsychEmitter, "initialize", initialize, 1);
+    rb_define_method(cPsychEmitter, "initialize", initialize, -1);
     rb_define_method(cPsychEmitter, "start_stream", start_stream, 1);
     rb_define_method(cPsychEmitter, "end_stream", end_stream, 0);
     rb_define_method(cPsychEmitter, "start_document", start_document, 3);
@@ -512,6 +530,9 @@
     rb_define_method(cPsychEmitter, "line_width", line_width, 0);
     rb_define_method(cPsychEmitter, "line_width=", set_line_width, 1);
 
-    id_write = rb_intern("write");
+    id_write       = rb_intern("write");
+    id_line_width  = rb_intern("line_width");
+    id_indentation = rb_intern("indentation");
+    id_canonical   = rb_intern("canonical");
 }
 /* vim: set noet sws=4 sw=4: */

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

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