Class: ActionDispatch::MiddlewareStack

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

Direct Known Subclasses

ActionController::MiddlewareStack

Defined Under Namespace

Classes: Middleware

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#as_json, #each_with_object, #exclude?, #group_by, #index_by, #many?, #sum

Constructor Details

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

Returns a new instance of MiddlewareStack.

Yields:

  • (_self)

Yield Parameters:



57
58
59
60
# File 'actionpack/lib/action_dispatch/middleware/stack.rb', line 57

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

Instance Attribute Details

#middlewaresObject

Returns the value of attribute middlewares



55
56
57
# File 'actionpack/lib/action_dispatch/middleware/stack.rb', line 55

def middlewares
  @middlewares
end

Instance Method Details

#[](i) ⇒ Object



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

def [](i)
  middlewares[i]
end

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



110
111
112
113
114
# File 'actionpack/lib/action_dispatch/middleware/stack.rb', line 110

def build(app = nil, &block)
  app ||= block
  raise "MiddlewareStack#build requires an app" unless app
  middlewares.reverse.inject(app) { |a, e| e.build(a) }
end

#delete(target) ⇒ Object



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

def delete(target)
  middlewares.delete target
end

#eachObject



62
63
64
# File 'actionpack/lib/action_dispatch/middleware/stack.rb', line 62

def each
  @middlewares.each { |x| yield x }
end

#initialize_copy(other) ⇒ Object



78
79
80
# File 'actionpack/lib/action_dispatch/middleware/stack.rb', line 78

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

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



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

def insert(index, *args, &block)
  index = assert_index(index, :before)
  middleware = self.class::Middleware.new(*args, &block)
  middlewares.insert(index, middleware)
end

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



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

def insert_after(index, *args, &block)
  index = assert_index(index, :after)
  insert(index + 1, *args, &block)
end

#lastObject



70
71
72
# File 'actionpack/lib/action_dispatch/middleware/stack.rb', line 70

def last
  middlewares.last
end

#sizeObject



66
67
68
# File 'actionpack/lib/action_dispatch/middleware/stack.rb', line 66

def size
  middlewares.size
end

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



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

def swap(target, *args, &block)
  index = assert_index(target, :before)
  insert(index, *args, &block)
  middlewares.delete_at(index + 1)
end

#use(*args, &block) ⇒ Object



105
106
107
108
# File 'actionpack/lib/action_dispatch/middleware/stack.rb', line 105

def use(*args, &block)
  middleware = self.class::Middleware.new(*args, &block)
  middlewares.push(middleware)
end