跳至内容 跳至搜索

Action Dispatch MiddlewareStack

在指南中了解更多关于 Rails 中间件栈 的信息。

命名空间
方法
#
B
D
E
I
L
M
N
S
U
包含的模块

属性

[RW] middlewares

类公共方法

new(*args)

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

实例公共方法

[](i)

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

build(app = nil, &block)

# 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

delete(target)

从中间件栈中删除一个中间件。

返回不包含已删除项的中间件数组,如果未找到目标,则返回 nil。

# File actionpack/lib/action_dispatch/middleware/stack.rb, line 131
def delete(target)
  middlewares.reject! { |m| m.name == target.name }
end

delete!(target)

从中间件栈中删除一个中间件。

返回不包含已删除项的中间件数组,如果未找到目标,则引发 RuntimeError

# File actionpack/lib/action_dispatch/middleware/stack.rb, line 139
def delete!(target)
  delete(target) || (raise "No such middleware to remove: #{target.inspect}")
end

each(&block)

# File actionpack/lib/action_dispatch/middleware/stack.rb, line 81
def each(&block)
  @middlewares.each(&block)
end

initialize_copy(other)

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

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

也称为:insert_before
# 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

insert_after(index, *args, &block)

# 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

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

别名:insert

last()

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

move(target, source)

也称为:move_before
# 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

move_after(target, source)

# 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

move_before(target, source)

别名:move

size()

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

swap(target, *args, &block)

# 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

unshift(klass, *args, &block)

# File actionpack/lib/action_dispatch/middleware/stack.rb, line 97
def unshift(klass, *args, &block)
  middlewares.unshift(build_middleware(klass, args, block))
end

use(klass, *args, &block)

# File actionpack/lib/action_dispatch/middleware/stack.rb, line 161
def use(klass, *args, &block)
  middlewares.push(build_middleware(klass, args, block))
end