pundit-matchers 3.0.1 → 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: deb38560b2277878e0a13f7f7a6fb510e257c5f84b625a387424e08cb5e8708b
4
- data.tar.gz: '084e0cb2b93b0e27f1bb710666b3d9c1aadd9fac71ffef0ca178b025a531cafe'
3
+ metadata.gz: 141abfa7ebc01c34b65ed5231dd3b66d7453d49a0797f2d8f8cf3c79058f4e4c
4
+ data.tar.gz: bd9508d4a9f95d60e4239ab0f96a9f210ca8137c907f846ecefcc35a062fcfae
5
5
  SHA512:
6
- metadata.gz: b219dfc3fbc0a1283f0c87627b5615947f97b449ce299d65be5106e0a46b06d9a0ed40f1f86a84830cc567a74d75da9b597136f1c563cebcce3752eb2a581d45
7
- data.tar.gz: ce6b7c6a259a6a4e22f673985c3154e334f87acf2f856b9340f97dd62d5d56945cfc16191bfb503bbce24b12c002394e0994554a29376bbe82849a690926fb19
6
+ metadata.gz: 7f9c778c08f01c7ffe3060480d55570d18c37358db1c87016bf66608028690764b8a4ac53419ef162fcce71b345dd05c8a617aa1f0598cbeb6d554033b0c1c0a
7
+ data.tar.gz: f68b63d3155d1fd806c3dd061e5e330fcf656a14bf51ada7d25d856e161b5324822913114328e01d5bd76c99d2198e69a415fdfb21137793bab6ec5815ec741a
@@ -8,6 +8,8 @@ module Pundit
8
8
  class AttributesMatcher < BaseMatcher
9
9
  # Error message to be raised when no attributes are specified.
10
10
  ARGUMENTS_REQUIRED_ERROR = 'At least one attribute must be specified'
11
+ # Error message to be raised when only one attribute may be specified.
12
+ ONE_ARGUMENT_REQUIRED_ERROR = 'Only one attribute may be specified'
11
13
 
12
14
  # Initializes a new instance of the AttributesMatcher class.
13
15
  #
@@ -29,6 +31,17 @@ module Pundit
29
31
  self
30
32
  end
31
33
 
34
+ # Ensures that only one attribute is specified.
35
+ #
36
+ # @raise [ArgumentError] If more than one attribute is specified.
37
+ #
38
+ # @return [AttributesMatcher] The object itself.
39
+ def ensure_single_attribute!
40
+ raise ArgumentError, ONE_ARGUMENT_REQUIRED_ERROR if expected_attributes.size > 1
41
+
42
+ self
43
+ end
44
+
32
45
  private
33
46
 
34
47
  attr_reader :expected_attributes, :options
@@ -6,6 +6,8 @@ module Pundit
6
6
  module Matchers
7
7
  # This is the base class for all matchers in the Pundit Matchers library.
8
8
  class BaseMatcher
9
+ include RSpec::Matchers::Composable
10
+
9
11
  # Error message when an ambiguous negated matcher is used.
10
12
  AMBIGUOUS_NEGATED_MATCHER_ERROR = <<~MSG
11
13
  `expect().not_to %<name>s` is not supported since it creates ambiguity.
@@ -152,15 +152,32 @@ module Pundit
152
152
  ForbidOnlyActionsMatcher.new(*actions)
153
153
  end
154
154
 
155
+ # Creates a matcher that tests if the policy permits mass assignment of an attribute.
156
+ #
157
+ # @param [String, Symbol, Hash] attribute the attribute to be tested.
158
+ # @return [PermitAttributesMatcher] the matcher object.
159
+ def permit_attribute(attribute)
160
+ PermitAttributesMatcher.new(attribute).ensure_single_attribute!
161
+ end
162
+
163
+ RSpec::Matchers.define_negated_matcher :forbid_attribute, :permit_attribute, &NEGATED_DESCRIPTION
164
+
155
165
  # Creates a matcher that tests if the policy permits mass assignment of a set of attributes.
156
166
  #
157
167
  # @param [Array<String, Symbol, Hash>] attributes the attributes to be tested.
158
168
  # @return [PermitAttributesMatcher] the matcher object.
159
- def permit_mass_assignment_of(*attributes)
169
+ def permit_attributes(*attributes)
160
170
  PermitAttributesMatcher.new(*attributes)
161
171
  end
162
172
 
163
- RSpec::Matchers.define_negated_matcher :forbid_mass_assignment_of, :permit_mass_assignment_of, &NEGATED_DESCRIPTION
173
+ RSpec::Matchers.define_negated_matcher :forbid_attributes, :permit_attributes, &NEGATED_DESCRIPTION
174
+
175
+ # @!macro [attach] RSpec::Matchers.alias_matcher
176
+ # @!method $1
177
+ #
178
+ # An alias matcher for {$2}.
179
+ RSpec::Matchers.alias_matcher :permit_mass_assignment_of, :permit_attributes
180
+ RSpec::Matchers.alias_matcher :forbid_mass_assignment_of, :forbid_attributes
164
181
  end
165
182
  end
166
183
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pundit-matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Alley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-16 00:00:00.000000000 Z
11
+ date: 2023-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-core