Permalink
Browse files

All proc evals (including after_config!) should eval_or_yield just li…

…ke all the regular blocks
  • Loading branch information...
1 parent b7fc396 commit 2d3a9765060296c20e903de490bb472f34648944 @mbklein mbklein committed Nov 9, 2011
Showing with 23 additions and 21 deletions.
  1. +1 −0 .gitignore
  2. +5 −12 lib/confstruct/configuration.rb
  3. +4 −9 lib/confstruct/hash_with_struct_access.rb
  4. +13 −0 lib/confstruct/utils.rb
View
@@ -2,3 +2,4 @@
.bundle
Gemfile.lock
pkg/*
+.yardoc
@@ -8,13 +8,7 @@ def self.ConfigClass defaults=nil, &block
@default_values = defaults
if @default_values.nil?
@default_values = HashWithStructAccess.new({})
- if block_given?
- if block.arity == -1
- @default_values.instance_eval(&block)
- else
- yield @default_values
- end
- end
+ eval_or_yield @default_values, &block
end
end
klazz
@@ -43,12 +37,11 @@ def configure *args, &block
end
if block_given?
- if block.arity == -1
- self.instance_eval(&block)
- else
- yield self
+ eval_or_yield self, &block
+ if self[:after_config!].is_a?(Proc)
+ p = self[:after_config!]
+ eval_or_yield self, &p
end
- self[:after_config!].call if self[:after_config!].is_a?(Proc)
end
self
end
@@ -1,4 +1,5 @@
require 'delegate'
+require 'confstruct/utils'
##############
# Confstruct::HashWithStructAccess is a Hash wrapper that provides deep struct access
@@ -109,15 +110,9 @@ def method_missing sym, *args, &block
end
if result.is_a?(HashWithStructAccess) and block_given?
- if block.arity == -1
- result.instance_eval(&block)
- else
- yield result
- end
- end
-
- if result.is_a?(Proc)
- result.call(self)
+ eval_or_yield result, &block
+ elsif result.is_a?(Proc)
+ eval_or_yield self, &result
else
result
end
View
@@ -0,0 +1,13 @@
+module Kernel
+ def eval_or_yield obj, &block
+ if block_given?
+ if block.arity == -1
+ obj.instance_eval(&block)
+ else
+ block.call(obj)
+ end
+ else
+ obj
+ end
+ end
+end

0 comments on commit 2d3a976

Please sign in to comment.