mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Added Inspector#hashify to simplify and shorten the options parameters
This commit is contained in:
parent
7161934b4d
commit
f49bfd024c
2 changed files with 89 additions and 0 deletions
|
@ -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 = {})
|
||||
|
|
65
spec/options_spec.rb
Normal file
65
spec/options_spec.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue