2009-11-07 14:23:21 -05:00
|
|
|
require "active_support/inflector/methods"
|
2010-06-22 20:38:44 -04:00
|
|
|
require "active_support/dependencies"
|
2009-11-07 14:23:21 -05:00
|
|
|
|
2009-01-27 19:54:01 -05:00
|
|
|
module ActionDispatch
|
2011-03-01 13:14:01 -05:00
|
|
|
class MiddlewareStack
|
2008-12-01 14:48:47 -05:00
|
|
|
class Middleware
|
2011-03-02 13:11:28 -05:00
|
|
|
attr_reader :args, :block, :name, :classcache
|
2008-12-01 14:48:47 -05:00
|
|
|
|
2010-06-05 23:34:30 -04:00
|
|
|
def initialize(klass_or_name, *args, &block)
|
2011-03-01 20:20:10 -05:00
|
|
|
@klass = nil
|
|
|
|
|
|
|
|
if klass_or_name.respond_to?(:name)
|
|
|
|
@klass = klass_or_name
|
|
|
|
@name = @klass.name
|
2011-03-01 20:43:45 -05:00
|
|
|
else
|
|
|
|
@name = klass_or_name.to_s
|
2011-03-01 20:20:10 -05:00
|
|
|
end
|
|
|
|
|
2011-03-02 13:11:28 -05:00
|
|
|
@classcache = ActiveSupport::Dependencies::Reference
|
2010-06-05 23:34:30 -04:00
|
|
|
@args, @block = args, block
|
2008-12-01 14:48:47 -05:00
|
|
|
end
|
|
|
|
|
2008-12-18 13:56:18 -05:00
|
|
|
def klass
|
2011-03-02 13:11:28 -05:00
|
|
|
@klass || classcache[@name]
|
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
|
2011-03-01 20:20:10 -05:00
|
|
|
normalize(@name) == normalize(middleware)
|
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 normalize(object)
|
|
|
|
object.to_s.strip.sub(/^::/, '')
|
|
|
|
end
|
2008-12-01 14:48:47 -05:00
|
|
|
end
|
|
|
|
|
2011-03-01 13:14:01 -05:00
|
|
|
include Enumerable
|
|
|
|
|
|
|
|
attr_accessor :middlewares
|
2010-09-29 05:18:31 -04:00
|
|
|
|
2010-09-28 14:10:55 -04:00
|
|
|
def initialize(*args)
|
2011-03-01 13:14:01 -05:00
|
|
|
@middlewares = []
|
2010-09-28 14:10:55 -04:00
|
|
|
yield(self) if block_given?
|
2008-12-15 17:33:31 -05:00
|
|
|
end
|
|
|
|
|
2011-03-01 13:14:01 -05:00
|
|
|
def each
|
|
|
|
@middlewares.each { |x| yield x }
|
|
|
|
end
|
|
|
|
|
|
|
|
def size
|
|
|
|
middlewares.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def last
|
|
|
|
middlewares.last
|
|
|
|
end
|
|
|
|
|
|
|
|
def [](i)
|
|
|
|
middlewares[i]
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_copy(other)
|
|
|
|
self.middlewares = other.middlewares.dup
|
|
|
|
end
|
|
|
|
|
2009-01-20 21:19:52 -05:00
|
|
|
def insert(index, *args, &block)
|
2010-07-21 09:17:04 -04:00
|
|
|
index = assert_index(index, :before)
|
2010-05-30 09:53:14 -04:00
|
|
|
middleware = self.class::Middleware.new(*args, &block)
|
2011-03-01 13:14:01 -05:00
|
|
|
middlewares.insert(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)
|
2010-07-21 09:17:04 -04:00
|
|
|
index = assert_index(index, :after)
|
|
|
|
insert(index + 1, *args, &block)
|
2009-01-20 21:19:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def swap(target, *args, &block)
|
2012-02-03 22:08:39 -05:00
|
|
|
index = assert_index(target, :before)
|
|
|
|
insert(index, *args, &block)
|
|
|
|
middlewares.delete_at(index + 1)
|
2009-01-04 00:02:29 -05:00
|
|
|
end
|
|
|
|
|
2011-03-01 13:14:01 -05:00
|
|
|
def delete(target)
|
|
|
|
middlewares.delete target
|
|
|
|
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)
|
2011-03-01 13:14:01 -05:00
|
|
|
middlewares.push(middleware)
|
2008-12-01 14:48:47 -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
|
2011-03-01 13:14:01 -05:00
|
|
|
middlewares.reverse.inject(app) { |a, e| e.build(a) }
|
2008-12-01 14:48:47 -05:00
|
|
|
end
|
2010-07-21 09:17:04 -04:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def assert_index(index, where)
|
2011-03-01 13:14:01 -05:00
|
|
|
i = index.is_a?(Integer) ? index : middlewares.index(index)
|
2010-07-21 09:17:04 -04:00
|
|
|
raise "No such middleware to insert #{where}: #{index.inspect}" unless i
|
|
|
|
i
|
|
|
|
end
|
2008-12-01 14:48:47 -05:00
|
|
|
end
|
|
|
|
end
|