Skip to content

Commit a4bd24a

Browse files
committed
Allow a user to insert a middleware at a specific location in the stack.
This commit adds a documented but not implemented feature to allow a user to insert a middleware at a specific point in the stack. It does this by extending the Grape::DSL::Middleware class with a new method `.insert` which delegates down to the already existing methods in existance on the Grape::Middleware::Stack class.
1 parent 5b20f2a commit a4bd24a

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/grape/dsl/middleware.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ def use(middleware_class, *args, &block)
2121
namespace_stackable(:middleware, arr)
2222
end
2323

24+
def insert(*args, &block)
25+
arr = [:insert, *args]
26+
arr << block if block_given?
27+
28+
namespace_stackable(:middleware, arr)
29+
end
30+
2431
def insert_before(*args, &block)
2532
arr = [:insert_before, *args]
2633
arr << block if block_given?

spec/grape/dsl/middleware_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ class Dummy
2222
end
2323
end
2424

25+
describe '.insert' do
26+
it 'adds a middleware with the right operation' do
27+
expect(subject).to receive(:namespace_stackable).with(:middleware, [:insert, 0, :arg1, proc])
28+
29+
subject.insert 0, :arg1, &proc
30+
end
31+
end
32+
2533
describe '.insert_before' do
2634
it 'adds a middleware with the right operation' do
2735
expect(subject).to receive(:namespace_stackable).with(:middleware, [:insert_before, foo_middleware, :arg1, proc])

0 commit comments

Comments
 (0)