Skip to content

Commit

Permalink
Fix issue #33 DSL object is replaced when #dsl_eval is nested (PR #34)
Browse files Browse the repository at this point in the history
* add examples for #33

* fix #33

* added workaround RSpec's issue
rspec/rspec-expectations#1111

Thanks @taichi-ishitani !
  • Loading branch information
taichi-ishitani authored and ms-ati committed Jun 12, 2019
1 parent 67517e7 commit 9362d1a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions docile.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "rake", "~> 10.5" if on_less_than_1_9_3? # Pin compatible rake on old rubies, see: https://github.com/travis-ci/travis.rb/issues/380
s.add_development_dependency "rake", "< 11.0" unless on_less_than_1_9_3? # See http://stackoverflow.com/questions/35893584/nomethoderror-undefined-method-last-comment-after-upgrading-to-rake-11
s.add_development_dependency "rspec", "~> 3.0"
s.add_development_dependency "rspec-expectations", "!= 3.8.3" # Workaround for RSpec's issue, see: https://github.com/rspec/rspec-expectations/issues/1111

# Run code coverage where possible - not on Rubinius
unless on_rubinius?
Expand Down
2 changes: 1 addition & 1 deletion lib/docile/fallback_context_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def initialize(receiver, fallback)
singleton_class.
send(:define_method, :method_missing) do |method, *args, &block|
m = method.to_sym
if !NON_FALLBACK_METHODS.include?(m) && receiver.respond_to?(m)
if !NON_FALLBACK_METHODS.include?(m) && !fallback.respond_to?(m) && receiver.respond_to?(m)
receiver.__send__(method.to_sym, *args, &block)
else
super(method, *args, &block)
Expand Down
13 changes: 9 additions & 4 deletions spec/docile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ def execute_dsl_against_builder_and_call_build
class InnerDSL
def initialize; @b = "b"; end
attr_accessor :b
def c; "inner c"; end
end

class OuterDSL
def initialize; @a = "a"; end
attr_accessor :a

def c; "outer c"; end

def inner(&block)
Docile.dsl_eval(InnerDSL.new, &block)
end
Expand Down Expand Up @@ -226,24 +229,26 @@ def push_element
context "method lookup" do
it "finds method of outer dsl in outer dsl scope" do
outer { expect(a).to eq("a") }
outer { inner {}; expect(c).to eq("outer c") }
end

it "finds method of inner dsl in inner dsl scope" do
outer { inner { expect(b).to eq("b") } }
outer { inner { expect(c).to eq("inner c") } }
end

it "finds method of outer dsl in inner dsl scope" do
outer { inner { expect(a).to eq("a") } }
end

it "finds method of block's context in outer dsl scope" do
def c; "c"; end
outer { expect(c).to eq("c") }
def d; "d"; end
outer { expect(d).to eq("d") }
end

it "finds method of block's context in inner dsl scope" do
def c; "c"; end
outer { inner { expect(c).to eq("c") } }
def d; "d"; end
outer { inner { expect(d).to eq("d") } }
end

it "finds method of outer dsl in preference to block context" do
Expand Down

0 comments on commit 9362d1a

Please sign in to comment.