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
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.bundle .bundle
Gemfile.lock Gemfile.lock
pkg/* pkg/*
.yardoc
17 changes: 5 additions & 12 deletions lib/confstruct/configuration.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ def self.ConfigClass defaults=nil, &block
@default_values = defaults @default_values = defaults
if @default_values.nil? if @default_values.nil?
@default_values = HashWithStructAccess.new({}) @default_values = HashWithStructAccess.new({})
if block_given? eval_or_yield @default_values, &block
if block.arity == -1
@default_values.instance_eval(&block)
else
yield @default_values
end
end
end end
end end
klazz klazz
Expand Down Expand Up @@ -43,12 +37,11 @@ def configure *args, &block
end end


if block_given? if block_given?
if block.arity == -1 eval_or_yield self, &block
self.instance_eval(&block) if self[:after_config!].is_a?(Proc)
else p = self[:after_config!]
yield self eval_or_yield self, &p
end end
self[:after_config!].call if self[:after_config!].is_a?(Proc)
end end
self self
end end
Expand Down
13 changes: 4 additions & 9 deletions lib/confstruct/hash_with_struct_access.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'delegate' require 'delegate'
require 'confstruct/utils'


############## ##############
# Confstruct::HashWithStructAccess is a Hash wrapper that provides deep struct access # 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 end


if result.is_a?(HashWithStructAccess) and block_given? if result.is_a?(HashWithStructAccess) and block_given?
if block.arity == -1 eval_or_yield result, &block
result.instance_eval(&block) elsif result.is_a?(Proc)
else eval_or_yield self, &result
yield result
end
end

if result.is_a?(Proc)
result.call(self)
else else
result result
end end
Expand Down
13 changes: 13 additions & 0 deletions lib/confstruct/utils.rb
Original file line number Original file line Diff line number Diff line change
@@ -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.