From e82cf0354863a706846e82e05ac252efbd73cdf3 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 18 Jul 2012 17:37:38 -0700 Subject: * 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. --- lib/psych/handler.rb | 15 +++++++++++++++ lib/psych/visitors/emitter.rb | 15 +++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/psych/handler.rb b/lib/psych/handler.rb index a2aa6bb..d3b9963 100644 --- a/lib/psych/handler.rb +++ b/lib/psych/handler.rb @@ -10,6 +10,21 @@ module Psych # # 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. diff --git a/lib/psych/visitors/emitter.rb b/lib/psych/visitors/emitter.rb index 30db176..c886e50 100644 --- a/lib/psych/visitors/emitter.rb +++ b/lib/psych/visitors/emitter.rb @@ -2,10 +2,17 @@ module Psych 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 -- cgit v1.2.3