diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-07-18 17:37:38 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-07-18 17:37:38 -0700 |
commit | e82cf0354863a706846e82e05ac252efbd73cdf3 (patch) | |
tree | 6d43dba58b7daa1560963e7c15b49be23c8e904a /lib | |
parent | 2e7bdb3ea359779c98b7ed1ca77491d317070b88 (diff) | |
download | psych-e82cf0354863a706846e82e05ac252efbd73cdf3.zip |
* 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.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/psych/handler.rb | 15 | ||||
-rw-r--r-- | lib/psych/visitors/emitter.rb | 15 |
2 files changed, 26 insertions, 4 deletions
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 @@ -11,6 +11,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 |