Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add some common Array methods to the middleware stack #6372

Merged
merged 1 commit into from

2 participants

@richo

The docs suggest that the middleware stack is an Array, and yet some
common Array methods do not work (#unshift #shift #pop and #push) are
the most common array operations that are not implemented that I can
think of.

To this end, I have implemented them simply by delegating to the
middlewares Array directly

( This commit also fixes two whitespace issues, I will rebase them out if they shouldn't get to piggyback)

@josevalim
Owner

Thanks for the PR but I believe we should fix the docs to mention it is an Enumerable. I can't see a use case for pop or shift, and use is the same as push. So I don't believe this is giving us anything.

@richo

The reason I started adding this was purely for #unshift which I tried to use after reading the docs, and which doesn't exist.

I agree about the others, I was really just fleshing out the implementation to reflect the docs.

Would you accept a doc fix + a carved down PR for #unshift which I believe has a usecase?

@josevalim
Owner

Would you accept a doc fix + a carved down PR for #unshift which I believe has a usecase?

Yes. :)

@richo richo Add the #unshift method to the middleware stack
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.
793205c
@richo

I've rebased the commit and removed everything except #unshift.

There's still a whitespace fix piggybacking though :)

I'll update the docs also.

@josevalim josevalim merged commit a6a8957 into rails:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 18, 2012
  1. @richo

    Add the #unshift method to the middleware stack

    richo authored
    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.
This page is out of date. Refresh to see the latest.
View
5 actionpack/lib/action_dispatch/middleware/stack.rb
@@ -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
View
9 actionpack/test/dispatch/middleware_stack_test.rb
@@ -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
@@ -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
@@ -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)
Something went wrong with that request. Please try again.