diff options
author | AJ Christensen <aj@opscode.com> | 2009-03-25 14:17:44 -0700 |
---|---|---|
committer | AJ Christensen <aj@junglist.gen.nz> | 2009-05-12 16:06:48 +1200 |
commit | 7a4e7604ef87f954945fca65a4d8a3e1febba09b (patch) | |
tree | d515a4abee5bba87326e55f78d43a77e3a8cdcc6 | |
parent | e032a3f0549b8ba75d8d651a207d6940d12aac93 (diff) | |
download | mixlib-config-7a4e7604ef87f954945fca65a4d8a3e1febba09b.tar.gz |
Convert mixlib-config to jeweler
Add internal_set method, specs
Clarify some behaviour
-rw-r--r-- | .gitignore | 6 | ||||
-rw-r--r-- | Rakefile | 97 | ||||
-rw-r--r-- | VERSION.yml | 4 | ||||
-rw-r--r-- | features/mixlib_config.feature (renamed from features/config.feature) | 0 | ||||
-rw-r--r-- | features/step_definitions/mixlib_config_steps.rb | 0 | ||||
-rw-r--r-- | lib/mixlib/config.rb | 55 | ||||
-rw-r--r-- | mixlib-config.gemspec | 44 | ||||
-rwxr-xr-x | script/destroy | 14 | ||||
-rwxr-xr-x | script/generate | 14 | ||||
-rw-r--r-- | spec/mixlib/config_spec.rb | 55 | ||||
-rw-r--r-- | spec/spec_helper.rb | 4 |
11 files changed, 190 insertions, 103 deletions
@@ -1 +1,5 @@ -pkg/ +*.sw? +.DS_Store +coverage +rdoc +pkg @@ -1,67 +1,56 @@ require 'rubygems' -require 'rake/gempackagetask' -require 'rubygems/specification' -require 'date' -require 'spec/rake/spectask' -require 'cucumber/rake/task' - - -GEM = "mixlib-config" -GEM_VERSION = "1.0.0" -AUTHOR = "Opscode, Inc." -EMAIL = "info@opscode.com" -HOMEPAGE = "http://www.opscode.com" -SUMMARY = "A class based config mixin, similar to the one found in Chef." - -spec = Gem::Specification.new do |s| - s.name = GEM - s.version = GEM_VERSION - s.platform = Gem::Platform::RUBY - s.has_rdoc = true - s.extra_rdoc_files = ["README.rdoc", "LICENSE", 'NOTICE'] - s.summary = SUMMARY - s.description = s.summary - s.author = AUTHOR - s.email = EMAIL - s.homepage = HOMEPAGE - - # Uncomment this to add a dependency - # s.add_dependency "foo" - - s.require_path = 'lib' - s.autorequire = GEM - s.files = %w(LICENSE README.rdoc Rakefile NOTICE) + Dir.glob("{lib,spec,features}/**/*") +require 'rake' + +begin + require 'jeweler' + Jeweler::Tasks.new do |gem| + gem.name = "mixlib-config" + gem.summary = "A class based config mixin, similar to the one found in Chef." + gem.email = "info@opscode.com" + gem.homepage = "http://www.opscode.com" + gem.authors = ["Opscode, Inc."] + + # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings + end +rescue LoadError + puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com" end -task :default => :test - -desc "Run specs" -Spec::Rake::SpecTask.new do |t| - t.spec_files = FileList['spec/**/*_spec.rb'] - t.spec_opts = %w(-fs --color) +require 'spec/rake/spectask' +Spec::Rake::SpecTask.new(:spec) do |spec| + spec.libs << 'lib' << 'spec' + spec.spec_files = FileList['spec/**/*_spec.rb'] end -Rake::GemPackageTask.new(spec) do |pkg| - pkg.gem_spec = spec +Spec::Rake::SpecTask.new(:rcov) do |spec| + spec.libs << 'lib' << 'spec' + spec.pattern = 'spec/**/*_spec.rb' + spec.rcov = true end -desc "install the gem locally" -task :install => [:package] do - sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}} +begin + require 'cucumber/rake/task' + Cucumber::Rake::Task.new(:features) +rescue LoadError + task :features do + abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber" + end end -desc "create a gemspec file" -task :make_spec do - File.open("#{GEM}.gemspec", "w") do |file| - file.puts spec.to_ruby +task :default => :spec + +require 'rake/rdoctask' +Rake::RDocTask.new do |rdoc| + if File.exist?('VERSION.yml') + config = YAML.load(File.read('VERSION.yml')) + version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}" + else + version = "" end -end -Cucumber::Rake::Task.new(:features) do |t| - t.step_pattern = 'features/steps/**/*.rb' - supportdir = 'features/support' - t.cucumber_opts = "--format pretty -r #{supportdir}" + rdoc.rdoc_dir = 'rdoc' + rdoc.title = "mixlib-config #{version}" + rdoc.rdoc_files.include('README*') + rdoc.rdoc_files.include('lib/**/*.rb') end -desc "Run the spec and features" -task :test => [ :features, :spec ]
\ No newline at end of file diff --git a/VERSION.yml b/VERSION.yml new file mode 100644 index 0000000..646ad57 --- /dev/null +++ b/VERSION.yml @@ -0,0 +1,4 @@ +--- +:major: 1 +:minor: 0 +:patch: 4 diff --git a/features/config.feature b/features/mixlib_config.feature index 6defc7a..6defc7a 100644 --- a/features/config.feature +++ b/features/mixlib_config.feature diff --git a/features/step_definitions/mixlib_config_steps.rb b/features/step_definitions/mixlib_config_steps.rb new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/features/step_definitions/mixlib_config_steps.rb diff --git a/lib/mixlib/config.rb b/lib/mixlib/config.rb index bfa488c..d4faa8e 100644 --- a/lib/mixlib/config.rb +++ b/lib/mixlib/config.rb @@ -55,13 +55,9 @@ module Mixlib # === Raises # <ArgumentError>:: If the configuration option does not exist def [](config_option) - if @@configuration.has_key?(config_option.to_sym) - @@configuration[config_option.to_sym] - else - raise ArgumentError, "Cannot find configuration option #{config_option.to_s}" - end + @@configuration[config_option.to_sym] end - + # Set the value of a configuration option # # === Parameters @@ -71,7 +67,7 @@ module Mixlib # === Returns # value:: The new value of the configuration option def []=(config_option, value) - @@configuration[config_option.to_sym] = value + internal_set(config_option,value) end # Check if Mixlib::Config has a configuration option. @@ -85,6 +81,51 @@ module Mixlib def has_key?(key) @@configuration.has_key?(key.to_sym) end + + # Merge an incoming hash with our config options + # + # === Parameters + # hash<Hash>:: The incoming hash + # + # === Returns + # result of Hash#merge! + def merge!(hash) + @@configuration.merge!(hash) + end + + # Return the set of config hash keys + # + # === Returns + # result of Hash#keys + def keys + @@configuration.keys + end + + # Creates a shallow copy of the internal hash + # + # === Returns + # result of Hash#dup + def hash_dup + @@configuration.dup + end + + # Internal dispatch setter, calling either the real defined method or setting the + # hash value directly + # + # === Parameters + # method_symbol<Symbol>:: Name of the method (variable setter) + # value<Object>:: Value to be set in config hash + # + def internal_set(method_symbol,value) + method_name = method_symbol.id2name + if self.public_methods.include?("#{method_name}=") + self.send("#{method_name}=", value) + else + @@configuration[method_symbol] = value + end + end + + private :internal_set # 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 diff --git a/mixlib-config.gemspec b/mixlib-config.gemspec new file mode 100644 index 0000000..a8571b9 --- /dev/null +++ b/mixlib-config.gemspec @@ -0,0 +1,44 @@ +# -*- encoding: utf-8 -*- + +Gem::Specification.new do |s| + s.name = %q{mixlib-config} + s.version = "1.0.4" + + s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= + s.authors = ["Opscode, Inc."] + s.date = %q{2009-05-12} + s.email = %q{info@opscode.com} + s.extra_rdoc_files = [ + "LICENSE", + "README.rdoc" + ] + s.files = [ + "LICENSE", + "README.rdoc", + "Rakefile", + "VERSION.yml", + "lib/mixlib/config.rb", + "spec/mixlib/config_spec.rb", + "spec/spec_helper.rb" + ] + s.has_rdoc = true + s.homepage = %q{http://www.opscode.com} + s.rdoc_options = ["--charset=UTF-8"] + s.require_paths = ["lib"] + s.rubygems_version = %q{1.3.2} + 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" + ] + + if s.respond_to? :specification_version then + current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION + s.specification_version = 3 + + if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then + else + end + else + end +end diff --git a/script/destroy b/script/destroy deleted file mode 100755 index 40901a8..0000000 --- a/script/destroy +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env ruby -APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')) - -begin - require 'rubigen' -rescue LoadError - require 'rubygems' - require 'rubigen' -end -require 'rubigen/scripts/destroy' - -ARGV.shift if ['--help', '-h'].include?(ARGV[0]) -RubiGen::Base.use_component_sources! [:newgem_simple, :test_unit] -RubiGen::Scripts::Destroy.new.run(ARGV) diff --git a/script/generate b/script/generate deleted file mode 100755 index 5c8ed01..0000000 --- a/script/generate +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env ruby -APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')) - -begin - require 'rubigen' -rescue LoadError - require 'rubygems' - require 'rubigen' -end -require 'rubigen/scripts/generate' - -ARGV.shift if ['--help', '-h'].include?(ARGV[0]) -RubiGen::Base.use_component_sources! [:newgem_simple, :test_unit] -RubiGen::Scripts::Generate.new.run(ARGV) diff --git a/spec/mixlib/config_spec.rb b/spec/mixlib/config_spec.rb index a11c28e..a4f7789 100644 --- a/spec/mixlib/config_spec.rb +++ b/spec/mixlib/config_spec.rb @@ -35,10 +35,10 @@ describe Mixlib::Config do }.should_not raise_error end - it "should raise an ArgumentError with an explanation if you try and set a non-existent variable" do + it "should not raise an ArgumentError with an explanation if you try and set a non-existent variable" do lambda { - ConfigIt[:foobar] - }.should raise_error(ArgumentError) + ConfigIt[:foobar] = "blah" + }.should_not raise_error(ArgumentError) end it "should raise an IOError if it can't find the file" do @@ -56,17 +56,23 @@ describe Mixlib::Config do ConfigIt[:alpha].should == "one" end - it "should allow you to set config values with a block" do - ConfigIt.configure do |c| - c[:cookbook_path] = "monkey_rabbit" - c[:otherthing] = "boo" + describe "when a block has been used to set config values" do + before do + ConfigIt.configure { |c| c[:cookbook_path] = "monkey_rabbit"; c[:otherthing] = "boo" } + end + + {:cookbook_path => "monkey_rabbit", :otherthing => "boo"}.each do |k,v| + it "should allow you to retrieve the config value for #{k} via []" do + ConfigIt[k].should == v + end + it "should allow you to retrieve the config value for #{k} via method_missing" do + ConfigIt.send(k).should == v + end end - ConfigIt.cookbook_path.should == "monkey_rabbit" - ConfigIt.otherthing.should == "boo" end - it "should raise an ArgumentError if you access a config option that does not exist" do - lambda { ConfigIt[:snob_hobbery] }.should raise_error(ArgumentError) + it "should not raise an ArgumentError if you access a config option that does not exist" do + lambda { ConfigIt[:snob_hobbery] }.should_not raise_error(ArgumentError) end it "should return true or false with has_key?" do @@ -75,4 +81,31 @@ describe Mixlib::Config do ConfigIt.has_key?(:monkey).should eql(true) end + 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 } + end + end + end + + it "should multiply an integer by 1000" do + ConfigIt[:test_method] = 53 + ConfigIt[:test_method].should == 53000 + end + + it "should receive internal_set with the method name and config value" do + ConfigIt.should_receive(:internal_set).with(:test_method, 53).and_return(true) + ConfigIt[:test_method] = 53 + end + + after do + class ConfigIt + class << self + undef test_method= + end + end + end + end end
\ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e7ba82b..b31875a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,5 +4,5 @@ $:.push File.join(File.dirname(__FILE__), '..', 'lib') require 'mixlib/config' class ConfigIt - extend Mixlib::Config -end + extend Mixlib::Config +end
\ No newline at end of file |