validates_cnpj 3.1.0 → 3.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 +4 -4
- data/.rubocop.yml +24 -4
- data/.ruby-version +1 -1
- data/lib/validates_cnpj/cnpj.rb +8 -8
- data/lib/validates_cnpj/cnpj_validator.rb +5 -1
- data/lib/validates_cnpj/require_a_valid_cnpj_matcher.rb +1 -1
- data/lib/validates_cnpj/version.rb +1 -1
- data/spec/cnpj_validator_spec.rb +40 -5
- data/spec/fake_app/masked_company.rb +17 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/validates_cnpj/cnpj_spec.rb +40 -0
- data/validates_cnpj.gemspec +4 -3
- metadata +25 -22
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 76d72b266a42dbde1f4a3bd6b0d4822450b29e617049699a8c9c7ec13248ce79
|
|
4
|
+
data.tar.gz: 21b2439edb281c481c1a3199ecbc992db3452bbdf775101010c29d9ed06d5f61
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5182b579f3926d2e29fb0a7e58c1894b2b6455e3a8e09fe6698ff307f56e7fd4680cb0a054513754b02ac810934a23e598b6ba72b439234ee7f015756e6897a9
|
|
7
|
+
data.tar.gz: c2621693f873408f0864f59f69d6d2df85f7ec72a18ca3129776cb00f2a9353c0f8b1b041ebc1c9a78d5e3c1b92cc27fac7427512eb14d0fdd393a9d313ee9e1
|
data/.rubocop.yml
CHANGED
|
@@ -1,17 +1,37 @@
|
|
|
1
|
+
plugins:
|
|
2
|
+
- rubocop-rake
|
|
3
|
+
- rubocop-rspec
|
|
4
|
+
|
|
1
5
|
AllCops:
|
|
6
|
+
NewCops: enable
|
|
2
7
|
TargetRubyVersion: 2.2
|
|
3
8
|
|
|
9
|
+
Gemspec/DevelopmentDependencies:
|
|
10
|
+
Enabled: false
|
|
11
|
+
|
|
12
|
+
Layout/LineLength:
|
|
13
|
+
Max: 150
|
|
14
|
+
|
|
4
15
|
Metrics/AbcSize:
|
|
5
|
-
|
|
16
|
+
Enabled: false
|
|
6
17
|
|
|
7
18
|
Metrics/BlockLength:
|
|
8
19
|
Enabled: false
|
|
9
20
|
|
|
10
|
-
Metrics/
|
|
11
|
-
|
|
21
|
+
Metrics/CyclomaticComplexity:
|
|
22
|
+
Enabled: false
|
|
12
23
|
|
|
13
24
|
Metrics/MethodLength:
|
|
14
25
|
Max: 25
|
|
15
26
|
|
|
27
|
+
Metrics/PerceivedComplexity:
|
|
28
|
+
Enabled: false
|
|
29
|
+
|
|
30
|
+
RSpec/DescribedClass:
|
|
31
|
+
Enabled: false
|
|
32
|
+
|
|
16
33
|
Style/Documentation:
|
|
17
|
-
Enabled: false
|
|
34
|
+
Enabled: false
|
|
35
|
+
|
|
36
|
+
Style/RedundantConstantBase:
|
|
37
|
+
Enabled: false
|
data/.ruby-version
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
4.0.5
|
data/lib/validates_cnpj/cnpj.rb
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
module ValidatesCnpj
|
|
4
4
|
class Cnpj
|
|
5
5
|
def initialize(number)
|
|
6
|
-
number =~ %r{^(\d{2}
|
|
6
|
+
number.to_s.upcase =~ %r{^([A-Z\d]{2}\.?[A-Z\d]{3}\.?[A-Z\d]{3}/?[A-Z\d]{4})-?(\d{2})$}
|
|
7
7
|
@number = number
|
|
8
8
|
@pure_number = Regexp.last_match(1)
|
|
9
9
|
@result = Regexp.last_match(2)
|
|
10
|
-
@cleaned_number = @pure_number.nil? ? nil : @number.gsub(%r{[
|
|
10
|
+
@cleaned_number = @pure_number.nil? ? nil : @number.to_s.upcase.gsub(%r{[./-]}, '')
|
|
11
11
|
format_number! if @pure_number
|
|
12
12
|
end
|
|
13
13
|
|
|
@@ -15,15 +15,15 @@ module ValidatesCnpj
|
|
|
15
15
|
return true if @number.blank?
|
|
16
16
|
return false unless @pure_number
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
valid_cnpj?
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
attr_reader :number
|
|
22
22
|
|
|
23
23
|
private
|
|
24
24
|
|
|
25
|
-
def
|
|
26
|
-
return false if (@cleaned_number.length != 14) || (@cleaned_number.
|
|
25
|
+
def valid_cnpj?
|
|
26
|
+
return false if (@cleaned_number.length != 14) || (@cleaned_number[0, 12].chars.uniq.length == 1)
|
|
27
27
|
|
|
28
28
|
@result == first_digit_verifier + second_digit_verifier
|
|
29
29
|
end
|
|
@@ -40,8 +40,8 @@ module ValidatesCnpj
|
|
|
40
40
|
|
|
41
41
|
def multiply_and_sum(array, number)
|
|
42
42
|
multiplied = []
|
|
43
|
-
number.scan(
|
|
44
|
-
multiplied.inject { |
|
|
43
|
+
number.scan(/[A-Z\d]/).each_with_index { |character, index| multiplied[index] = (character.ord - 48) * array[index] }
|
|
44
|
+
multiplied.inject { |sum, element| sum + element }
|
|
45
45
|
end
|
|
46
46
|
|
|
47
47
|
def digit_verifier(rest)
|
|
@@ -49,7 +49,7 @@ module ValidatesCnpj
|
|
|
49
49
|
end
|
|
50
50
|
|
|
51
51
|
def format_number!
|
|
52
|
-
@cleaned_number =~ /(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/
|
|
52
|
+
@cleaned_number =~ /([A-Z\d]{2})([A-Z\d]{3})([A-Z\d]{3})([A-Z\d]{4})(\d{2})/
|
|
53
53
|
|
|
54
54
|
match1 = Regexp.last_match(1)
|
|
55
55
|
match2 = Regexp.last_match(2)
|
|
@@ -5,7 +5,11 @@ class CnpjValidator < ActiveModel::EachValidator
|
|
|
5
5
|
cnpj = ValidatesCnpj::Cnpj.new(value)
|
|
6
6
|
|
|
7
7
|
if cnpj.valid?
|
|
8
|
-
|
|
8
|
+
if options[:mask]
|
|
9
|
+
record.send("#{attribute}=", cnpj.number)
|
|
10
|
+
elsif value.is_a?(String) && value != value.upcase
|
|
11
|
+
record.send("#{attribute}=", value.upcase)
|
|
12
|
+
end
|
|
9
13
|
else
|
|
10
14
|
ruby_prior_version_three =
|
|
11
15
|
Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.0.0')
|
|
@@ -17,7 +17,7 @@ module Shoulda
|
|
|
17
17
|
|
|
18
18
|
def matches?(subject)
|
|
19
19
|
@subject = subject
|
|
20
|
-
disallows_value_of('123456') && allows_value_of('51.114.450/0001-46')
|
|
20
|
+
disallows_value_of('123456') && allows_value_of('51.114.450/0001-46') && allows_value_of('AB.12C.D34/EFGH-83')
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
end
|
data/spec/cnpj_validator_spec.rb
CHANGED
|
@@ -8,12 +8,12 @@ describe CnpjValidator do
|
|
|
8
8
|
context 'when cnpj is invalid' do
|
|
9
9
|
before do
|
|
10
10
|
company.cnpj = 12_345
|
|
11
|
-
allow(I18n).to receive(:t).with(:
|
|
12
|
-
default: :
|
|
11
|
+
allow(I18n).to receive(:t).with(:'activerecord.errors.models.company.attributes.cnpj.invalid',
|
|
12
|
+
default: :'activerecord.errors.messages.invalid').and_return('is invalid')
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
it 'sets object as invalid' do
|
|
16
|
-
expect(company.valid?).to
|
|
16
|
+
expect(company.valid?).to be(false)
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
it 'sets an error on attribute' do
|
|
@@ -28,7 +28,7 @@ describe CnpjValidator do
|
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
it 'sets object as valid' do
|
|
31
|
-
expect(company.valid?).to
|
|
31
|
+
expect(company.valid?).to be(true)
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
it 'does not set an error on attribute' do
|
|
@@ -37,7 +37,42 @@ describe CnpjValidator do
|
|
|
37
37
|
end
|
|
38
38
|
end
|
|
39
39
|
|
|
40
|
+
context 'when cnpj has lowercase letters' do
|
|
41
|
+
before do
|
|
42
|
+
company.cnpj = 'ab12cd34efgh83'
|
|
43
|
+
company.valid?
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it 'upcases the value' do
|
|
47
|
+
expect(company.cnpj).to eq('AB12CD34EFGH83')
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
context 'when cnpj is numeric' do
|
|
52
|
+
before do
|
|
53
|
+
company.cnpj = '37525685000108'
|
|
54
|
+
company.valid?
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it 'keeps the value as it was' do
|
|
58
|
+
expect(company.cnpj).to eq('37525685000108')
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
context 'with the mask option' do
|
|
63
|
+
let(:company) { MaskedCompany.new }
|
|
64
|
+
|
|
65
|
+
before do
|
|
66
|
+
company.cnpj = 'ab12cd34efgh83'
|
|
67
|
+
company.valid?
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it 'upcases and masks the value' do
|
|
71
|
+
expect(company.cnpj).to eq('AB.12C.D34/EFGH-83')
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
40
75
|
it 'accepts a nil value' do
|
|
41
|
-
expect(company.valid?).to
|
|
76
|
+
expect(company.valid?).to be(true)
|
|
42
77
|
end
|
|
43
78
|
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class MaskedCompany
|
|
4
|
+
include ActiveModel::Validations
|
|
5
|
+
include ActiveModel::Conversion
|
|
6
|
+
extend ActiveModel::Naming
|
|
7
|
+
|
|
8
|
+
attr_accessor :cnpj, :name
|
|
9
|
+
|
|
10
|
+
validates :cnpj, cnpj: { mask: true }
|
|
11
|
+
|
|
12
|
+
def initialize(attributes = {})
|
|
13
|
+
attributes.each do |key, value|
|
|
14
|
+
instance_variable_set("@#{key}", value)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -47,6 +47,26 @@ describe ValidatesCnpj::Cnpj do
|
|
|
47
47
|
it '6910360400016000 as number' do
|
|
48
48
|
expect(ValidatesCnpj::Cnpj.new('6910360400016000')).not_to be_valid
|
|
49
49
|
end
|
|
50
|
+
|
|
51
|
+
it 'AB12CD34EFGH84 as number' do
|
|
52
|
+
expect(ValidatesCnpj::Cnpj.new('AB12CD34EFGH84')).not_to be_valid
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it 'AB.12C.D34/EFGH-84 as number' do
|
|
56
|
+
expect(ValidatesCnpj::Cnpj.new('AB.12C.D34/EFGH-84')).not_to be_valid
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it 'AAAAAAAAAAAA00 as number' do
|
|
60
|
+
expect(ValidatesCnpj::Cnpj.new('AAAAAAAAAAAA00')).not_to be_valid
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it 'AB12CD34EFGH8 as number' do
|
|
64
|
+
expect(ValidatesCnpj::Cnpj.new('AB12CD34EFGH8')).not_to be_valid
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it 'AB12CD34EFGHAB as number' do
|
|
68
|
+
expect(ValidatesCnpj::Cnpj.new('AB12CD34EFGHAB')).not_to be_valid
|
|
69
|
+
end
|
|
50
70
|
end
|
|
51
71
|
|
|
52
72
|
context 'with valid option' do
|
|
@@ -77,12 +97,32 @@ describe ValidatesCnpj::Cnpj do
|
|
|
77
97
|
it '00.000.000/1447-89 as number' do
|
|
78
98
|
expect(ValidatesCnpj::Cnpj.new('00.000.000/1447-89')).to be_valid
|
|
79
99
|
end
|
|
100
|
+
|
|
101
|
+
it 'AB12CD34EFGH83 as number' do
|
|
102
|
+
expect(ValidatesCnpj::Cnpj.new('AB12CD34EFGH83')).to be_valid
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
it 'AB.12C.D34/EFGH-83 as number' do
|
|
106
|
+
expect(ValidatesCnpj::Cnpj.new('AB.12C.D34/EFGH-83')).to be_valid
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
it 'ab12cd34efgh83 as number' do
|
|
110
|
+
expect(ValidatesCnpj::Cnpj.new('ab12cd34efgh83')).to be_valid
|
|
111
|
+
end
|
|
80
112
|
end
|
|
81
113
|
|
|
82
114
|
context 'with a valid value' do
|
|
83
115
|
it 'returns it formatted' do
|
|
84
116
|
expect(ValidatesCnpj::Cnpj.new('69103604000160').number).to eq('69.103.604/0001-60')
|
|
85
117
|
end
|
|
118
|
+
|
|
119
|
+
it 'returns an alphanumeric number formatted' do
|
|
120
|
+
expect(ValidatesCnpj::Cnpj.new('AB12CD34EFGH83').number).to eq('AB.12C.D34/EFGH-83')
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
it 'returns a lowercase alphanumeric number formatted and upcased' do
|
|
124
|
+
expect(ValidatesCnpj::Cnpj.new('ab12cd34efgh83').number).to eq('AB.12C.D34/EFGH-83')
|
|
125
|
+
end
|
|
86
126
|
end
|
|
87
127
|
|
|
88
128
|
context 'with an invalid value' do
|
data/validates_cnpj.gemspec
CHANGED
|
@@ -10,7 +10,6 @@ Gem::Specification.new do |gem|
|
|
|
10
10
|
gem.summary = 'Validates CNPJ and test it with matchers in a simple way.'
|
|
11
11
|
|
|
12
12
|
gem.files = `git ls-files`.split("\n")
|
|
13
|
-
gem.test_files = `git ls-files -- {test,spec,features,examples,gemfiles}/*`.split("\n")
|
|
14
13
|
gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
|
15
14
|
gem.require_paths = %w[lib]
|
|
16
15
|
|
|
@@ -21,9 +20,11 @@ Gem::Specification.new do |gem|
|
|
|
21
20
|
gem.add_development_dependency 'coveralls'
|
|
22
21
|
gem.add_development_dependency 'rake'
|
|
23
22
|
gem.add_development_dependency 'rspec'
|
|
24
|
-
gem.add_development_dependency 'rubocop'
|
|
23
|
+
gem.add_development_dependency 'rubocop'
|
|
24
|
+
gem.add_development_dependency 'rubocop-rake'
|
|
25
25
|
gem.add_development_dependency 'rubocop-rspec'
|
|
26
26
|
gem.add_development_dependency 'shoulda-matchers'
|
|
27
27
|
|
|
28
|
-
gem.
|
|
28
|
+
gem.add_dependency 'activemodel'
|
|
29
|
+
gem.metadata['rubygems_mfa_required'] = 'true'
|
|
29
30
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: validates_cnpj
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Paulo Henrique Lopes Ribeiro
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: coveralls
|
|
@@ -56,16 +55,30 @@ dependencies:
|
|
|
56
55
|
name: rubocop
|
|
57
56
|
requirement: !ruby/object:Gem::Requirement
|
|
58
57
|
requirements:
|
|
59
|
-
- - "
|
|
58
|
+
- - ">="
|
|
60
59
|
- !ruby/object:Gem::Version
|
|
61
|
-
version: '0
|
|
60
|
+
version: '0'
|
|
62
61
|
type: :development
|
|
63
62
|
prerelease: false
|
|
64
63
|
version_requirements: !ruby/object:Gem::Requirement
|
|
65
64
|
requirements:
|
|
66
|
-
- - "
|
|
65
|
+
- - ">="
|
|
67
66
|
- !ruby/object:Gem::Version
|
|
68
|
-
version: '0
|
|
67
|
+
version: '0'
|
|
68
|
+
- !ruby/object:Gem::Dependency
|
|
69
|
+
name: rubocop-rake
|
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
type: :development
|
|
76
|
+
prerelease: false
|
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
69
82
|
- !ruby/object:Gem::Dependency
|
|
70
83
|
name: rubocop-rspec
|
|
71
84
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -108,7 +121,6 @@ dependencies:
|
|
|
108
121
|
- - ">="
|
|
109
122
|
- !ruby/object:Gem::Version
|
|
110
123
|
version: '0'
|
|
111
|
-
description:
|
|
112
124
|
email: plribeiro3000@gmail.com
|
|
113
125
|
executables: []
|
|
114
126
|
extensions: []
|
|
@@ -134,15 +146,15 @@ files:
|
|
|
134
146
|
- lib/validates_cnpj/version.rb
|
|
135
147
|
- spec/cnpj_validator_spec.rb
|
|
136
148
|
- spec/fake_app/company.rb
|
|
149
|
+
- spec/fake_app/masked_company.rb
|
|
137
150
|
- spec/shoulda/matchers/active_model/require_a_valid_cnpj_matcher_spec.rb
|
|
138
151
|
- spec/spec_helper.rb
|
|
139
152
|
- spec/validates_cnpj/cnpj_spec.rb
|
|
140
153
|
- validates_cnpj.gemspec
|
|
141
|
-
homepage:
|
|
142
154
|
licenses:
|
|
143
155
|
- MIT
|
|
144
|
-
metadata:
|
|
145
|
-
|
|
156
|
+
metadata:
|
|
157
|
+
rubygems_mfa_required: 'true'
|
|
146
158
|
rdoc_options: []
|
|
147
159
|
require_paths:
|
|
148
160
|
- lib
|
|
@@ -157,16 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
157
169
|
- !ruby/object:Gem::Version
|
|
158
170
|
version: '0'
|
|
159
171
|
requirements: []
|
|
160
|
-
rubygems_version:
|
|
161
|
-
signing_key:
|
|
172
|
+
rubygems_version: 4.0.10
|
|
162
173
|
specification_version: 4
|
|
163
174
|
summary: Validates CNPJ and test it with matchers in a simple way.
|
|
164
|
-
test_files:
|
|
165
|
-
- gemfiles/Gemfile.rails4
|
|
166
|
-
- gemfiles/Gemfile.rails5
|
|
167
|
-
- gemfiles/Gemfile.rails6
|
|
168
|
-
- spec/cnpj_validator_spec.rb
|
|
169
|
-
- spec/fake_app/company.rb
|
|
170
|
-
- spec/shoulda/matchers/active_model/require_a_valid_cnpj_matcher_spec.rb
|
|
171
|
-
- spec/spec_helper.rb
|
|
172
|
-
- spec/validates_cnpj/cnpj_spec.rb
|
|
175
|
+
test_files: []
|