Class: Pundit::Matchers::ActionsMatcher

Inherits:
BaseMatcher show all
Defined in:
lib/pundit/matchers/actions_matcher.rb

Overview

This is the base action matcher class. Matchers related to actions should inherit from this class.

Constant Summary collapse

ACTIONS_NOT_IMPLEMENTED_ERROR =

Error message when actions are not implemented in a policy.

"'%<policy>s' does not implement %<actions>s"
ARGUMENTS_REQUIRED_ERROR =

Error message when at least one action must be specified.

'At least one action must be specified'
ONE_ARGUMENT_REQUIRED_ERROR =

Error message when only one action may be specified.

'Only one action may be specified'

Constants inherited from BaseMatcher

BaseMatcher::AMBIGUOUS_NEGATED_MATCHER_ERROR

Instance Method Summary collapse

Constructor Details

#initialize(*expected_actions) ⇒ ActionsMatcher

Initializes a new instance of the ActionsMatcher class.

Parameters:

  • expected_actions (Array<String, Symbol>)

    The expected actions to be checked.

Raises:

  • (ArgumentError)

    If no actions are specified.



21
22
23
24
25
26
# File 'lib/pundit/matchers/actions_matcher.rb', line 21

def initialize(*expected_actions)
  raise ArgumentError, ARGUMENTS_REQUIRED_ERROR if expected_actions.empty?

  super()
  @expected_actions = expected_actions.flatten.map(&:to_sym).sort
end

Instance Method Details

#ensure_single_action!ActionsMatcher

Ensures that only one action is specified.

Returns:

Raises:

  • (ArgumentError)

    If more than one action is specified.



33
34
35
36
37
# File 'lib/pundit/matchers/actions_matcher.rb', line 33

def ensure_single_action!
  raise ArgumentError, ONE_ARGUMENT_REQUIRED_ERROR if expected_actions.size > 1

  self
end