From fcbd6841ab651f48d0057c42a693a9467a81f801 Mon Sep 17 00:00:00 2001 From: Taichi Ishitani Date: Thu, 24 May 2018 11:16:32 +0900 Subject: [PATCH] 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 --- lib/docile/execution.rb | 1 + spec/docile_spec.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/lib/docile/execution.rb b/lib/docile/execution.rb index 63f79a1..a9326f7 100644 --- a/lib/docile/execution.rb +++ b/lib/docile/execution.rb @@ -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 diff --git a/spec/docile_spec.rb b/spec/docile_spec.rb index 47768b0..d621d02 100644 --- a/spec/docile_spec.rb +++ b/spec/docile_spec.rb @@ -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