validates_as_cnpj 0.1.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.
- data/MIT-LICENSE +20 -0
- data/README.textile +23 -0
- data/Rakefile +24 -0
- data/TODO +1 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/validates_as_cnpj/cnpj.rb +46 -0
- data/lib/validates_as_cnpj/validates_as_cnpj.rb +16 -0
- data/lib/validates_as_cnpj.rb +2 -0
- data/test/test_helper.rb +3 -0
- data/test/validate_as_cpf_test.rb +8 -0
- metadata +74 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009-2010 Gabriel Sobrinho
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
h1. ValidatesAsCnpj
|
2
|
+
|
3
|
+
This gem is based on *OSS* works in "https://projetos.ossystems.com.br/projects/plugonrails/repository/browse/plugins/validates_as_cnpj":https://projetos.ossystems.com.br/projects/plugonrails/repository/browse/plugins/validates_as_cnpj
|
4
|
+
|
5
|
+
h2. Installation
|
6
|
+
|
7
|
+
You can install as gem:
|
8
|
+
|
9
|
+
<pre>config.gem "validates_as_cnpj"</pre>
|
10
|
+
|
11
|
+
Or as plugin:
|
12
|
+
|
13
|
+
<pre>$ script/plugin install git://github.com/sobrinho/validates_as_cnpj.git</pre>
|
14
|
+
|
15
|
+
h2. Usage
|
16
|
+
|
17
|
+
<pre>class Company < ActiveRecord::Base
|
18
|
+
validates_as_cnpj :document
|
19
|
+
end</pre>
|
20
|
+
|
21
|
+
h2. License
|
22
|
+
|
23
|
+
Copyright (c) 2009-2010 Gabriel Sobrinho, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
desc 'Default: run unit tests.'
|
4
|
+
task :default => :test
|
5
|
+
|
6
|
+
desc 'Test the validates_as_cnpj plugin.'
|
7
|
+
Rake::TestTask.new(:test) do |t|
|
8
|
+
t.libs << 'test'
|
9
|
+
t.pattern = 'test/**/*_test.rb'
|
10
|
+
end
|
11
|
+
|
12
|
+
begin
|
13
|
+
require 'jeweler'
|
14
|
+
Jeweler::Tasks.new do |gemspec|
|
15
|
+
gemspec.name = "validates_as_cnpj"
|
16
|
+
gemspec.summary = "CNPJ validation for ActiveModel"
|
17
|
+
gemspec.email = "gabriel.sobrinho@gmail.com"
|
18
|
+
gemspec.homepage = "http://github.com/sobrinho/validates_as_cnpj"
|
19
|
+
gemspec.authors = ["Gabriel Sobrinho"]
|
20
|
+
end
|
21
|
+
Jeweler::GemcutterTasks.new
|
22
|
+
rescue LoadError
|
23
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
24
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'validates_as_cnpj'
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module CNPJ
|
2
|
+
# Invalids
|
3
|
+
INVALIDS = %w(
|
4
|
+
00000000000000 11111111111111 22222222222222 33333333333333
|
5
|
+
44444444444444 55555555555555 66666666666666 77777777777777
|
6
|
+
88888888888888 99999999999999)
|
7
|
+
|
8
|
+
def self.valid? cnpj
|
9
|
+
# Basic validation
|
10
|
+
cnpj = cnpj.to_s
|
11
|
+
|
12
|
+
if cnpj !~ /^\d{14}|\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/
|
13
|
+
return false
|
14
|
+
end
|
15
|
+
|
16
|
+
cnpj = cnpj.scan(/\d/).collect(&:to_i)
|
17
|
+
|
18
|
+
if INVALIDS.member? cnpj.to_s
|
19
|
+
return false
|
20
|
+
end
|
21
|
+
|
22
|
+
# Parse first digit
|
23
|
+
factor = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
|
24
|
+
|
25
|
+
sum = (0..11).inject(0) do |sum, i|
|
26
|
+
sum + (cnpj[i] * factor[i])
|
27
|
+
end
|
28
|
+
|
29
|
+
result = sum % 11
|
30
|
+
result = result < 2 ? 0 : 11 - result
|
31
|
+
|
32
|
+
return false unless cnpj[12] == result
|
33
|
+
|
34
|
+
# Parse second digit
|
35
|
+
factor.unshift 6
|
36
|
+
|
37
|
+
sum = (0..12).inject(0) do |sum, i|
|
38
|
+
sum + (cnpj[i] * factor[i])
|
39
|
+
end
|
40
|
+
|
41
|
+
result = sum % 11
|
42
|
+
result = result < 2 ? 0 : 11 - result
|
43
|
+
|
44
|
+
return result == cnpj[13]
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,16 @@
|
|
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
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_as_cnpj
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Gabriel Sobrinho
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-02-28 00:00:00 -03:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description:
|
22
|
+
email: gabriel.sobrinho@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.textile
|
29
|
+
- TODO
|
30
|
+
files:
|
31
|
+
- MIT-LICENSE
|
32
|
+
- README.textile
|
33
|
+
- Rakefile
|
34
|
+
- TODO
|
35
|
+
- VERSION
|
36
|
+
- init.rb
|
37
|
+
- lib/validates_as_cnpj.rb
|
38
|
+
- lib/validates_as_cnpj/cnpj.rb
|
39
|
+
- lib/validates_as_cnpj/validates_as_cnpj.rb
|
40
|
+
- test/test_helper.rb
|
41
|
+
- test/validate_as_cpf_test.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/sobrinho/validates_as_cnpj
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --charset=UTF-8
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.6
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: CNPJ validation for ActiveModel
|
72
|
+
test_files:
|
73
|
+
- test/test_helper.rb
|
74
|
+
- test/validate_as_cpf_test.rb
|