validates_as_cnpj 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/validates_as_cnpj.rb +1 -1
- data/lib/validates_as_cnpj/cnpj.rb +22 -16
- data/lib/validates_as_cnpj/cnpj_validator.rb +5 -0
- data/test/test_helper.rb +16 -1
- data/test/validate_as_cnpj_test.rb +35 -0
- metadata +5 -7
- data/TODO +0 -1
- data/lib/validates_as_cnpj/validates_as_cnpj.rb +0 -16
- data/test/validate_as_cpf_test.rb +0 -8
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/validates_as_cnpj.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
require 'validates_as_cnpj/cnpj'
|
2
|
-
require 'validates_as_cnpj/
|
2
|
+
require 'validates_as_cnpj/cnpj_validator'
|
@@ -1,46 +1,52 @@
|
|
1
1
|
module CNPJ
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
def self.valid? cnpj
|
9
|
-
# Basic validation
|
2
|
+
BLACK_LIST = %w(00000000000000 11111111111111 22222222222222 33333333333333
|
3
|
+
44444444444444 55555555555555 66666666666666 77777777777777
|
4
|
+
88888888888888 99999999999999)
|
5
|
+
|
6
|
+
def self.valid?(cnpj)
|
10
7
|
cnpj = cnpj.to_s
|
11
8
|
|
12
|
-
|
9
|
+
# could be 13 or 14 digits or with mask 99.999.999/9999-99
|
10
|
+
if cnpj !~ /^\d{13,14}|\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/
|
13
11
|
return false
|
14
12
|
end
|
15
13
|
|
16
14
|
cnpj = cnpj.scan(/\d/).collect(&:to_i)
|
15
|
+
cnpj.unshift(0) if cnpj.length == 13
|
17
16
|
|
18
|
-
|
17
|
+
# filter black list
|
18
|
+
if BLACK_LIST.include? cnpj.join
|
19
19
|
return false
|
20
20
|
end
|
21
21
|
|
22
|
-
#
|
22
|
+
# calculate first digit
|
23
23
|
factor = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
|
24
24
|
|
25
25
|
sum = (0..11).inject(0) do |sum, i|
|
26
|
-
sum +
|
26
|
+
sum + cnpj[i] * factor[i]
|
27
27
|
end
|
28
28
|
|
29
29
|
result = sum % 11
|
30
30
|
result = result < 2 ? 0 : 11 - result
|
31
31
|
|
32
|
-
|
32
|
+
if result != cnpj[12]
|
33
|
+
return false
|
34
|
+
end
|
33
35
|
|
34
|
-
#
|
36
|
+
# calculate second digit
|
35
37
|
factor.unshift 6
|
36
38
|
|
37
39
|
sum = (0..12).inject(0) do |sum, i|
|
38
|
-
sum +
|
40
|
+
sum + cnpj[i] * factor[i]
|
39
41
|
end
|
40
42
|
|
41
43
|
result = sum % 11
|
42
44
|
result = result < 2 ? 0 : 11 - result
|
43
45
|
|
44
|
-
|
46
|
+
result == cnpj[13]
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.invalid?(cnpj)
|
50
|
+
!valid?(cnpj)
|
45
51
|
end
|
46
52
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,3 +1,18 @@
|
|
1
1
|
require 'rubygems'
|
2
|
+
require 'test/unit'
|
2
3
|
require 'active_support'
|
3
|
-
require '
|
4
|
+
require 'active_record'
|
5
|
+
require 'validates_as_cnpj/cnpj_validator'
|
6
|
+
require 'validates_as_cnpj/cnpj'
|
7
|
+
|
8
|
+
# create a temporary database
|
9
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
|
10
|
+
|
11
|
+
silence_stream(STDOUT) do
|
12
|
+
ActiveRecord::Schema.define do
|
13
|
+
create_table :companies do |t|
|
14
|
+
t.string :name
|
15
|
+
t.string :document
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidatesAsCnpjTest < ActiveSupport::TestCase
|
4
|
+
class Company < ActiveRecord::Base
|
5
|
+
validates :document, :cnpj => true
|
6
|
+
end
|
7
|
+
|
8
|
+
test "blank values" do
|
9
|
+
assert_equal false, Company.new(:document => '').valid?
|
10
|
+
assert_equal false, Company.new(:document => false).valid?
|
11
|
+
assert_equal false, Company.new(:document => nil).valid?
|
12
|
+
end
|
13
|
+
|
14
|
+
test "black list" do
|
15
|
+
CNPJ::BLACK_LIST.each do |cnpj|
|
16
|
+
assert_equal false, Company.new(:document => cnpj).valid?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
test "only digits" do
|
21
|
+
assert Company.new(:document => '41821571000174').valid?
|
22
|
+
end
|
23
|
+
|
24
|
+
test "with mask" do
|
25
|
+
assert Company.new(:document => '41.821.571/0001-74').valid?
|
26
|
+
end
|
27
|
+
|
28
|
+
test "integer value" do
|
29
|
+
assert Company.new(:document => 41821571000174).valid?
|
30
|
+
end
|
31
|
+
|
32
|
+
test "integer value missing 0" do
|
33
|
+
assert Company.new(:document => 3133161000141).valid?
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 2
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Gabriel Sobrinho
|
@@ -26,19 +26,17 @@ extensions: []
|
|
26
26
|
|
27
27
|
extra_rdoc_files:
|
28
28
|
- README.textile
|
29
|
-
- TODO
|
30
29
|
files:
|
31
30
|
- MIT-LICENSE
|
32
31
|
- README.textile
|
33
32
|
- Rakefile
|
34
|
-
- TODO
|
35
33
|
- VERSION
|
36
34
|
- init.rb
|
37
35
|
- lib/validates_as_cnpj.rb
|
38
36
|
- lib/validates_as_cnpj/cnpj.rb
|
39
|
-
- lib/validates_as_cnpj/
|
37
|
+
- lib/validates_as_cnpj/cnpj_validator.rb
|
40
38
|
- test/test_helper.rb
|
41
|
-
- test/
|
39
|
+
- test/validate_as_cnpj_test.rb
|
42
40
|
has_rdoc: true
|
43
41
|
homepage: http://github.com/sobrinho/validates_as_cnpj
|
44
42
|
licenses: []
|
@@ -71,4 +69,4 @@ specification_version: 3
|
|
71
69
|
summary: CNPJ validation for ActiveModel
|
72
70
|
test_files:
|
73
71
|
- test/test_helper.rb
|
74
|
-
- test/
|
72
|
+
- test/validate_as_cnpj_test.rb
|
@@ -1,16 +0,0 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module Validations
|
3
|
-
module ClassMethods
|
4
|
-
def validates_as_cnpj *attr_names
|
5
|
-
configuration = { :message => :invalid }
|
6
|
-
configuration.update(attr_names.extract_options!)
|
7
|
-
|
8
|
-
validates_each(attr_names, configuration) do |record, attr_name, value|
|
9
|
-
unless CNPJ::valid? value
|
10
|
-
record.errors.add attr_name, configuration[:message]
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|