class ActionDispatch::MiddlewareStack
Action Dispatch MiddlewareStack
Read more about Rails middleware stack in the guides.
Inherits From
-
module
Enumerable
Attributes
[RW] | middlewares |
Public class methods
Source code GitHub
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 76
def initialize(*args)
@middlewares = []
yield(self) if block_given?
end
Public instance methods
Source code GitHub
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 93
def [](i)
middlewares[i]
end
Source code GitHub
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 166
def build(app = nil, &block)
instrumenting = ActiveSupport::Notifications.notifier.listening?(InstrumentationProxy::EVENT_NAME)
middlewares.freeze.reverse.inject(app || block) do |a, e|
if instrumenting
e.build_instrumented(a)
else
e.build(a)
end
end
end
Deletes a middleware from the middleware stack.
Returns the array of middlewares not including the deleted item, or returns nil if the target is not found.
Source code GitHub
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 131
def delete(target)
middlewares.reject! { |m| m.name == target.name }
end
Deletes a middleware from the middleware stack.
Returns the array of middlewares not including the deleted item, or raises RuntimeError
if the target is not found.
Source code GitHub
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 139
def delete!(target)
delete(target) || (raise "No such middleware to remove: #{target.inspect}")
end
Source code GitHub
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 81
def each(&block)
@middlewares.each(&block)
end
Source code GitHub
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 102
def initialize_copy(other)
self.middlewares = other.middlewares.dup
end
Also aliased as:
insert_before
.
Source code GitHub
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 106
def insert(index, klass, *args, &block)
index = assert_index(index, :before)
middlewares.insert(index, build_middleware(klass, args, block))
end
Source code GitHub
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 114
def insert_after(index, *args, &block)
index = assert_index(index, :after)
insert(index + 1, *args, &block)
end
Alias for:
insert
.
Source code GitHub
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 89
def last
middlewares.last
end
Also aliased as:
move_before
.
Source code GitHub
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 143
def move(target, source)
source_index = assert_index(source, :before)
source_middleware = middlewares.delete_at(source_index)
target_index = assert_index(target, :before)
middlewares.insert(target_index, source_middleware)
end
Source code GitHub
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 153
def move_after(target, source)
source_index = assert_index(source, :after)
source_middleware = middlewares.delete_at(source_index)
target_index = assert_index(target, :after)
middlewares.insert(target_index + 1, source_middleware)
end
Alias for:
move
.
Source code GitHub
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 85
def size
middlewares.size
end
Source code GitHub
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 120
def swap(target, *args, &block)
index = assert_index(target, :before)
insert(index, *args, &block)
middlewares.delete_at(index + 1)
end
Source code GitHub
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 97
def unshift(klass, *args, &block)
middlewares.unshift(build_middleware(klass, args, block))
end
Source code GitHub
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 161
def use(klass, *args, &block)
middlewares.push(build_middleware(klass, args, block))
end