Permalink
Browse files

Simplify! Get rid of ConfigClass() in favor of instantiating with def…

…aults
  • Loading branch information...
1 parent 47b6e64 commit a9fa035e46c825f2274d5d574e4222ddcd682023 @mbklein mbklein committed Nov 10, 2011
Showing with 33 additions and 51 deletions.
  1. +5 −11 README.md
  2. +9 −25 lib/confstruct/configuration.rb
  3. +4 −1 lib/confstruct/hash_with_struct_access.rb
  4. +15 −14 spec/confstruct/configuration_spec.rb
View
@@ -8,34 +8,28 @@ way, while keeping things simple and intuitive.
First, either create an empty `ConfStruct::Configuration` object:
- config = Confstruct::Confguration.new
+ config = Confstruct::Configuration.new
-Or use `ConfigClass` to define a subclass with default values:
+Or with some default values:
- MyConfigClass = Confstruct::ConfigClass({
+ config = Confstruct::Configuration.new({
:project => 'confstruct',
:github => {
:url => 'http://www.github.com/mbklein/confstruct',
:branch => 'master'
}
})
- config = MyConfigClass.new
The above can also be done in block form:
- # Confstruct::ConfigClass with no parentheses will be treated as a
- # constant, not a method. In block form, use either Confstruct.ConfigClass
- # or Confstruct::ConfigClass()
-
- MyConfigClass = Confstruct.ConfigClass do
+ config = Confstruct::Configuration.new do
project 'confstruct'
github do
url 'http://www.github.com/mbklein/confstruct'
branch 'master'
end
end
- config = MyConfigClass.new
-
+
There are many ways to configure the resulting `config` object...
The Struct-like way
@@ -2,38 +2,18 @@
module Confstruct
- def self.ConfigClass defaults=nil, &block
- klazz = Class.new(Confstruct::Configuration)
- klazz.instance_eval do
- @default_values = defaults
- if @default_values.nil?
- @default_values = HashWithStructAccess.new({})
- eval_or_yield @default_values, &block
- end
- end
- klazz
- end
-
class Configuration < HashWithStructAccess
- class << self; attr_accessor :default_values; end
- @default_values = {}
-
- def initialize hash=nil, &block
- super(hash || {})
- initialize_default_values! if hash.nil?
- configure &block if block_given?
+ def initialize hash=@@hash_class.new, &block
+ super({})
+ @default_values = hash.is_a?(HashWithStructAccess) ? hash : HashWithStructAccess.new(hash)
+ eval_or_yield @default_values, &block
+ reset_defaults!
end
def after_config! obj
end
- def initialize_default_values!
- self.class.new(self.class.default_values).deep_copy.each do |k,v|
- self[k] ||= v
- end
- end
-
def configure *args, &block
if args[0].respond_to?(:each_pair)
self.deep_merge!(args[0])
@@ -63,5 +43,9 @@ def pop!
self
end
+ def reset_defaults!
+ self.replace(default_values.deep_copy)
+ end
+
end
end
@@ -14,6 +14,9 @@ class HashWithStructAccess < DelegateClass(Hash); @@ordered = true; @@hash_class
end
class HashWithStructAccess
+ attr_accessor :default_values
+ @default_values = {}
+
class << self
def from_hash hash
symbolized_hash = symbolize_hash hash
@@ -85,7 +88,7 @@ def inspect
end
def is_a? klazz
- klazz == Hash or super
+ klazz == @@hash_class or super
end
alias_method :_keys, :keys
@@ -10,7 +10,7 @@
conf.should == {}
end
- context "subclass" do
+ context "default values" do
before :all do
@defaults = {
:project => 'confstruct',
@@ -19,32 +19,27 @@
:branch => 'master'
}
}
- @test_config_class = Confstruct::ConfigClass(@defaults)
end
before :each do
- @config = @test_config_class.new
+ @config = Confstruct::Configuration.new(@defaults)
end
it "should have the correct defaults" do
- @test_config_class.default_values.should == @defaults
- end
-
- it "should instantiate with the correct default values" do
+ @config.default_values.should == @defaults
@config.should == @defaults
- @config.object_id.should_not == @defaults.object_id
end
it "can be defined in block mode" do
- block_config_class = Confstruct.ConfigClass do
+ config = Confstruct::Configuration.new do
project 'confstruct'
github do
url 'http://www.github.com/mbklein/confstruct'
branch 'master'
end
end
- block_config_class.default_values.should == @defaults
- block_config_class.new.should == @defaults
+ config.default_values.should == @defaults
+ config.should == @defaults
end
end
@@ -65,12 +60,10 @@
:branch => 'master'
}
}
-
- TestConfigClass = Confstruct::ConfigClass(@defaults)
end
before :each do
- @config = TestConfigClass.new
+ @config = Confstruct::Configuration.new(@defaults)
end
it "should deep merge a hash" do
@@ -105,6 +98,14 @@
lambda { @config.pop! }.should raise_error(IndexError)
end
+ it "should #reset_defaults!" do
+ @config.project = 'other-project'
+ @config.github.url = 'http://www.github.com/mbklein/other-project'
+ @config.should == @configured
+ @config.reset_defaults!
+ @config.should == @defaults
+ end
+
it "should call #after_config! when configuration is complete" do
postconfigurator = RSpec::Mocks::Mock.new('after_config!')
postconfigurator.should_receive(:configured!).once.with(@config)

0 comments on commit a9fa035

Please sign in to comment.