lib/pundit/matchers/attributes_matcher.rb in pundit-matchers-3.0.1 vs lib/pundit/matchers/attributes_matcher.rb in pundit-matchers-3.1.0
- old
+ new
@@ -6,10 +6,12 @@
module Matchers
# The AttributesMatcher class is used to test whether a Pundit policy allows or denies access to certain attributes.
class AttributesMatcher < BaseMatcher
# Error message to be raised when no attributes are specified.
ARGUMENTS_REQUIRED_ERROR = 'At least one attribute must be specified'
+ # Error message to be raised when only one attribute may be specified.
+ ONE_ARGUMENT_REQUIRED_ERROR = 'Only one attribute may be specified'
# Initializes a new instance of the AttributesMatcher class.
#
# @param expected_attributes [Array<String, Symbol, Hash>] The list of attributes to be tested.
def initialize(*expected_attributes)
@@ -24,9 +26,20 @@
#
# @param action [Symbol, String] The action to be tested.
# @return [AttributesMatcher] The current instance of the AttributesMatcher class.
def for_action(action)
@options[:action] = action
+ self
+ end
+
+ # Ensures that only one attribute is specified.
+ #
+ # @raise [ArgumentError] If more than one attribute is specified.
+ #
+ # @return [AttributesMatcher] The object itself.
+ def ensure_single_attribute!
+ raise ArgumentError, ONE_ARGUMENT_REQUIRED_ERROR if expected_attributes.size > 1
+
self
end
private