validates_identity 0.3.1 → 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: f932507ebec9a185edee94cebdf2f6ca7ff6b8142ade9243d197654fd36201d2
4
- data.tar.gz: b484245e99d1429b632d46b729deb0683042da47ec87b59010d6f6970e952a89
3
+ metadata.gz: de2775d07796e202d6cc7ecdb166bed0233950b9b3439ae5da5e3fc7b89e3957
4
+ data.tar.gz: 5cf2f4410a8326c2a0f20c3471540453da92ccbc4617ad171ded389da84f145c
5
5
  SHA512:
6
- metadata.gz: c9945d350a984600a079149e126fbf6a234110f5d1faba2415fd87d92dcb1d52704d018442e149047e2285bcfbaee15a15a07e32dafa055c99e13f4948cd45e4
7
- data.tar.gz: 2c1cab2c85a42f3d0494838624b3d794a3730a70dd937ef33c1bd2c1b23955c12b06837a9ed36c55b498730a6fce01148b8c738333da621f0d6c12c3dd497d2d
6
+ metadata.gz: 1bbfa6dc9295dbd292dc37eabedc189129eeb03fa891a8e33ca8cb496213fc99b4defaa0f56b00fdac3f8c1e379f235104ca44281c91aafc84a37e0a46bfcffd
7
+ data.tar.gz: 4d359891c62c0c8025528c28b69685d828cb2cb3b809d6bde18cf833fba6514ad152fe3522e79d9e866f17beab939b2eb12113024b96f573c4c1067a48e3061c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## [0.4.0] - 2024-02-25
2
+
3
+ ### Added
4
+
5
+ - Shoulda Custom Matcher
6
+
7
+ ## [0.3.2] - 2024-02-25
8
+
9
+ ### Fixed
10
+
11
+ - Missing Api Exposure
12
+
1
13
  ## [0.3.1] - 2024-02-25
2
14
 
3
15
  ### Fixed
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validates_identity (0.3.1)
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
@@ -19,8 +19,14 @@ class ValidatesIdentity
19
19
 
20
20
  return false if validator_class.nil?
21
21
 
22
- validator = validator_class.new(value)
23
- validator.valid?
22
+ @validator = validator_class.new(value)
23
+ @validator.valid?
24
+ end
25
+
26
+ def formatted
27
+ return if @validator.nil?
28
+
29
+ @validator.formatted
24
30
  end
25
31
 
26
32
  private
@@ -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.1'
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.1
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