From 4b4cdfd351c9514bf6a60382dfc7d9bae95b97ba Mon Sep 17 00:00:00 2001 From: jana4u Date: Fri, 13 Jul 2012 01:59:26 +0200 Subject: [PATCH] fixed configuration nesting --- configuration.gemspec | 3 ++- lib/configuration.rb | 19 +++++++++++++------ test/defaults_test.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 test/defaults_test.rb diff --git a/configuration.gemspec b/configuration.gemspec index 35a4c20..4dbde08 100644 --- a/configuration.gemspec +++ b/configuration.gemspec @@ -30,10 +30,11 @@ Gem::Specification::new do |spec| "samples/e.rb", "samples/f.rb", "test", + "test/defaults_test.rb", "test/overwrite_test.rb"] spec.executables = [] - + spec.require_path = "lib" spec.test_files = nil diff --git a/lib/configuration.rb b/lib/configuration.rb index 4a42385..871ca5d 100644 --- a/lib/configuration.rb +++ b/lib/configuration.rb @@ -58,7 +58,7 @@ def load name Table[key] end end - send :extend, ClassMethods + send :extend, ClassMethods module InstanceMethods attr 'name' @@ -113,7 +113,7 @@ class DSL instance_methods.each do |m| undef_method m unless m[Protected] - end + end Kernel.methods.each do |m| next if m[Protected] @@ -134,8 +134,15 @@ def Method m def self.evaluate configuration, options = {}, &block dsl = new configuration - - options.each{|key, value| Pure[dsl].send key, value} + + options.each do |key, value| + if value.is_a?(Hash) + Pure[dsl].send key, evaluate(dsl, value) + else + Pure[dsl].send key, value + end + end + Pure[dsl].instance_eval(&block) if block Pure[dsl].instance_eval{ @__configuration } @@ -164,7 +171,7 @@ def method_missing(m, *a, &b) name = m.to_s configuration = if @__configuration.respond_to?(name) and Configuration === @__configuration.send(name) - @__configuration.send name + @__configuration.send name else Configuration.new name end @@ -208,7 +215,7 @@ class Pure ::Object.instance_methods.each do |m| Instance_Methods[m.to_s] = ::Object.instance_method m undef_method m unless m[Protected] - end + end def method_missing m, *a, &b Instance_Methods[m.to_s].bind(@object).call(*a, &b) diff --git a/test/defaults_test.rb b/test/defaults_test.rb new file mode 100644 index 0000000..efeee12 --- /dev/null +++ b/test/defaults_test.rb @@ -0,0 +1,26 @@ +require 'minitest/autorun' +require 'configuration.rb' + +describe Configuration do + + before do + @b = Configuration.for('b') { + host "codeforpeople.com" + + mail { + host "gmail.com" + } + } + + @c = Configuration.for('c', @b) { + foo 'bar' + } + end + + it "must return default values" do + @b.host.must_equal @c.host + @b.mail.must_equal @c.mail + @b.mail.host.must_equal @c.mail.host + end + +end