Fix for when DSL object is also the block's context

* fix #29

* added RSpec example for issue #29

* fixed a typo on the descripton comment
This commit is contained in:
Taichi Ishitani 2018-05-24 11:16:32 +09:00 committed by Marc Siegel
parent 6cb966f98e
commit fcbd6841ab
2 changed files with 33 additions and 0 deletions

View File

@ -30,6 +30,7 @@ module Docile
end
block_context.instance_variables.each do |ivar|
next unless proxy_context.instance_variables.include?(ivar)
value_from_dsl_proxy = proxy_context.instance_variable_get(ivar)
block_context.instance_variable_set(ivar, value_from_dsl_proxy)
end

View File

@ -349,6 +349,38 @@ describe Docile do
end
context "when DSL context object is the same as the block's context object" do
class DSLContextSameAsBlockContext
def foo(v = nil)
@foo = v if v
@foo
end
def bar(v = nil)
@bar = v if v
@bar
end
def dsl_eval(block)
Docile.dsl_eval(self, &block)
end
def dsl_eval_string(string)
block = binding.eval("proc { #{string} }")
dsl_eval(block)
end
end
let(:dsl) { DSLContextSameAsBlockContext.new }
it "calls DSL methods and sets instance variables on the DSL conetxt object" do
dsl.dsl_eval_string('foo 0; bar 1')
expect(dsl.foo).to eq(0)
expect(dsl.bar).to eq(1)
end
end
end
describe ".dsl_eval_with_block_return" do