validates_identity 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +5 -1
- data/Gemfile.lock +1 -1
- data/README.md +17 -1
- data/lib/identity_validator.rb +11 -9
- data/lib/validates_identity/identity.rb +4 -3
- data/lib/validates_identity/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3848016930f18f09b27ec31333622b53df51786d31c3eb612fe92da34c9f640a
|
4
|
+
data.tar.gz: c07d435462ce6caa0e3c4d1611af1aa2cfa8f06956a331696773845fafb6a02f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 539ce96f3cc65ca74b12ea9b83a6aaa568e3c15bb178e945c1224893092e6741f5abf6f69db65fe989163c2d528a808f34f3b9704b800b5a550e179f4a50bcea
|
7
|
+
data.tar.gz: ab5c1fe930362622f2efd4c928f71246aef44db216f36fd795492b5fbe4a7cdc3b11c27c295c95ba60f853ff46501b4f037dd379b2b1cf5fe681df46bcc1361f
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -31,6 +31,19 @@ end
|
|
31
31
|
|
32
32
|
## Advanced Usage
|
33
33
|
|
34
|
+
### Format
|
35
|
+
|
36
|
+
The `format` option can be used to format the final identity value.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
class User < ActiveRecord::Base
|
40
|
+
# :identity_type is the attribute that will be used to determine the identity type and is required
|
41
|
+
validates :identity, identity: { identity_type: :identity_type, format: true }
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
### Custom Validators
|
46
|
+
|
34
47
|
New Identity Validators can be registered through the public apis of `ValidatesIdentity`
|
35
48
|
|
36
49
|
```ruby
|
@@ -39,8 +52,11 @@ ValidatesIdentity.register_identity_type('CustomIdentity', CustomIdentityValidat
|
|
39
52
|
|
40
53
|
Each Validator should have:
|
41
54
|
|
42
|
-
- a constructor with
|
55
|
+
- a constructor with 1 param: `value`
|
43
56
|
- a `valid?` method that returns a boolean
|
57
|
+
- a `formatted` method that returns the value formatted
|
58
|
+
|
59
|
+
### Validators Aliases
|
44
60
|
|
45
61
|
In case of a legacy system where keys were already defined and differ from the official ones, aliases can be registered as well
|
46
62
|
|
data/lib/identity_validator.rb
CHANGED
@@ -2,17 +2,19 @@
|
|
2
2
|
|
3
3
|
class IdentityValidator < ActiveModel::EachValidator
|
4
4
|
def validate_each(record, attribute, value)
|
5
|
-
identity = ValidatesIdentity::Identity.new(record, value, options)
|
5
|
+
identity = ValidatesIdentity::Identity.new(record, attribute, value, options)
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
ruby_prior_version_three =
|
10
|
-
Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.0.0')
|
11
|
-
|
12
|
-
if ruby_prior_version_three
|
13
|
-
record.errors.add(attribute, :invalid, options)
|
7
|
+
if identity.valid?
|
8
|
+
record.send("#{attribute}=", identity.formatted) if options[:format]
|
14
9
|
else
|
15
|
-
|
10
|
+
ruby_prior_version_three =
|
11
|
+
Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.0.0')
|
12
|
+
|
13
|
+
if ruby_prior_version_three
|
14
|
+
record.errors.add(attribute, :invalid, options)
|
15
|
+
else
|
16
|
+
record.errors.add(attribute, :invalid, **options)
|
17
|
+
end
|
16
18
|
end
|
17
19
|
end
|
18
20
|
end
|
@@ -2,10 +2,11 @@
|
|
2
2
|
|
3
3
|
module ValidatesIdentity
|
4
4
|
class Identity
|
5
|
-
attr_reader :record, :value, :options
|
5
|
+
attr_reader :record, :attribute, :value, :options
|
6
6
|
|
7
|
-
def initialize(record, value, options)
|
7
|
+
def initialize(record, attribute, value, options)
|
8
8
|
@record = record
|
9
|
+
@attribute = attribute
|
9
10
|
@value = value
|
10
11
|
@options = options
|
11
12
|
end
|
@@ -18,7 +19,7 @@ module ValidatesIdentity
|
|
18
19
|
|
19
20
|
return false if validator_class.nil?
|
20
21
|
|
21
|
-
validator = validator_class.new(value
|
22
|
+
validator = validator_class.new(value)
|
22
23
|
validator.valid?
|
23
24
|
end
|
24
25
|
|