123456789_123456789_123456789_123456789_123456789_

Class: ActionDispatch::MiddlewareStack

Relationships & Source Files
Namespace Children
Classes:
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: actionpack/lib/action_dispatch/middleware/stack.rb

Overview

Read more about [Rails middleware stack](guides.rubyonrails.org/rails_on_rack.html#action-dispatcher-middleware-stack) in the guides.

Class Method Summary

Instance Attribute Summary

::Enumerable - Included

#many?

Returns true if the enumerable has more than 1 element.

Instance Method Summary

::Enumerable - Included

#compact_blank

Returns a new ::Array without the blank items.

#exclude?

The negative of the Enumerable#include?.

#excluding

Returns a copy of the enumerable excluding the specified elements.

#in_order_of

Returns a new ::Array where the order has been set to that provided in the series, based on the key of the objects in the original enumerable.

#including

Returns a new array that includes the passed elements.

#index_by

Convert an enumerable to a hash, using the block result as the key and the element as the value.

#index_with

Convert an enumerable to a hash, using the element as the key and the block result as the value.

#maximum

Calculates the maximum from the extracted elements.

#minimum

Calculates the minimum from the extracted elements.

#pick

Extract the given key from the first element in the enumerable.

#pluck

Extract the given key from each element in the enumerable.

#sole

Returns the sole item in the enumerable.

#without
#as_json

::ActiveSupport::EnumerableCoreExt::Constants - Included

Constructor Details

.new(*args) {|_self| ... } ⇒ MiddlewareStack

Yields:

  • (_self)

Yield Parameters:

  • _self (MiddlewareStack)

    the object that the method was called on

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/middleware/stack.rb', line 76

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

Instance Attribute Details

#middlewares (rw)

[ GitHub ]

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

attr_accessor :middlewares

Instance Method Details

#[](i)

[ GitHub ]

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

def [](i)
  middlewares[i]
end

#assert_index(index, where) (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/middleware/stack.rb', line 178

def assert_index(index, where)
  i = index.is_a?(Integer) ? index : index_of(index)
  raise "No such middleware to insert #{where}: #{index.inspect}" unless i
  i
end

#build(app = nil, &block)

[ 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

#build_middleware(klass, args, block) (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/middleware/stack.rb', line 184

def build_middleware(klass, args, block)
  Middleware.new(klass, args, block)
end

#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.

[ GitHub ]

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

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

#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.

[ 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

#each(&block)

[ GitHub ]

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

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

#index_of(klass) (private)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/middleware/stack.rb', line 188

def index_of(klass)
  middlewares.index do |m|
    m.name == klass.name
  end
end

#initialize_copy(other)

[ GitHub ]

  
# 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) Also known as: #insert_before

[ 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

#insert_after(index, *args, &block)

[ 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

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

Alias for #insert.

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/middleware/stack.rb', line 112

alias_method :insert_before, :insert

#last

[ GitHub ]

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

def last
  middlewares.last
end

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

[ 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

#move_after(target, source)

[ 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

#move_before(target, source)

Alias for #move.

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/middleware/stack.rb', line 151

alias_method :move_before, :move

#size

[ GitHub ]

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

def size
  middlewares.size
end

#swap(target, *args, &block)

[ 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

#unshift(klass, *args, &block)

[ GitHub ]

  
# 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)

[ GitHub ]

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

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