From f49bfd024cb698cc14b1881904879acf6c513c5e Mon Sep 17 00:00:00 2001 From: Michael Dvorkin Date: Tue, 15 Oct 2013 13:41:17 -0700 Subject: [PATCH] Added Inspector#hashify to simplify and shorten the options parameters --- lib/awesome_print/inspector.rb | 24 +++++++++++++ spec/options_spec.rb | 65 ++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 spec/options_spec.rb diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index 0893a90..49e1025 100755 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -151,6 +151,30 @@ module AwesomePrint end end + # Convert *options arguments that might include symbols and hashes to canonical + # options = {} hash. Symbols with the "no" prefix are treated as negatives. + # For example: + # + # hashify(:noindex, :plain, raw: true) => { index: false, plain: true, raw: true} + #------------------------------------------------------------------------------ + def hashify(options) + options.inject({}) do |hash, option| + case option + when Symbol + if option.to_s !~ /^no(.+)$/i + hash[option] = true + else + hash[$1.downcase.to_sym] = false + end + when Hash + hash.merge!(option) + else + raise ArgumentError, "Invalid option #{option.inspect}" + end + hash + end + end + # Update @options by first merging the :color hash and then the remaining keys. #------------------------------------------------------------------------------ def merge_options!(options = {}) diff --git a/spec/options_spec.rb b/spec/options_spec.rb new file mode 100644 index 0000000..d43238d --- /dev/null +++ b/spec/options_spec.rb @@ -0,0 +1,65 @@ +require File.expand_path(File.dirname(__FILE__) + '/spec_helper') + +describe "AwesomePrint" do + describe "Options" do + before do + stub_dotfile! + @ap = AwesomePrint::Inspector.new + end + + it "should merge options" do + @ap.send(:merge_options!, { :color => { :array => :black }, :indent => 0 }) + options = @ap.instance_variable_get("@options") + options[:color][:array].should == :black + options[:indent].should == 0 + end + + it "empty list" do + @ap.send(:hashify, []).should == {} + end + + it "hash only" do + @ap.send(:hashify, [ { html: true, plain: true } ]).should == { html: true, plain: true } + end + + it "symbol without [no] prefix" do + @ap.send(:hashify, [ :index ]).should == { index: true } + end + + it "multiple symbols without [no] prefix" do + @ap.send(:hashify, [ :index, :plain ]).should == { index: true, plain: true } + end + + it "symbol with [no] prefix" do + @ap.send(:hashify, [ :noindex ]).should == { index: false } + end + + it "multiple symbols with [no] prefix" do + @ap.send(:hashify, [ :noindex, :noplain ]).should == { index: false, plain: false } + end + + it "multiple symbols with and without [no] prefix" do + @ap.send(:hashify, [ :noindex, :plain ]).should == { index: false, plain: true } + end + + it "mixing symbols and hashes #1" do + @ap.send(:hashify, [ :noindex, { plain: false } ]).should == { index: false, plain: false } + end + + it "mixing symbols and hashes #2" do + @ap.send(:hashify, [ { plain: true }, :noindex, :noplain ]).should == { index: false, plain: false } + end + + it "mixing symbols and hashes #3" do + @ap.send(:hashify, [ { plain: false }, :noindex, :plain ]).should == { index: false, plain: true } + end + + it "mixing symbols and hashes #4" do + @ap.send(:hashify, [ { plain: false }, :noindex, { multiline: true } ]).should == { plain: false, index: false, multiline: true } + end + + it "mixing symbols and hashes #5" do + @ap.send(:hashify, [ { color: { array: :red } }, :noindex, :plain ]).should == { color: { array: :red }, index: false, plain: true } + end + end +end