Skip to content

Commit

Permalink
All proc evals (including after_config!) should eval_or_yield just li…
Browse files Browse the repository at this point in the history
…ke all the regular blocks
  • Loading branch information
mbklein committed Nov 10, 2011
1 parent b7fc396 commit 2d3a976
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
.bundle
Gemfile.lock
pkg/*
.yardoc
17 changes: 5 additions & 12 deletions lib/confstruct/configuration.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 4 additions & 9 deletions lib/confstruct/hash_with_struct_access.rb
@@ -1,4 +1,5 @@
require 'delegate'
require 'confstruct/utils'

##############
# Confstruct::HashWithStructAccess is a Hash wrapper that provides deep struct access
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lib/confstruct/utils.rb
@@ -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.