aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-05-29 22:29:14 +0200
committerMikel Lindsaar <raasdnil@gmail.com>2010-06-03 23:32:10 +1000
commit2740943634fe151fb3fb87e2af7881a93f5dd6b5 (patch)
tree4916366ccf706c49f7e2082a406a2a90471dff79 /actionpack
parent27939bd753b79c8205d1d3c1cdc7db6205642302 (diff)
downloadrails-2740943634fe151fb3fb87e2af7881a93f5dd6b5.tar.gz
rails-2740943634fe151fb3fb87e2af7881a93f5dd6b5.tar.bz2
rails-2740943634fe151fb3fb87e2af7881a93f5dd6b5.zip
Remove the laziness from the middleware stack.
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_dispatch/middleware/stack.rb72
-rw-r--r--actionpack/test/dispatch/middleware_stack_test.rb19
2 files changed, 17 insertions, 74 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb
index 5c5362ce4a..0e5ab507df 100644
--- a/actionpack/lib/action_dispatch/middleware/stack.rb
+++ b/actionpack/lib/action_dispatch/middleware/stack.rb
@@ -3,49 +3,15 @@ require "active_support/inflector/methods"
module ActionDispatch
class MiddlewareStack < Array
class Middleware
- def self.new(klass, *args, &block)
- if klass.is_a?(self)
- klass
- else
- super
- end
- end
-
attr_reader :args, :block
def initialize(klass, *args, &block)
- @klass = klass
-
- options = args.extract_options!
- if options.has_key?(:if)
- @conditional = options.delete(:if)
- else
- @conditional = true
- end
- args << options unless options.empty?
-
- @args = args
- @block = block
+ @klass, @args, @block = klass, args, block
end
def klass
- if @klass.respond_to?(:new)
- @klass
- elsif @klass.respond_to?(:call)
- @klass.call
- else
- ActiveSupport::Inflector.constantize(@klass.to_s)
- end
- end
-
- def active?
- return false unless klass
-
- if @conditional.respond_to?(:call)
- @conditional.call
- else
- @conditional
- end
+ return @klass if @klass.respond_to?(:new)
+ @klass = ActiveSupport::Inflector.constantize(@klass.to_s)
end
def ==(middleware)
@@ -58,7 +24,7 @@ module ActionDispatch
if lazy_compare?(@klass) && lazy_compare?(middleware)
normalize(@klass) == normalize(middleware)
else
- klass.name == middleware.to_s
+ klass.name == normalize(middleware.to_s)
end
end
end
@@ -68,25 +34,18 @@ module ActionDispatch
end
def build(app)
- if block
- klass.new(app, *build_args, &block)
- else
- klass.new(app, *build_args)
- end
+ klass.new(app, *args, &block)
end
- private
- def lazy_compare?(object)
- object.is_a?(String) || object.is_a?(Symbol)
- end
+ private
- def normalize(object)
- object.to_s.strip.sub(/^::/, '')
- end
+ def lazy_compare?(object)
+ object.is_a?(String) || object.is_a?(Symbol)
+ end
- def build_args
- Array(args).map { |arg| arg.respond_to?(:call) ? arg.call : arg }
- end
+ def normalize(object)
+ object.to_s.strip.sub(/^::/, '')
+ end
end
def initialize(*args, &block)
@@ -119,15 +78,14 @@ module ActionDispatch
end
def active
- find_all { |middleware| middleware.active? }
+ ActiveSupport::Deprecation.warn "All middlewares in the chaing are active since the laziness " <<
+ "was removed from the middleware stack", caller
end
def build(app = nil, &blk)
app ||= blk
-
raise "MiddlewareStack#build requires an app" unless app
-
- active.reverse.inject(app) { |a, e| e.build(a) }
+ reverse.inject(app) { |a, e| e.build(a) }
end
end
end
diff --git a/actionpack/test/dispatch/middleware_stack_test.rb b/actionpack/test/dispatch/middleware_stack_test.rb
index 7cf6365af3..170c5b8565 100644
--- a/actionpack/test/dispatch/middleware_stack_test.rb
+++ b/actionpack/test/dispatch/middleware_stack_test.rb
@@ -66,29 +66,14 @@ class MiddlewareStackTest < ActiveSupport::TestCase
assert_equal BazMiddleware, @stack[0].klass
end
- test "active returns all only enabled middleware" do
- assert_no_difference "@stack.active.size" do
- assert_difference "@stack.size" do
- @stack.use BazMiddleware, :if => lambda { false }
- end
- end
- end
-
test "lazy evaluates middleware class" do
assert_difference "@stack.size" do
- @stack.use lambda { BazMiddleware }
+ @stack.use "MiddlewareStackTest::BazMiddleware"
end
assert_equal BazMiddleware, @stack.last.klass
end
- test "lazy evaluates middleware arguments" do
- assert_difference "@stack.size" do
- @stack.use BazMiddleware, lambda { :foo }
- end
- assert_equal [:foo], @stack.last.send(:build_args)
- end
-
- test "lazy compares so unloaded constants can be loaded" do
+ test "lazy compares so unloaded constants are not loaded" do
@stack.use "UnknownMiddleware"
@stack.use :"MiddlewareStackTest::BazMiddleware"
assert @stack.include?("::MiddlewareStackTest::BazMiddleware")