validates_identity 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a9b5937129800250884456627b93b6a8966523f1500109fccaf9c325f75be4ba
4
- data.tar.gz: 0e411812a260b49a7f541e6ddbedcc3cb58e296d1bc3231f3097505505a92b75
3
+ metadata.gz: 3848016930f18f09b27ec31333622b53df51786d31c3eb612fe92da34c9f640a
4
+ data.tar.gz: c07d435462ce6caa0e3c4d1611af1aa2cfa8f06956a331696773845fafb6a02f
5
5
  SHA512:
6
- metadata.gz: 035474f190f44cb5a087dc36bace8ac6d7d794a2f75038fe5ede863d009981df90c9ba0190b72d159f046a4ec06728cba7b0776b68506b644dc742516008a136
7
- data.tar.gz: 8839f1aba9aac3383a5b1323504c415d36ec626ec9f5cc4b3e60846160b47e116012295f6f0d0e93152ebaa750f590eb76526ae110cccb5ff0b4aaf81a17fd82
6
+ metadata.gz: 539ce96f3cc65ca74b12ea9b83a6aaa568e3c15bb178e945c1224893092e6741f5abf6f69db65fe989163c2d528a808f34f3b9704b800b5a550e179f4a50bcea
7
+ data.tar.gz: ab5c1fe930362622f2efd4c928f71246aef44db216f36fd795492b5fbe4a7cdc3b11c27c295c95ba60f853ff46501b4f037dd379b2b1cf5fe681df46bcc1361f
data/.rubocop.yml CHANGED
@@ -12,5 +12,8 @@ Layout/LineLength:
12
12
  Metrics/BlockLength:
13
13
  Enabled: false
14
14
 
15
+ Metrics/MethodLength:
16
+ Max: 15
17
+
15
18
  Style/Documentation:
16
19
  Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,4 +1,8 @@
1
- ## [Unreleased]
1
+ ## [0.3.0] - 2024-02-25
2
+
3
+ ### Added
4
+
5
+ - `Format` option to format the final identity value
2
6
 
3
7
  ## [0.2.0] - 2024-02-25
4
8
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validates_identity (0.1.0)
4
+ validates_identity (0.3.0)
5
5
  activemodel
6
6
 
7
7
  GEM
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 2 params: `value` and `options` as a hash
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
 
@@ -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
- return if identity.valid?
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
- record.errors.add(attribute, :invalid, **options)
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, options)
22
+ validator = validator_class.new(value)
22
23
  validator.valid?
23
24
  end
24
25
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ValidatesIdentity
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo Ribeiro