validates_identity 0.2.0 → 0.3.1

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: a9b5937129800250884456627b93b6a8966523f1500109fccaf9c325f75be4ba
4
- data.tar.gz: 0e411812a260b49a7f541e6ddbedcc3cb58e296d1bc3231f3097505505a92b75
3
+ metadata.gz: f932507ebec9a185edee94cebdf2f6ca7ff6b8142ade9243d197654fd36201d2
4
+ data.tar.gz: b484245e99d1429b632d46b729deb0683042da47ec87b59010d6f6970e952a89
5
5
  SHA512:
6
- metadata.gz: 035474f190f44cb5a087dc36bace8ac6d7d794a2f75038fe5ede863d009981df90c9ba0190b72d159f046a4ec06728cba7b0776b68506b644dc742516008a136
7
- data.tar.gz: 8839f1aba9aac3383a5b1323504c415d36ec626ec9f5cc4b3e60846160b47e116012295f6f0d0e93152ebaa750f590eb76526ae110cccb5ff0b4aaf81a17fd82
6
+ metadata.gz: c9945d350a984600a079149e126fbf6a234110f5d1faba2415fd87d92dcb1d52704d018442e149047e2285bcfbaee15a15a07e32dafa055c99e13f4948cd45e4
7
+ data.tar.gz: 2c1cab2c85a42f3d0494838624b3d794a3730a70dd937ef33c1bd2c1b23955c12b06837a9ed36c55b498730a6fce01148b8c738333da621f0d6c12c3dd497d2d
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,14 @@
1
- ## [Unreleased]
1
+ ## [0.3.1] - 2024-02-25
2
+
3
+ ### Fixed
4
+
5
+ - Validators registration
6
+
7
+ ## [0.3.0] - 2024-02-25
8
+
9
+ ### Added
10
+
11
+ - `Format` option to format the final identity value
2
12
 
3
13
  ## [0.2.0] - 2024-02-25
4
14
 
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.1)
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
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ValidatesIdentity
3
+ class 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.1'
5
5
  end
@@ -3,7 +3,7 @@
3
3
  require 'active_model'
4
4
  require 'identity_validator'
5
5
 
6
- module ValidatesIdentity
6
+ class ValidatesIdentity
7
7
  autoload :Identity, 'validates_identity/identity'
8
8
 
9
9
  class << self
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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo Ribeiro