2009-11-07 14:23:21 -05:00
|
|
|
require "active_support/inflector/methods"
|
|
|
|
|
2009-01-27 19:54:01 -05:00
|
|
|
module ActionDispatch
|
2008-12-01 14:48:47 -05:00
|
|
|
class MiddlewareStack < Array
|
|
|
|
class Middleware
|
2008-12-18 13:56:18 -05:00
|
|
|
attr_reader :args, :block
|
2008-12-01 14:48:47 -05:00
|
|
|
|
|
|
|
def initialize(klass, *args, &block)
|
2010-05-29 16:29:14 -04:00
|
|
|
@klass, @args, @block = klass, args, block
|
2008-12-01 14:48:47 -05:00
|
|
|
end
|
|
|
|
|
2008-12-18 13:56:18 -05:00
|
|
|
def klass
|
2010-05-29 16:29:14 -04:00
|
|
|
return @klass if @klass.respond_to?(:new)
|
|
|
|
@klass = ActiveSupport::Inflector.constantize(@klass.to_s)
|
2008-12-18 13:00:54 -05:00
|
|
|
end
|
|
|
|
|
2008-12-01 14:48:47 -05:00
|
|
|
def ==(middleware)
|
|
|
|
case middleware
|
|
|
|
when Middleware
|
|
|
|
klass == middleware.klass
|
|
|
|
when Class
|
|
|
|
klass == middleware
|
|
|
|
else
|
2010-01-25 16:59:08 -05:00
|
|
|
if lazy_compare?(@klass) && lazy_compare?(middleware)
|
|
|
|
normalize(@klass) == normalize(middleware)
|
|
|
|
else
|
2010-05-29 16:29:14 -04:00
|
|
|
klass.name == normalize(middleware.to_s)
|
2010-01-25 16:59:08 -05:00
|
|
|
end
|
2008-12-01 14:48:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
2010-01-19 23:25:10 -05:00
|
|
|
klass.to_s
|
2008-12-01 14:48:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def build(app)
|
2010-05-29 16:29:14 -04:00
|
|
|
klass.new(app, *args, &block)
|
2008-12-01 14:48:47 -05:00
|
|
|
end
|
2009-02-20 11:50:21 -05:00
|
|
|
|
2010-05-29 16:29:14 -04:00
|
|
|
private
|
2010-01-25 16:59:08 -05:00
|
|
|
|
2010-05-29 16:29:14 -04:00
|
|
|
def lazy_compare?(object)
|
|
|
|
object.is_a?(String) || object.is_a?(Symbol)
|
|
|
|
end
|
2010-01-25 16:59:08 -05:00
|
|
|
|
2010-05-29 16:29:14 -04:00
|
|
|
def normalize(object)
|
|
|
|
object.to_s.strip.sub(/^::/, '')
|
|
|
|
end
|
2008-12-01 14:48:47 -05:00
|
|
|
end
|
|
|
|
|
2008-12-15 17:33:31 -05:00
|
|
|
def initialize(*args, &block)
|
|
|
|
super(*args)
|
|
|
|
block.call(self) if block_given?
|
|
|
|
end
|
|
|
|
|
2009-01-20 21:19:52 -05:00
|
|
|
def insert(index, *args, &block)
|
2009-01-04 00:02:29 -05:00
|
|
|
index = self.index(index) unless index.is_a?(Integer)
|
2010-05-30 09:53:14 -04:00
|
|
|
middleware = self.class::Middleware.new(*args, &block)
|
2009-01-20 21:19:52 -05:00
|
|
|
super(index, middleware)
|
2009-01-04 00:02:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :insert_before, :insert
|
|
|
|
|
2009-01-20 21:19:52 -05:00
|
|
|
def insert_after(index, *args, &block)
|
2009-12-28 23:18:14 -05:00
|
|
|
i = index.is_a?(Integer) ? index : self.index(index)
|
|
|
|
raise "No such middleware to insert after: #{index.inspect}" unless i
|
|
|
|
insert(i + 1, *args, &block)
|
2009-01-20 21:19:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def swap(target, *args, &block)
|
|
|
|
insert_before(target, *args, &block)
|
|
|
|
delete(target)
|
2009-01-04 00:02:29 -05:00
|
|
|
end
|
|
|
|
|
2008-12-01 14:48:47 -05:00
|
|
|
def use(*args, &block)
|
2010-05-30 09:53:14 -04:00
|
|
|
middleware = self.class::Middleware.new(*args, &block)
|
2008-12-15 17:33:31 -05:00
|
|
|
push(middleware)
|
2008-12-01 14:48:47 -05:00
|
|
|
end
|
|
|
|
|
2008-12-18 13:00:54 -05:00
|
|
|
def active
|
2010-05-29 16:29:14 -04:00
|
|
|
ActiveSupport::Deprecation.warn "All middlewares in the chaing are active since the laziness " <<
|
|
|
|
"was removed from the middleware stack", caller
|
2008-12-18 13:00:54 -05:00
|
|
|
end
|
|
|
|
|
2010-05-30 09:53:14 -04:00
|
|
|
def build(app = nil, &block)
|
|
|
|
app ||= block
|
2010-03-08 19:49:47 -05:00
|
|
|
raise "MiddlewareStack#build requires an app" unless app
|
2010-05-29 16:29:14 -04:00
|
|
|
reverse.inject(app) { |a, e| e.build(a) }
|
2008-12-01 14:48:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|