diff options
author | Christopher Brown <cb@opscode.com> | 2009-08-26 12:53:57 -0700 |
---|---|---|
committer | Christopher Brown <cb@opscode.com> | 2009-08-26 12:53:57 -0700 |
commit | 4804a909ab7b7d1a8f290869ed09ccf5f330f2bf (patch) | |
tree | 1f0cd0ecdf010393c9d7667f8f4e06dff6a37d81 | |
parent | fa4bdd1a6b39c7dc4be804d6c871d28a552afbcc (diff) | |
parent | 89b9dac377a441acd9c41b15482320ffcb03779e (diff) | |
download | mixlib-config-4804a909ab7b7d1a8f290869ed09ccf5f330f2bf.tar.gz |
integrate nuo's class instance variable with attribute setter changesclass_instance_variable
-rw-r--r-- | features/mixlib_config.feature | 14 | ||||
-rw-r--r-- | features/steps/config_steps.rb | 24 | ||||
-rw-r--r-- | features/support/config_it.rb | 4 | ||||
-rw-r--r-- | lib/mixlib/config.rb | 31 |
4 files changed, 54 insertions, 19 deletions
diff --git a/features/mixlib_config.feature b/features/mixlib_config.feature index 6defc7a..35432b7 100644 --- a/features/mixlib_config.feature +++ b/features/mixlib_config.feature @@ -4,12 +4,20 @@ Feature: Configure an application I want to utilize a simple configuration object Scenario: Set a configuration option to a string - Given a configuration class - When I set 'foo' to 'bar' + Given a configuration class 'ConfigIt' + When I set 'foo' to 'bar' in configuration class 'ConfigIt' Then config option 'foo' is 'bar' + +Scenario: Set the same configuration option to different strings for two configuration classes + Given a configuration class 'ConfigIt' + And a configuration class 'ConfigItToo' + When I set 'foo' to 'bar' in configuration class 'ConfigIt' + And I set 'foo' to 'bar2' in configuration class 'ConfigItToo' + Then in configuration class 'ConfigItToo' config option 'foo' is 'bar2' + And in configuration class 'ConfigIt' config option 'foo' is 'bar' Scenario: Set a configuration option to an Array - Given a configuration class + Given a configuration class 'ConfigIt' When I set 'foo' to: |key| |bar| diff --git a/features/steps/config_steps.rb b/features/steps/config_steps.rb index 867702d..0785af3 100644 --- a/features/steps/config_steps.rb +++ b/features/steps/config_steps.rb @@ -16,17 +16,35 @@ # limitations under the License. # -Given /^a configuration class$/ do +Given /^a configuration class '(.+)'$/ do |classname| end -When /^I set '(.+)' to '(.+)'$/ do |key, value| - ConfigIt[key.to_sym] = value +When /^I set '(.+)' to '(.+)' in configuration class '(.+)'$/ do |key, value, classname| + + #ConfigIt[key.to_sym] = value + if classname == 'ConfigIt' + ConfigIt[key.to_sym] = value + elsif classname == 'ConfigItToo' + ConfigItToo[key.to_sym] = value + else + raise ArgumentError, "configuration class must be ConfigIt or ConfigItToo" + end end Then /^config option '(.+)' is '(.+)'$/ do |key, value| ConfigIt[key.to_sym].should == value end +Then /^in configuration class '(.+)' config option '(.+)' is '(.+)'$/ do |classname, key, value| + if classname == 'ConfigIt' + ConfigIt[key.to_sym].should == value + elsif classname == 'ConfigItToo' + ConfigItToo[key.to_sym].should == value + else + raise ArgumentError, "configuration class must be ConfigIt or ConfigItToo" + end +end + When /^I set '(.+)' to:$/ do |key, foo_table| ConfigIt[key.to_sym] = Array.new foo_table.hashes.each do |hash| diff --git a/features/support/config_it.rb b/features/support/config_it.rb index 28ea454..c872e33 100644 --- a/features/support/config_it.rb +++ b/features/support/config_it.rb @@ -21,4 +21,8 @@ require 'mixlib/config' class ConfigIt extend Mixlib::Config +end + +class ConfigItToo + extend Mixlib::Config end
\ No newline at end of file diff --git a/lib/mixlib/config.rb b/lib/mixlib/config.rb index 2fd0c94..14699b0 100644 --- a/lib/mixlib/config.rb +++ b/lib/mixlib/config.rb @@ -1,5 +1,7 @@ # # Author:: Adam Jacob (<adam@opscode.com>) +# Author:: Nuo Yan (<nuo@opscode.com>) +# Author:: Christopher Brown (<cb@opscode.com>) # Copyright:: Copyright (c) 2008 Opscode, Inc. # License:: Apache License, Version 2.0 # @@ -24,8 +26,11 @@ end module Mixlib module Config - - @@configuration = Hash.new + + def self.extended(base) + class << base; attr_accessor :configuration; end + base.configuration = Hash.new + end # Loads a given ruby file, and runs instance_eval against it in the context of the current # object. @@ -38,12 +43,12 @@ module Mixlib self.instance_eval(IO.read(filename), filename, 1) end - # Pass Mixlib::Config.configure() a block, and it will yield @@configuration. + # Pass Mixlib::Config.configure() a block, and it will yield self.configuration. # # === Parameters - # <block>:: A block that is sent @@configuration as its argument + # <block>:: A block that is sent self.configuration as its argument def configure(&block) - block.call(@@configuration) + block.call(self.configuration) end # Get the value of a configuration option @@ -57,7 +62,7 @@ module Mixlib # === Raises # <ArgumentError>:: If the configuration option does not exist def [](config_option) - @@configuration[config_option.to_sym] + self.configuration[config_option.to_sym] end # Set the value of a configuration option @@ -81,7 +86,7 @@ module Mixlib # <True>:: If the configuration option exists # <False>:: If the configuration option does not exist def has_key?(key) - @@configuration.has_key?(key.to_sym) + self.configuration.has_key?(key.to_sym) end # Merge an incoming hash with our config options @@ -92,7 +97,7 @@ module Mixlib # === Returns # result of Hash#merge! def merge!(hash) - @@configuration.merge!(hash) + self.configuration.merge!(hash) end # Return the set of config hash keys @@ -100,7 +105,7 @@ module Mixlib # === Returns # result of Hash#keys def keys - @@configuration.keys + self.configuration.keys end # Creates a shallow copy of the internal hash @@ -108,7 +113,7 @@ module Mixlib # === Returns # result of Hash#dup def hash_dup - @@configuration.dup + self.configuration.dup end # Internal dispatch setter, calling either the real defined method or setting the @@ -123,7 +128,7 @@ module Mixlib if (self.public_methods - ["[]="]).include?("#{method_name}=") self.send("#{method_name}=", value) else - @@configuration[method_symbol] = value + self.configuration[method_symbol] = value end end @@ -139,7 +144,7 @@ module Mixlib 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) + self.configuration[method_symbol] = blk.call(value) end end @@ -165,7 +170,7 @@ module Mixlib end # Returning - @@configuration[method_symbol] + self.configuration[method_symbol] end end |