Skip to content

Commit

Permalink
Add the #unshift method to the middleware stack
Browse files Browse the repository at this point in the history
The docs suggest that the middleware stack is an Array, so I've added
the unshift method to it. Originally I added some more Array methods,
but it was agreed that they lacked usecases.
  • Loading branch information
Rich Healey committed May 18, 2012
1 parent b23ac93 commit 793205c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions actionpack/lib/action_dispatch/middleware/stack.rb
Expand Up @@ -75,6 +75,11 @@ def [](i)
middlewares[i]
end

def unshift(*args, &block)
middleware = self.class::Middleware.new(*args, &block)
middlewares.unshift(middleware)
end

def initialize_copy(other)
self.middlewares = other.middlewares.dup
end
Expand Down
9 changes: 7 additions & 2 deletions actionpack/test/dispatch/middleware_stack_test.rb
Expand Up @@ -45,7 +45,7 @@ def setup
assert_equal BazMiddleware, @stack.last.klass
assert_equal([true, {:foo => "bar"}], @stack.last.args)
end

test "use should push middleware class with block arguments onto the stack" do
proc = Proc.new {}
assert_difference "@stack.size" do
Expand All @@ -54,7 +54,7 @@ def setup
assert_equal BlockMiddleware, @stack.last.klass
assert_equal proc, @stack.last.block
end

test "insert inserts middleware at the integer index" do
@stack.insert(1, BazMiddleware)
assert_equal BazMiddleware, @stack[1].klass
Expand Down Expand Up @@ -87,6 +87,11 @@ def setup
assert_equal FooMiddleware, @stack[0].klass
end

test "unshift adds a new middleware at the beginning of the stack" do
@stack.unshift :"MiddlewareStackTest::BazMiddleware"
assert_equal BazMiddleware, @stack.first.klass
end

test "raise an error on invalid index" do
assert_raise RuntimeError do
@stack.insert("HiyaMiddleware", BazMiddleware)
Expand Down

0 comments on commit 793205c

Please sign in to comment.