support instance variables from block context in multi-level dsl

This commit is contained in:
Marc Siegel 2011-12-06 16:21:11 -05:00
parent d7fd1a9450
commit f45c3cf8d7
3 changed files with 24 additions and 11 deletions

View File

@ -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)) }

View File

@ -2,12 +2,14 @@ require 'set'
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
@ -26,6 +28,11 @@ module Docile
__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

View File

@ -76,11 +76,17 @@ describe Docile do
@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