From 27eddbb332c4adb533e0699e4b1532c3bd4de222 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 7 Aug 2015 12:03:13 -0700 Subject: simplify the Middleware constructor We should do the hard work outside the constructor. Also fix the tests to not directly construct middleware objects, but to go through the stack object. --- actionpack/lib/action_dispatch/middleware/stack.rb | 23 ++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb index 87e627319e..b39a502d77 100644 --- a/actionpack/lib/action_dispatch/middleware/stack.rb +++ b/actionpack/lib/action_dispatch/middleware/stack.rb @@ -6,7 +6,7 @@ module ActionDispatch class Middleware attr_reader :args, :block, :name, :classcache - def initialize(klass_or_name, *args, &block) + def initialize(klass_or_name, args, block) @klass = nil if klass_or_name.respond_to?(:name) @@ -75,19 +75,17 @@ module ActionDispatch middlewares[i] end - def unshift(*args, &block) - middleware = self.class::Middleware.new(*args, &block) - middlewares.unshift(middleware) + def unshift(klass, *args, &block) + middlewares.unshift(build_middleware(klass, args, block)) end def initialize_copy(other) self.middlewares = other.middlewares.dup end - def insert(index, *args, &block) + def insert(index, klass, *args, &block) index = assert_index(index, :before) - middleware = self.class::Middleware.new(*args, &block) - middlewares.insert(index, middleware) + middlewares.insert(index, build_middleware(klass, args, block)) end alias_method :insert_before, :insert @@ -107,9 +105,8 @@ module ActionDispatch middlewares.delete target end - def use(*args, &block) - middleware = self.class::Middleware.new(*args, &block) - middlewares.push(middleware) + def use(klass, *args, &block) + middlewares.push(build_middleware(klass, args, block)) end def build(app = Proc.new) @@ -123,5 +120,11 @@ module ActionDispatch raise "No such middleware to insert #{where}: #{index.inspect}" unless i i end + + private + + def build_middleware(klass, args, block) + Middleware.new(klass, args, block) + end end end -- cgit v1.2.3