Class: Cuprum::Rails::Controllers::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/cuprum/rails/controllers/middleware.rb

Overview

A configured middleware option for a controller.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command:, except: [], only: []) ⇒ Middleware

Returns a new instance of Middleware.

Parameters:

  • command (Cuprum::Command)

    The middleware command to wrap the action or actions.

  • except (Array<Symbol>) (defaults to: [])

    A list of action names; the middleware will not be applied to actions on the list.

  • only (Array<Symbol>) (defaults to: [])

    A list of action names; the middleware will be applied only to actions on the list.



16
17
18
19
20
# File 'lib/cuprum/rails/controllers/middleware.rb', line 16

def initialize(command:, except: [], only: [])
  @command = command
  @except  = Set.new(except.map(&:intern))
  @only    = Set.new(only.map(&:intern))
end

Instance Attribute Details

#commandCuprum::Middleware (readonly)

Returns the middleware command to wrap the action or actions.

Returns:

  • (Cuprum::Middleware)

    the middleware command to wrap the action or actions.



24
25
26
# File 'lib/cuprum/rails/controllers/middleware.rb', line 24

def command
  @command
end

#exceptArray<Symbol> (readonly)

Returns a list of action names; the middleware will not be applied to actions on the list.

Returns:

  • (Array<Symbol>)

    a list of action names; the middleware will not be applied to actions on the list.



28
29
30
# File 'lib/cuprum/rails/controllers/middleware.rb', line 28

def except
  @except
end

#onlyArray<Symbol> (readonly)

Returns a list of action names; the middleware will be applied only to actions on the list.

Returns:

  • (Array<Symbol>)

    a list of action names; the middleware will be applied only to actions on the list.



32
33
34
# File 'lib/cuprum/rails/controllers/middleware.rb', line 32

def only
  @only
end

Instance Method Details

#==(other) ⇒ Object



35
36
37
38
39
40
# File 'lib/cuprum/rails/controllers/middleware.rb', line 35

def ==(other)
  other.is_a?(Cuprum::Rails::Controllers::Middleware) &&
    other.command == command &&
    other.except  == except &&
    other.only    == only
end

#matches?(action_name) ⇒ true, false Also known as: match?

Checks if the middleware will be applied to the named action.

If the middleware defines any :except actions, returns false if the action name is in the set. If the middleware defines any :only actions, returns false unless the action name is in the set. Otherwise, returns true.

Parameters:

  • action_name (Symbol)

    The name of the action.

Returns:

  • (true, false)

    whether the middleware will be applied.



51
52
53
54
55
56
# File 'lib/cuprum/rails/controllers/middleware.rb', line 51

def matches?(action_name)
  return false unless except.empty? || except.exclude?(action_name)
  return false unless only.empty?   || only.include?(action_name)

  true
end