summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Jacob <adam@opscode.com>2009-09-01 21:18:21 -0700
committerAdam Jacob <adam@opscode.com>2009-09-01 21:18:21 -0700
commitf5fd577ba417e8967c8c94c9d49429be49ba065e (patch)
treeca356129bbfa31cf9bee82809dbcc5beb1998089
parent7519c26df9f9bbe9443977c5fccfc6a441b899eb (diff)
parentfa4bdd1a6b39c7dc4be804d6c871d28a552afbcc (diff)
downloadmixlib-config-f5fd577ba417e8967c8c94c9d49429be49ba065e.tar.gz
Merge branch 'master' of git@github.com:opscode/mixlib-config
-rw-r--r--VERSION.yml2
-rw-r--r--lib/mixlib/config.rb31
-rw-r--r--mixlib-config.gemspec33
-rw-r--r--spec/mixlib/config_spec.rb40
4 files changed, 69 insertions, 37 deletions
diff --git a/VERSION.yml b/VERSION.yml
index f2defa3..5909255 100644
--- a/VERSION.yml
+++ b/VERSION.yml
@@ -1,4 +1,4 @@
---
:major: 1
:minor: 0
-:patch: 10
+:patch: 12
diff --git a/lib/mixlib/config.rb b/lib/mixlib/config.rb
index 9107a91..81d1580 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
@@ -29,11 +35,7 @@ module Mixlib
# === Parameters
# <string>:: A filename to read from
def from_file(filename)
- begin
- self.instance_eval(IO.read(filename), filename, 1)
- rescue Exception => e
- raise IOError, "Cannot open or read #{filename}!" + e
- end
+ self.instance_eval(IO.read(filename), filename, 1)
end
# Pass Mixlib::Config.configure() a block, and it will yield @@configuration.
@@ -125,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.
@@ -142,11 +158,10 @@ 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?
- @@configuration[method_symbol] = (num_args == 1 ? args[0] : args)
+ internal_set method_symbol, (num_args == 1 ? args[0] : args)
end
# Returning
diff --git a/mixlib-config.gemspec b/mixlib-config.gemspec
index 6a0c2d0..8fc3a5b 100644
--- a/mixlib-config.gemspec
+++ b/mixlib-config.gemspec
@@ -2,34 +2,25 @@
Gem::Specification.new do |s|
s.name = %q{mixlib-config}
- s.version = "1.0.10"
+ s.version = "1.0.12"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Opscode, Inc."]
- s.date = %q{2009-08-18}
+ s.date = %q{2009-08-26}
s.email = %q{info@opscode.com}
s.extra_rdoc_files = [
"LICENSE",
- "README.rdoc"
+ "README.rdoc"
]
s.files = [
- ".gitignore",
- "LICENSE",
- "NOTICE",
- "README.rdoc",
- "Rakefile",
- "VERSION.yml",
- "features/mixlib_config.feature",
- "features/step_definitions/mixlib_config_steps.rb",
- "features/steps/config_steps.rb",
- "features/support/bobo.config",
- "features/support/config_it.rb",
- "features/support/env.rb",
- "lib/mixlib/config.rb",
- "mixlib-config.gemspec",
- "spec/mixlib/config_spec.rb",
- "spec/spec.opts",
- "spec/spec_helper.rb"
+ "LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION.yml",
+ "lib/mixlib/config.rb",
+ "spec/mixlib/config_spec.rb",
+ "spec/spec.opts",
+ "spec/spec_helper.rb"
]
s.homepage = %q{http://www.opscode.com}
s.rdoc_options = ["--charset=UTF-8"]
@@ -38,7 +29,7 @@ Gem::Specification.new do |s|
s.summary = %q{A class based config mixin, similar to the one found in Chef.}
s.test_files = [
"spec/mixlib/config_spec.rb",
- "spec/spec_helper.rb"
+ "spec/spec_helper.rb"
]
if s.respond_to? :specification_version then
diff --git a/spec/mixlib/config_spec.rb b/spec/mixlib/config_spec.rb
index 619cd59..7dce8f4 100644
--- a/spec/mixlib/config_spec.rb
+++ b/spec/mixlib/config_spec.rb
@@ -41,10 +41,17 @@ describe Mixlib::Config do
}.should_not raise_error(ArgumentError)
end
- it "should raise an IOError if it can't find the file" do
+ it "should raise an Errno::ENOENT if it can't find the file" do
lambda {
ConfigIt.from_file("/tmp/timmytimmytimmy")
- }.should raise_error(IOError)
+ }.should raise_error(Errno::ENOENT)
+ end
+
+ it "should allow the error to bubble up when it's anything other than IOError" do
+ IO.stub!(:read).with('config.rb').and_return("@#asdf")
+ lambda {
+ ConfigIt.from_file('config.rb')
+ }.should raise_error(SyntaxError)
end
it "should allow you to reference a value by index" do
@@ -55,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
@@ -84,20 +97,33 @@ 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
end
it "should multiply an integer by 1000 with the method_missing form" do
- ConfigIt.test_method = 53
- ConfigIt.test_method.should == 53000
+ ConfigIt.test_method = 63
+ ConfigIt.test_method.should == 63000
+ end
+
+ it "should multiply an integer by 1000 with the instance_eval DSL form" do
+ ConfigIt.instance_eval("test_method 73")
+ ConfigIt.test_method.should == 73000
+ end
+
+ it "should multiply an integer by 1000 via from-file, too" do
+ IO.stub!(:read).with('config.rb').and_return("test_method 99")
+ ConfigIt.from_file('config.rb')
+ ConfigIt.test_method.should == 99000
end
it "should receive internal_set with the method name and config value" do