Class: Pancake::Middleware::StackMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/pancake/middleware.rb

Overview

StackMiddleware manages the definition of the middleware stack for a given class. It’s instances are responsible for the definition of a single piece of middleware, and the class is responsible for specifying the full stack for a given class.

When Pancake::Middleware extends a class, an inner class is created in that class called StackMiddleware. That StackMiddleware class inherits from Pancake::Middleware::StackMiddleware.

This is then set is an inheritable inner class on the extended class, such that when it is inherited, the StackMiddleware class is inherited to an inner class of the same name on the child.

Examples:

The setup when Pancake::Middleware is extended

MyClass.extend Pancake::Middleware
# sets up

class MyClass
  class StackMiddleware < Pancake::Middleware::StackMiddleware; end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, stack, options = {}) ⇒ StackMiddleware

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of StackMiddleware.

Parameters:

  • name (Object)

    a name for this middleware definition. Usually a symbol, but could be the class.

  • stack (Object)

    the stack owner of this middleware.

  • options (Hash) (defaults to: {})

    an options hash. Provide labels for this middleware.

Options Hash (options):

  • :before (Object)

    A middleware name to add this middleware before

  • :after (Object)

    A middleware name to add this middleware after

See Also:

  • Pancake::Middleware.stack_labels

Author:

  • Daniel Neighman



240
241
242
# File 'lib/pancake/middleware.rb', line 240

def initialize(name, stack, options = {})
  @name, @stack, @options = name, stack, options
end

Instance Attribute Details

#argsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



157
158
159
# File 'lib/pancake/middleware.rb', line 157

def args
  @args
end

#blockObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



157
158
159
# File 'lib/pancake/middleware.rb', line 157

def block
  @block
end

#middlewareObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



155
156
157
# File 'lib/pancake/middleware.rb', line 155

def middleware
  @middleware
end

#nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



155
156
157
# File 'lib/pancake/middleware.rb', line 155

def name
  @name
end

#optionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



157
158
159
# File 'lib/pancake/middleware.rb', line 157

def options
  @options
end

#stackObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



157
158
159
# File 'lib/pancake/middleware.rb', line 157

def stack
  @stack
end

Class Method Details

.[](name) ⇒ StackMiddleware

Provides access to a named middleware

Parameters:

  • name (Object)

    The name of the defined middleware

Returns:

Author:

  • Daniel Neighman

Since:

  • 0.1.0



217
218
219
# File 'lib/pancake/middleware.rb', line 217

def [](name)
  _mwares[name]
end

.map_middleware(name) ⇒ Array<StackMiddleware>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Map the middleware for a given <name>ed middleware. Applies the before and after groups of middlewares

Parameters:

  • name (Object)

    The name of the middleware to map the before and after groups to

Returns:

  • (Array<StackMiddleware>)

    Provides an array of StackMiddleware instances in the array [<before :foo>, <:foo>, <after :foo>]

Author:

  • Daniel Neighman

Since:

  • 0.1.0



198
199
200
201
202
203
204
205
206
# File 'lib/pancake/middleware.rb', line 198

def map_middleware(name)
  result = []
  _before[name] ||= []
  _after[name]  ||= []
  result << _before[name].map{|n| map_middleware(n)}
  result << _mwares[name]
  result << _after[name].map{|n| map_middleware(n)}
  result.flatten
end

.middlewaresArray<StackMiddleware>

Get the middleware list for this StackMiddleware

Returns:

  • (Array<StackMiddleware>)

    An array of the middleware definitions to use in the order that they should be applied Takes into account all :before, :after settings and only constructs the stack where the labels are applied

See Also:

  • for a description on stack labels

Author:

  • Daniel Neighman

Since:

  • 0.1.0



183
184
185
186
187
# File 'lib/pancake/middleware.rb', line 183

def middlewares
  _central_mwares.map do |name|
    map_middleware(name)
  end.flatten
end

.reset!Object

Resets this stack middlware. Useful for specs



165
166
167
168
169
170
# File 'lib/pancake/middleware.rb', line 165

def reset!
  _central_mwares.clear
  _mwares.clear
  _before.clear
  _after.clear
end

.use(mware, *_args, &block) ⇒ Object



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

def use(mware, *_args, &block)
  new(mware).use(mware, *_args, &block)
end

Instance Method Details

#[](name) ⇒ Object

Provides access to a named middleware

See Also:

Author:

  • Daniel Neighman

Since:

  • 0.1.0



227
228
229
# File 'lib/pancake/middleware.rb', line 227

def [](name)
  self.class._mwares[name]
end

#delete!Object

Delete this middleware from the current stack

Author:

  • Daniel Neighman

Since:

  • 0.1.0



249
250
251
252
253
254
255
# File 'lib/pancake/middleware.rb', line 249

def delete!
  self.class._mwares.delete(name)
  self.class._before.delete(name)
  self.class._after.delete(name)
  self.class._central_mwares.delete(name)
  self
end

#dupObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



286
287
288
289
290
291
# File 'lib/pancake/middleware.rb', line 286

def dup
  result = super
  result.args = result.args.map{|element| element.dup}
  result.options = result.options.dup
  result
end

#use(mware, *_args) { ... } ⇒ Object

Specify the actual middleware definition to use

Parameters:

  • mware (Class)

    A Middleware class to use. This should be a class of Middleware which conforms to the Rack spec

  • config (Hash)

    A configuration hash to give to the middleware class on initialization

Yields:

  • The block is passed to the middleware on initialization

See Also:

  • Pancake::Middleware.use

Author:

  • Daniel Neighman

Since:

  • 0.1.0



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/pancake/middleware.rb', line 267

def use(mware, *_args, &block)
  @middleware, @args, @block = mware, _args, block
  @name = @middleware if name.nil?
  if options[:before]
    raise "#{options[:before].inspect} middleware is not defined for this stack" unless self.class._mwares.keys.include?(options[:before])
    self.class._before[options[:before]] ||= []
    self.class._before[options[:before]] << name
  elsif options[:after]
    raise "#{options[:after].inspect} middleware is not defined for this stack" unless self.class._mwares.keys.include?(options[:after])
    self.class._after[options[:after]] ||= []
    self.class._after[options[:after]] << name
  else
    self.class._central_mwares << name unless self.class._central_mwares.include?(name)
  end
  self.class._mwares[name] = self
  self
end