Class: ActionDispatch::MiddlewareStack

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/action_dispatch/middleware/stack.rb

Overview

Action Dispatch MiddlewareStack

Read more about Rails middleware stack in the guides.

Direct Known Subclasses

ActionController::MiddlewareStack

Defined Under Namespace

Classes: InstrumentationProxy, Middleware

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) {|_self| ... } ⇒ MiddlewareStack

Returns a new instance of MiddlewareStack.

Yields:

  • (_self)

Yield Parameters:



75
76
77
78
# File 'lib/action_dispatch/middleware/stack.rb', line 75

def initialize(*args)
  @middlewares = []
  yield(self) if block_given?
end

Instance Attribute Details

#middlewaresObject

Returns the value of attribute middlewares.



73
74
75
# File 'lib/action_dispatch/middleware/stack.rb', line 73

def middlewares
  @middlewares
end

Instance Method Details

#[](i) ⇒ Object



92
93
94
# File 'lib/action_dispatch/middleware/stack.rb', line 92

def [](i)
  middlewares[i]
end

#build(app = nil, &block) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File '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

#delete(target) ⇒ Object

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.



130
131
132
# File 'lib/action_dispatch/middleware/stack.rb', line 130

def delete(target)
  middlewares.reject! { |m| m.name == target.name }
end

#delete!(target) ⇒ Object

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.



138
139
140
# File 'lib/action_dispatch/middleware/stack.rb', line 138

def delete!(target)
  delete(target) || (raise "No such middleware to remove: #{target.inspect}")
end

#each(&block) ⇒ Object



80
81
82
# File 'lib/action_dispatch/middleware/stack.rb', line 80

def each(&block)
  @middlewares.each(&block)
end

#initialize_copy(other) ⇒ Object



101
102
103
# File 'lib/action_dispatch/middleware/stack.rb', line 101

def initialize_copy(other)
  self.middlewares = other.middlewares.dup
end

#insert(index, klass, *args, &block) ⇒ Object Also known as: insert_before



105
106
107
108
# File '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

#insert_after(index, *args, &block) ⇒ Object



113
114
115
116
# File '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

#lastObject



88
89
90
# File 'lib/action_dispatch/middleware/stack.rb', line 88

def last
  middlewares.last
end

#move(target, source) ⇒ Object Also known as: move_before



142
143
144
145
146
147
148
# File '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

#move_after(target, source) ⇒ Object



152
153
154
155
156
157
158
# File '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

#sizeObject



84
85
86
# File 'lib/action_dispatch/middleware/stack.rb', line 84

def size
  middlewares.size
end

#swap(target, *args, &block) ⇒ Object



119
120
121
122
123
# File '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

#unshift(klass, *args, &block) ⇒ Object



96
97
98
# File 'lib/action_dispatch/middleware/stack.rb', line 96

def unshift(klass, *args, &block)
  middlewares.unshift(build_middleware(klass, args, block))
end

#use(klass, *args, &block) ⇒ Object



160
161
162
# File 'lib/action_dispatch/middleware/stack.rb', line 160

def use(klass, *args, &block)
  middlewares.push(build_middleware(klass, args, block))
end