mirror of
https://github.com/ms-ati/docile
synced 2023-03-27 23:21:52 -04:00
support instance variables from block context in one-level dsl
This commit is contained in:
parent
c04587a03f
commit
d7fd1a9450
3 changed files with 17 additions and 3 deletions
|
@ -18,7 +18,13 @@ module Docile
|
||||||
# @return [Object] the given DSL object
|
# @return [Object] the given DSL object
|
||||||
def dsl_eval(dsl, &block)
|
def dsl_eval(dsl, &block)
|
||||||
block_context = eval("self", block.binding)
|
block_context = eval("self", block.binding)
|
||||||
FallbackContextProxy.new(dsl, block_context).instance_eval(&block)
|
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
|
||||||
|
proxy_context.instance_eval(&block)
|
||||||
|
ensure
|
||||||
|
block_context.instance_variables.each { |ivar| block_context.instance_variable_set(ivar, proxy_context.instance_variable_get(ivar)) }
|
||||||
|
end
|
||||||
dsl
|
dsl
|
||||||
end
|
end
|
||||||
module_function :dsl_eval
|
module_function :dsl_eval
|
||||||
|
|
|
@ -2,7 +2,9 @@ require 'set'
|
||||||
|
|
||||||
module Docile
|
module Docile
|
||||||
class FallbackContextProxy
|
class FallbackContextProxy
|
||||||
BASIC_METHODS = Set[:==, :equal?, :"!", :"!=", :instance_eval, :object_id, :__send__, :__id__]
|
BASIC_METHODS = Set[:==, :equal?, :"!", :"!=",
|
||||||
|
:instance_eval, :instance_variable_get, :instance_variable_set,
|
||||||
|
:object_id, :__send__, :__id__]
|
||||||
|
|
||||||
instance_methods.each do |method|
|
instance_methods.each do |method|
|
||||||
unless BASIC_METHODS.include?(method.to_sym)
|
unless BASIC_METHODS.include?(method.to_sym)
|
||||||
|
|
|
@ -72,8 +72,14 @@ describe Docile do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "instance variables" do
|
context "instance variables" do
|
||||||
|
it "should find instance variable from block definition in outer dsl scope" do
|
||||||
|
@iv1 = 'iv1'; outer { @iv1.should == 'iv1' }
|
||||||
|
end
|
||||||
|
|
||||||
#it "should find instance variable from block definition in inner dsl scope" do
|
#it "should find instance variable from block definition in inner dsl scope" do
|
||||||
# @iv1 = 'iv1'; outer { inner { @iv1.should == 'iv1' } }
|
# @iv2 = 'iv2'; outer { inner {
|
||||||
|
# @iv2.should == 'iv2'
|
||||||
|
# } }
|
||||||
#end
|
#end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue