support instance variables from block context in multi-level dsl
This commit is contained in:
parent
d7fd1a9450
commit
f45c3cf8d7
|
@ -15,12 +15,12 @@ module Docile
|
||||||
#
|
#
|
||||||
# @param dsl [Object] an object whose methods represent a DSL
|
# @param dsl [Object] an object whose methods represent a DSL
|
||||||
# @param block [Proc] a block to execute in the DSL context
|
# @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)
|
def dsl_eval(dsl, &block)
|
||||||
block_context = eval("self", block.binding)
|
block_context = eval("self", block.binding)
|
||||||
proxy_context = FallbackContextProxy.new(dsl, block_context)
|
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
|
begin
|
||||||
|
block_context.instance_variables.each { |ivar| proxy_context.instance_variable_set(ivar, block_context.instance_variable_get(ivar)) }
|
||||||
proxy_context.instance_eval(&block)
|
proxy_context.instance_eval(&block)
|
||||||
ensure
|
ensure
|
||||||
block_context.instance_variables.each { |ivar| block_context.instance_variable_set(ivar, proxy_context.instance_variable_get(ivar)) }
|
block_context.instance_variables.each { |ivar| block_context.instance_variable_set(ivar, proxy_context.instance_variable_get(ivar)) }
|
||||||
|
|
|
@ -2,12 +2,14 @@ require 'set'
|
||||||
|
|
||||||
module Docile
|
module Docile
|
||||||
class FallbackContextProxy
|
class FallbackContextProxy
|
||||||
BASIC_METHODS = Set[:==, :equal?, :"!", :"!=",
|
NON_PROXIED_METHODS = Set[:object_id, :__send__, :__id__, :==, :equal?, :"!", :"!=", :instance_eval,
|
||||||
:instance_eval, :instance_variable_get, :instance_variable_set,
|
:instance_variables, :instance_variable_get, :instance_variable_set,
|
||||||
:object_id, :__send__, :__id__]
|
:remove_instance_variable]
|
||||||
|
|
||||||
|
NON_PROXIED_INSTANCE_VARIABLES = Set[:@__receiver__, :@__fallback__]
|
||||||
|
|
||||||
instance_methods.each do |method|
|
instance_methods.each do |method|
|
||||||
unless BASIC_METHODS.include?(method.to_sym)
|
unless NON_PROXIED_METHODS.include?(method.to_sym)
|
||||||
undef_method(method)
|
undef_method(method)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -26,6 +28,11 @@ module Docile
|
||||||
__proxy_method__(:sub, *args, &block)
|
__proxy_method__(:sub, *args, &block)
|
||||||
end
|
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)
|
def method_missing(method, *args, &block)
|
||||||
__proxy_method__(method, *args, &block)
|
__proxy_method__(method, *args, &block)
|
||||||
end
|
end
|
||||||
|
|
|
@ -76,11 +76,17 @@ describe Docile do
|
||||||
@iv1 = 'iv1'; outer { @iv1.should == 'iv1' }
|
@iv1 = 'iv1'; outer { @iv1.should == 'iv1' }
|
||||||
end
|
end
|
||||||
|
|
||||||
#it "should find instance variable from block definition in inner dsl scope" do
|
it "should write instance variable assigned in block into outer dsl scope" do
|
||||||
# @iv2 = 'iv2'; outer { inner {
|
@iv1 = 'foo'; outer { @iv1 = 'bar' }; @iv1.should == 'bar'
|
||||||
# @iv2.should == 'iv2'
|
end
|
||||||
# } }
|
|
||||||
#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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue