Skip to content

Commit 881019f

Browse files
committed
If the block takes an argument, don't modify the scope.
1 parent eaa4702 commit 881019f

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/protocol/http/middleware/builder.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ def to_app
3030
def self.build(&block)
3131
builder = Builder.new
3232

33-
builder.instance_eval(&block)
33+
if block_given?
34+
if block.arity == 0
35+
builder.instance_exec(&block)
36+
else
37+
yield builder
38+
end
39+
end
3440

3541
return builder.to_app
3642
end

test/protocol/http/middleware/builder.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,16 @@
2929

3030
expect(app).to be_a(Protocol::HTTP::Middleware)
3131
end
32+
33+
it "provides the builder as an argument" do
34+
current_self = self
35+
36+
app = Protocol::HTTP::Middleware.build do |builder|
37+
builder.use Protocol::HTTP::Middleware
38+
39+
expect(self).to be_equal(current_self)
40+
end
41+
42+
expect(app).to be_a(Protocol::HTTP::Middleware)
43+
end
3244
end

0 commit comments

Comments
 (0)