validates_rfc 0.1.0 → 0.2.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: 7cf233289e707966e2f192bb34893c52d0728983fc1d4cd6e4a91a8d05f7250a
4
- data.tar.gz: 150f2118a15d87f92793de36515304fa4a0abe5d680d482596e52227b4fff647
3
+ metadata.gz: fc4e96fd9caa90d6dbb49d88ad36e492a35c9a27997fbc1113453f4422ea9d69
4
+ data.tar.gz: 1d3dd86f67e7873cd5463173d2ff75adeba6356bae0b69777d00ef7ae678dc76
5
5
  SHA512:
6
- metadata.gz: 83ea8e6beaef6f67eb6da3c231513760a56a897b4016d4d9025ad4da9fdde8554998e19e86c097335c52c7beabf366371e02d8b025ba9de0e7c770a68bca4d96
7
- data.tar.gz: 22ce98e250464662b6e5ac6110da51f853078ad27386f699405087e1c0b30ed4ff11753911e03a469297ef8164f8dbdb24f881db28ad9d638482a9a27e0e9d2f
6
+ metadata.gz: 644dc0e7c47aa817adb05496f67ff6505010896a6723d96c4af7ea9974576dea7b27857a03f0f7bf42e0e9c3596ac6ed84dbad4c2f098150464228aad9b5b550
7
+ data.tar.gz: bdf952cb4951ca946b7afb9290e19223264376dde9d9909a8ecc0e2a1f85fd2244c05bb137f0b07fda0db62a8c423eb73c46196cf79e934c38ab417915f3b68b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ### Added
4
+
5
+ - Option to validate Person RFC only
6
+ - Option to validate Company RFC only
7
+
3
8
  ## [0.1.0] - 2023-01-26
4
9
 
5
10
  - Initial release
@@ -10,17 +10,59 @@ module Shoulda
10
10
  end
11
11
 
12
12
  class RequireAValidRfcMatcher < ValidationMatcher
13
+ def initialize(attribute)
14
+ @type = :both
15
+ super
16
+ end
17
+
18
+ def for_company
19
+ @type = :company
20
+ self
21
+ end
22
+
23
+ def for_person
24
+ @type = :person
25
+ self
26
+ end
27
+
13
28
  def description
14
- 'requires a valid RFC'
29
+ case @type
30
+ when :both then 'requires a valid RFC'
31
+ when :company then 'requires a valid company RFC'
32
+ when :person then 'requires a valid person RFC'
33
+ end
15
34
  end
16
35
 
17
36
  def failure_message
18
- 'does not require a valid RFC'
37
+ case @type
38
+ when :both then 'does not require a valid RFC'
39
+ when :company then 'does not require a valid company RFC'
40
+ when :person then 'does not require a valid person RFC'
41
+ end
19
42
  end
20
43
 
21
44
  def matches?(subject)
22
45
  super(subject)
23
- disallows_value_of('HEGG560427MVCRRL04') && allows_value_of('DSRT850311SA3')
46
+
47
+ case @type
48
+ when :both then check_for_both_types
49
+ when :company then check_for_company_type
50
+ when :person then check_for_person_type
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def check_for_both_types
57
+ disallows_value_of('HEGG560427MVCRRL04') && allows_value_of('DSRT850311SA3') && allows_value_of('DSR850311SA3')
58
+ end
59
+
60
+ def check_for_company_type
61
+ disallows_value_of('HEGG560427MVCRRL04') && disallows_value_of('DSRT850311SA3') && allows_value_of('DSR850311SA3')
62
+ end
63
+
64
+ def check_for_person_type
65
+ disallows_value_of('HEGG560427MVCRRL04') && allows_value_of('DSRT850311SA3') && disallows_value_of('DSR850311SA3')
24
66
  end
25
67
  end
26
68
  end
@@ -2,16 +2,23 @@
2
2
 
3
3
  module ValidatesRfc
4
4
  class Rfc
5
+ COMPANY_REGEX = /\A([A-ZÑ&]{3})([0-9]{2}[0-1][0-9][0-3][0-9])([A-Z0-9]{3})\z/i.freeze
6
+ PERSON_REGEX = /\A([A-ZÑ&]{4})([0-9]{2}[0-1][0-9][0-3][0-9])([A-Z0-9]{3})\z/i.freeze
5
7
  REGEX = /\A([A-ZÑ&]{3,4})([0-9]{2}[0-1][0-9][0-3][0-9])([A-Z0-9]{3})\z/i.freeze
6
8
 
7
- def initialize(value)
9
+ def initialize(value, type = :both)
8
10
  @value = value
11
+ @type = type
9
12
  end
10
13
 
11
14
  def valid?
12
15
  return true if @value.blank?
13
16
 
14
- @value.match(REGEX)
17
+ case @type
18
+ when :both then @value.match(REGEX)
19
+ when :person then @value.match(PERSON_REGEX)
20
+ when :company then @value.match(COMPANY_REGEX)
21
+ end
15
22
 
16
23
  Regexp.last_match(0).present? && valid_date?(Regexp.last_match(2))
17
24
  end
@@ -2,7 +2,8 @@
2
2
 
3
3
  class RfcValidator < ActiveModel::EachValidator
4
4
  def validate_each(record, attribute, value)
5
- rfc = ValidatesRfc::Rfc.new(value)
5
+ type = options[:type] || :both
6
+ rfc = ValidatesRfc::Rfc.new(value, type)
6
7
 
7
8
  return if rfc.valid?
8
9
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ValidatesRfc
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_rfc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo Ribeiro