validates_identity 0.3.2 → 0.4.0

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: 314621c0cd80707a6ee1f7a776307f767e167cab1e63e63c4cbe0eeeb404d92a
4
- data.tar.gz: 3e7c4f2a9406ec4ade871fe38599451bb18440466c2f17829d440cff6f282cbd
3
+ metadata.gz: de2775d07796e202d6cc7ecdb166bed0233950b9b3439ae5da5e3fc7b89e3957
4
+ data.tar.gz: 5cf2f4410a8326c2a0f20c3471540453da92ccbc4617ad171ded389da84f145c
5
5
  SHA512:
6
- metadata.gz: f0c7bf51aaaeb802d817ad33d30881d2f8fcc46091915f0bba6938692d0356342fbd92755378713572dd6eb8dcf9439d60bb1eeedcfbb118e3d601df0194433c
7
- data.tar.gz: aa22f5dd5e57e3df142c183de1f993450d7fb6e338ad46325b6c8b12d733f080561a6ffd3c8d6fd9435f0029cbd02541aabd1013e0bf9f8c3fa90bf29efdcc09
6
+ metadata.gz: 1bbfa6dc9295dbd292dc37eabedc189129eeb03fa891a8e33ca8cb496213fc99b4defaa0f56b00fdac3f8c1e379f235104ca44281c91aafc84a37e0a46bfcffd
7
+ data.tar.gz: 4d359891c62c0c8025528c28b69685d828cb2cb3b809d6bde18cf833fba6514ad152fe3522e79d9e866f17beab939b2eb12113024b96f573c4c1067a48e3061c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## [0.4.0] - 2024-02-25
2
+
3
+ ### Added
4
+
5
+ - Shoulda Custom Matcher
6
+
1
7
  ## [0.3.2] - 2024-02-25
2
8
 
3
9
  ### Fixed
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validates_identity (0.3.2)
4
+ validates_identity (0.4.0)
5
5
  activemodel
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -64,6 +64,30 @@ In case of a legacy system where keys were already defined and differ from the o
64
64
  ValidatesIdentity.register_identity_type_alias('LegacyIdentity', 'CustomIdentity')
65
65
  ```
66
66
 
67
+ ### Custom scenarios for matching
68
+
69
+ When adding a new validator, cases of success and error can be added through and API so that the `Matcher` will test against them
70
+
71
+ ```ruby
72
+ ValidatesIdentity::ShouldaMatchers.register_allowed_values('CustomIdentity', ['123456789', '123.456.789'])
73
+ ValidatesIdentity::ShouldaMatchers.register_disallowed_values('CustomIdentity', ['12345679', '12.456.789'])
74
+ ```
75
+
76
+ ## Testing
77
+
78
+ Require matcher in your `spec_helper` or `rails_helper` file:
79
+
80
+ ```ruby
81
+ require 'shoulda/matchers/active_model/require_a_valid_identity_matcher'
82
+ ```
83
+
84
+ Use in your tests:
85
+
86
+ ```ruby
87
+ it { is_expected.to require_a_valid_identity } # It will test the attributes :identity and :identity_type by default
88
+ it { is_expected.to require_a_valid_identity(:id, :my_type) }
89
+ ```
90
+
67
91
  ## Development
68
92
 
69
93
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'shoulda-matchers'
4
+
5
+ module Shoulda
6
+ module Matchers
7
+ module ActiveModel
8
+ def require_a_valid_identity(identity = :identity, identity_type = :identity_type)
9
+ RequireAValidIdentityMatcher.new(identity, identity_type)
10
+ end
11
+
12
+ class RequireAValidIdentityMatcher < ValidationMatcher
13
+ def initialize(identity, identity_type)
14
+ super(identity)
15
+ @identity_type = identity_type
16
+ end
17
+
18
+ def description
19
+ 'requires a valid identity'
20
+ end
21
+
22
+ def failure_message
23
+ 'does not require a valid identity'
24
+ end
25
+
26
+ def matches?(subject)
27
+ super(subject)
28
+
29
+ result = []
30
+
31
+ ValidatesIdentity::ShouldaMatchers.allowed_values.each do |identity_type, values|
32
+ subject.send("#{@identity_type}=", identity_type)
33
+
34
+ values.each do |value|
35
+ result << allows_value_of(value)
36
+ end
37
+ end
38
+
39
+ ValidatesIdentity::ShouldaMatchers.disallowed_values.each do |identity_type, values|
40
+ subject.send("#{@identity_type}=", identity_type)
41
+
42
+ values.each do |value|
43
+ result << disallows_value_of(value)
44
+ end
45
+ end
46
+
47
+ result.inject(:&)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ValidatesIdentity
4
+ class ShouldaMatchers
5
+ class << self
6
+ def allowed_values
7
+ @allowed_values ||= {}
8
+ end
9
+
10
+ def disallowed_values
11
+ @disallowed_values ||= {}
12
+ end
13
+
14
+ def register_allowed_values(identity_type_acronym, values = [])
15
+ allowed_values[identity_type_acronym] = values
16
+ end
17
+
18
+ def register_disallowed_values(identity_type_acronym, values = [])
19
+ disallowed_values[identity_type_acronym] = values
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ValidatesIdentity
4
- VERSION = '0.3.2'
3
+ class ValidatesIdentity
4
+ VERSION = '0.4.0'
5
5
  end
@@ -5,6 +5,7 @@ require 'identity_validator'
5
5
 
6
6
  class ValidatesIdentity
7
7
  autoload :Identity, 'validates_identity/identity'
8
+ autoload :ShouldaMatchers, 'validates_identity/shoulda_matchers'
8
9
 
9
10
  class << self
10
11
  private
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_identity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo Ribeiro
@@ -43,8 +43,10 @@ files:
43
43
  - README.md
44
44
  - Rakefile
45
45
  - lib/identity_validator.rb
46
+ - lib/shoulda/matchers/active_model/require_a_valid_identity_matcher.rb
46
47
  - lib/validates_identity.rb
47
48
  - lib/validates_identity/identity.rb
49
+ - lib/validates_identity/shoulda_matchers.rb
48
50
  - lib/validates_identity/version.rb
49
51
  - validates_identity.gemspec
50
52
  homepage: https://github.com/plribeiro3000/validates_identity