Permalink
Browse files

add 'all' config option for global defaults

  • Loading branch information...
1 parent 570cfd4 commit 35774b76f75bc973f458575d2fa9a41ec7db2f56 @davejacobs committed Jul 11, 2013
Showing with 36 additions and 2 deletions.
  1. +15 −1 lib/letters/config.rb
  2. +21 −1 spec/letters/config_spec.rb
View
@@ -18,15 +18,25 @@ def self.defaults
end
end
+ def self.global_defaults=(opts)
+ @global_defaults = opts
+ end
+
+ def self.global_defaults
+ @global_defaults || {}
+ end
+
def self.user_defaults
@user_defaults ||= Hash.new {|h, k| h[k] = Hash.new }
end
def self.defaults_with(letter, opts={})
- defaults[letter].merge(user_defaults[letter]).merge(opts)
+ # TODO: This is obviously a reduce, so change it to that
+ global_defaults.merge(defaults[letter]).merge(user_defaults[letter]).merge(opts)
end
def self.reset_config!
+ global_defaults.clear
user_defaults.clear
end
@@ -35,6 +45,10 @@ def self.config(&block)
end
module Config
+ define_singleton_method("all") do |opts={}|
+ Letters.global_defaults = opts
+ end
+
("a".."z").each do |letter|
define_singleton_method(letter) do |opts={}|
Letters.user_defaults[letter.to_sym] = opts
@@ -12,14 +12,34 @@ module Letters
end
after do
- FileUtils.rm "log"
+ Letters.reset_config!
+ FileUtils.rm_rf "log"
end
describe ".config" do
it "allows default argument configuration" do
hash.f
File.read("log").should == hash.pretty_inspect
end
+
+ it "allows global default argument configuration" do
+ Letters.config do
+ all :line_no => true
+ end
+
+ $stdout.should_receive(:puts).exactly(4).times
+ hash.b
+ end
+
+ it "allows specific defaults to override global defaults" do
+ Letters.config do
+ all :line_no => true
+ b :line_no => false
+ end
+
+ $stdout.should_receive(:puts).never
+ hash.b
+ end
end
describe ".reset_config!" do

0 comments on commit 35774b7

Please sign in to comment.