summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Brown <cb@opscode.com>2009-08-24 15:14:13 -0700
committerChristopher Brown <cb@opscode.com>2009-08-24 15:14:13 -0700
commit8970135a7225a7ee8508d73978c4e44d3c7c953d (patch)
treeca7d6087135bdca0453e05e3a9c010cd25fbedc1
parent535d7c04b71f59826e719e5a9b37a9425fe9ba76 (diff)
downloadmixlib-config-internal_set_fix.tar.gz
added config_attr_writerinternal_set_fix
-rw-r--r--lib/mixlib/config.rb23
-rw-r--r--spec/mixlib/config_spec.rb14
2 files changed, 32 insertions, 5 deletions
diff --git a/lib/mixlib/config.rb b/lib/mixlib/config.rb
index edc1a08..2fd0c94 100644
--- a/lib/mixlib/config.rb
+++ b/lib/mixlib/config.rb
@@ -16,6 +16,12 @@
# limitations under the License.
#
+class Object # http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html
+ def meta_def name, &blk
+ (class << self; self; end).instance_eval { define_method name, &blk }
+ end
+end
+
module Mixlib
module Config
@@ -121,8 +127,22 @@ module Mixlib
end
end
- private :internal_set
+ protected :internal_set
+ # metaprogramming to ensure that the slot for method_symbol
+ # gets set to value after any other logic is run
+ # === Parameters
+ # method_symbol<Symbol>:: Name of the method (variable setter)
+ # blk<Block>:: logic block to run in setting slot method_symbol to value
+ # value<Object>:: Value to be set in config hash
+ #
+ def config_attr_writer(method_symbol, &blk)
+ method_name = "#{method_symbol.to_s}="
+ meta_def method_name do |value|
+ @@configuration[method_symbol] = blk.call(value)
+ end
+ end
+
# Allows for simple lookups and setting of configuration options via method calls
# on Mixlib::Config. If there any arguments to the method, they are used to set
# the value of the configuration option. Otherwise, it's a simple get operation.
@@ -138,7 +158,6 @@ module Mixlib
# <ArgumentError>:: If the method_symbol does not match a configuration option.
def method_missing(method_symbol, *args)
num_args = args.length
-
# Setting
if num_args > 0
method_symbol = $1.to_sym unless (method_symbol.to_s =~ /(.+)=$/).nil?
diff --git a/spec/mixlib/config_spec.rb b/spec/mixlib/config_spec.rb
index 1566beb..7dce8f4 100644
--- a/spec/mixlib/config_spec.rb
+++ b/spec/mixlib/config_spec.rb
@@ -62,6 +62,12 @@ describe Mixlib::Config do
ConfigIt[:alpha] = "one"
ConfigIt[:alpha].should == "one"
end
+
+ it "should allow setting a value with attribute form" do
+ ConfigIt.arbitrary_value = 50
+ ConfigIt.arbitrary_value.should == 50
+ ConfigIt[:arbitrary_value].should == 50
+ end
describe "when a block has been used to set config values" do
before do
@@ -91,12 +97,14 @@ describe Mixlib::Config do
describe "when a class method override accessor exists" do
before do
class ConfigIt
- def self.test_method=(blah)
- configure { |c| c[:test_method] = blah.is_a?(Integer) ? blah * 1000 : blah }
+
+ config_attr_writer :test_method do |blah|
+ blah.is_a?(Integer) ? blah * 1000 : blah
end
+
end
end
-
+
it "should multiply an integer by 1000" do
ConfigIt[:test_method] = 53
ConfigIt[:test_method].should == 53000