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 +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +1 -1
- data/README.md +24 -0
- data/lib/shoulda/matchers/active_model/require_a_valid_identity_matcher.rb +52 -0
- data/lib/validates_identity/shoulda_matchers.rb +23 -0
- data/lib/validates_identity/version.rb +2 -2
- data/lib/validates_identity.rb +1 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de2775d07796e202d6cc7ecdb166bed0233950b9b3439ae5da5e3fc7b89e3957
|
4
|
+
data.tar.gz: 5cf2f4410a8326c2a0f20c3471540453da92ccbc4617ad171ded389da84f145c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1bbfa6dc9295dbd292dc37eabedc189129eeb03fa891a8e33ca8cb496213fc99b4defaa0f56b00fdac3f8c1e379f235104ca44281c91aafc84a37e0a46bfcffd
|
7
|
+
data.tar.gz: 4d359891c62c0c8025528c28b69685d828cb2cb3b809d6bde18cf833fba6514ad152fe3522e79d9e866f17beab939b2eb12113024b96f573c4c1067a48e3061c
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
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
|
data/lib/validates_identity.rb
CHANGED
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.
|
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
|