Skip to content

Commit

Permalink
support instance variables from block context in multi-level dsl
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Siegel committed Dec 6, 2011
1 parent d7fd1a9 commit f45c3cf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/docile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ module Docile
#
# @param dsl [Object] an object whose methods represent a DSL
# @param block [Proc] a block to execute in the DSL context
# @return [Object] the given DSL object
# @return [Object] the dsl object, after execution of the block
def dsl_eval(dsl, &block)
block_context = eval("self", block.binding)
proxy_context = FallbackContextProxy.new(dsl, block_context)
block_context.instance_variables.each { |ivar| proxy_context.instance_variable_set(ivar, block_context.instance_variable_get(ivar)) }
begin
block_context.instance_variables.each { |ivar| proxy_context.instance_variable_set(ivar, block_context.instance_variable_get(ivar)) }
proxy_context.instance_eval(&block)
ensure
block_context.instance_variables.each { |ivar| block_context.instance_variable_set(ivar, proxy_context.instance_variable_get(ivar)) }
Expand Down
15 changes: 11 additions & 4 deletions lib/docile/fallback_context_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

module Docile
class FallbackContextProxy
BASIC_METHODS = Set[:==, :equal?, :"!", :"!=",
:instance_eval, :instance_variable_get, :instance_variable_set,
:object_id, :__send__, :__id__]
NON_PROXIED_METHODS = Set[:object_id, :__send__, :__id__, :==, :equal?, :"!", :"!=", :instance_eval,
:instance_variables, :instance_variable_get, :instance_variable_set,
:remove_instance_variable]

NON_PROXIED_INSTANCE_VARIABLES = Set[:@__receiver__, :@__fallback__]

instance_methods.each do |method|
unless BASIC_METHODS.include?(method.to_sym)
unless NON_PROXIED_METHODS.include?(method.to_sym)
undef_method(method)
end
end
Expand All @@ -26,6 +28,11 @@ def sub(*args, &block)
__proxy_method__(:sub, *args, &block)
end

# Special case to allow proxy instance variables
def instance_variables
super - NON_PROXIED_INSTANCE_VARIABLES.to_a
end

def method_missing(method, *args, &block)
__proxy_method__(method, *args, &block)
end
Expand Down
16 changes: 11 additions & 5 deletions spec/docile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,17 @@ def a; 'not a'; end
@iv1 = 'iv1'; outer { @iv1.should == 'iv1' }
end

#it "should find instance variable from block definition in inner dsl scope" do
# @iv2 = 'iv2'; outer { inner {
# @iv2.should == 'iv2'
# } }
#end
it "should write instance variable assigned in block into outer dsl scope" do
@iv1 = 'foo'; outer { @iv1 = 'bar' }; @iv1.should == 'bar'
end

it "should find instance variable from block definition in inner dsl scope" do
@iv2 = 'iv2'; outer { inner { @iv2.should == 'iv2' } }
end

it "should find instance variable from block definition in inner dsl scope" do
@iv2 = 'foo'; outer { inner { @iv2 = 'bar' } }; @iv2.should == 'bar'
end
end

end
Expand Down

0 comments on commit f45c3cf

Please sign in to comment.