From 0a18a2c16e21979f9f1e1f18ad9647d1115281f7 Mon Sep 17 00:00:00 2001 From: Ian White Date: Tue, 30 Dec 2008 20:05:35 +1100 Subject: [PATCH] Added Config#configure --- lib/pickle/config.rb | 14 ++++++++++---- spec/lib/pickle_config_spec.rb | 11 +++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/pickle/config.rb b/lib/pickle/config.rb index d3c40d06..9da96d98 100644 --- a/lib/pickle/config.rb +++ b/lib/pickle/config.rb @@ -5,10 +5,16 @@ class Config attr_writer :adapters, :factories, :mappings # default configuration object - def self.default - returning(@default ||= new) do |config| - yield(config) if block_given? - end + def self.default(&block) + returning(@default ||= new) {|config| config.configure(&block) if block_given?} + end + + def initialize(&block) + configure(&block) if block_given? + end + + def configure(&block) + yield(self) end def adapters diff --git a/spec/lib/pickle_config_spec.rb b/spec/lib/pickle_config_spec.rb index 902593a0..b60eb3af 100644 --- a/spec/lib/pickle_config_spec.rb +++ b/spec/lib/pickle_config_spec.rb @@ -73,11 +73,18 @@ class SomeAdapter; end @config.map 'foo', 'bar', :to => 'faz' end - it "should create {:search => '(?:foo|bar)', :replace => 'faz'} mapping" do + it "should create OpenStruct(search: '(?:foo|bar)', replace: 'faz') mapping" do @config.mappings.first.should == OpenStruct.new(:search => '(?:foo|bar)', :replace => 'faz') end end - + + it "#configure(&block) should execiute on self" do + @config.should_receive(:foo).with(:bar) + @config.configure do |c| + c.foo :bar + end + end + describe ".default (class method)" do it "should refer to same object" do Pickle::Config.default.should == Pickle::Config.default