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, #exclude?, #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



115
116
117
118
119
# File 'actionpack/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) ⇒ Object



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

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



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

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

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



87
88
89
90
91
# File 'actionpack/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

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



95
96
97
98
# File 'actionpack/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

#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



100
101
102
103
104
# File 'actionpack/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) ⇒ Object



78
79
80
81
# File 'actionpack/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) ⇒ Object



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

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