Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from May 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/docile/execution.rb
Expand Up @@ -30,6 +30,7 @@ def exec_in_proxy_context(dsl, proxy_type, *args, &block)
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
Expand Down
32 changes: 32 additions & 0 deletions spec/docile_spec.rb
Expand Up @@ -349,6 +349,38 @@ def x(y) ; "Got a #{y}"; end

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
Expand Down