Action Dispatch MiddlewareStack

Read more about Rails middleware stack in the guides.

Namespace

Class

Methods

Included Modules

Attributes

[RW] middlewares

Class Public methods

new(*args)

📝 Source code
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 75
    def initialize(*args)
      @middlewares = []
      yield(self) if block_given?
    end
🔎 See on GitHub

Instance Public methods

[](i)

📝 Source code
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 92
    def [](i)
      middlewares[i]
    end
🔎 See on GitHub

build(app = nil, &block)

📝 Source code
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 165
    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
🔎 See on GitHub

delete(target)

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
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 130
    def delete(target)
      middlewares.reject! { |m| m.name == target.name }
    end
🔎 See on GitHub

delete!(target)

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
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 138
    def delete!(target)
      delete(target) || (raise "No such middleware to remove: #{target.inspect}")
    end
🔎 See on GitHub

each(&block)

📝 Source code
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 80
    def each(&block)
      @middlewares.each(&block)
    end
🔎 See on GitHub

initialize_copy(other)

📝 Source code
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 101
    def initialize_copy(other)
      self.middlewares = other.middlewares.dup
    end
🔎 See on GitHub

insert(index, klass, *args, &block)

Also aliased as: insert_before
📝 Source code
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 105
    def insert(index, klass, *args, &block)
      index = assert_index(index, :before)
      middlewares.insert(index, build_middleware(klass, args, block))
    end
🔎 See on GitHub

insert_after(index, *args, &block)

📝 Source code
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 113
    def insert_after(index, *args, &block)
      index = assert_index(index, :after)
      insert(index + 1, *args, &block)
    end
🔎 See on GitHub

insert_before(index, klass, *args, &block)

Alias for: insert

last()

📝 Source code
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 88
    def last
      middlewares.last
    end
🔎 See on GitHub

move(target, source)

Also aliased as: move_before
📝 Source code
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 142
    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
🔎 See on GitHub

move_after(target, source)

📝 Source code
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 152
    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
🔎 See on GitHub

move_before(target, source)

Alias for: move

size()

📝 Source code
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 84
    def size
      middlewares.size
    end
🔎 See on GitHub

swap(target, *args, &block)

📝 Source code
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 119
    def swap(target, *args, &block)
      index = assert_index(target, :before)
      insert(index, *args, &block)
      middlewares.delete_at(index + 1)
    end
🔎 See on GitHub

unshift(klass, *args, &block)

📝 Source code
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 96
    def unshift(klass, *args, &block)
      middlewares.unshift(build_middleware(klass, args, block))
    end
🔎 See on GitHub

use(klass, *args, &block)

📝 Source code
# File actionpack/lib/action_dispatch/middleware/stack.rb, line 160
    def use(klass, *args, &block)
      middlewares.push(build_middleware(klass, args, block))
    end
🔎 See on GitHub