class ActionDispatch::MiddlewareStack - actionpack-4.2.4 Documentation

# class ActionDispatch::MiddlewareStack

### Attributes

middlewares[RW]

### Public Class Methods

new(*args) { |self| ... } click to toggle source

    # File lib/action_dispatch/middleware/stack.rb, line 57
    def initialize(*args)
      @middlewares = []
      yield(self) if block_given?
    end

### Public Instance Methods

[](i) click to toggle source

    # File lib/action_dispatch/middleware/stack.rb, line 74
    def [](i)
      middlewares[i]
    end

build(app = nil, &block) click to toggle source

    # File lib/action_dispatch/middleware/stack.rb, line 115
    def build(app = nil, &block)
      app ||= block
      raise "MiddlewareStack#build requires an app" unless app
      middlewares.freeze.reverse.inject(app) { |a, e| e.build(a) }
    end

delete(target) click to toggle source

    # File lib/action_dispatch/middleware/stack.rb, line 106
    def delete(target)
      middlewares.delete target
    end

each() { |x| ... } click to toggle source

    # File lib/action_dispatch/middleware/stack.rb, line 62
    def each
      @middlewares.each { |x| yield x }
    end

initialize_copy(other) click to toggle source

    # File lib/action_dispatch/middleware/stack.rb, line 83
    def initialize_copy(other)
      self.middlewares = other.middlewares.dup
    end

insert(index, *args, &block) click to toggle source

    # File lib/action_dispatch/middleware/stack.rb, line 87
    def insert(index, *args, &block)
      index = assert_index(index, :before)
      middleware = self.class::Middleware.new(*args, &block)
      middlewares.insert(index, middleware)
    end

Also aliased as: insert_before
insert_after(index, *args, &block) click to toggle source

    # File lib/action_dispatch/middleware/stack.rb, line 95
    def insert_after(index, *args, &block)
      index = assert_index(index, :after)
      insert(index + 1, *args, &block)
    end

insert_before(index, *args, &block)
Alias for: insert
last() click to toggle source

    # File lib/action_dispatch/middleware/stack.rb, line 70
    def last
      middlewares.last
    end

size() click to toggle source

    # File lib/action_dispatch/middleware/stack.rb, line 66
    def size
      middlewares.size
    end

swap(target, *args, &block) click to toggle source

    # File lib/action_dispatch/middleware/stack.rb, line 100
    def swap(target, *args, &block)
      index = assert_index(target, :before)
      insert(index, *args, &block)
      middlewares.delete_at(index + 1)
    end

unshift(*args, &block) click to toggle source

    # File lib/action_dispatch/middleware/stack.rb, line 78
    def unshift(*args, &block)
      middleware = self.class::Middleware.new(*args, &block)
      middlewares.unshift(middleware)
    end

use(*args, &block) click to toggle source

    # File lib/action_dispatch/middleware/stack.rb, line 110
    def use(*args, &block)
      middleware = self.class::Middleware.new(*args, &block)
      middlewares.push(middleware)
    end

### Protected Instance Methods

assert_index(index, where) click to toggle source

    # File lib/action_dispatch/middleware/stack.rb, line 123
    def assert_index(index, where)
      i = index.is_a?(Integer) ? index : middlewares.index(index)
      raise "No such middleware to insert #{where}: #{index.inspect}" unless i
      i
    end