validates_as_cpf 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +21 -0
- data/README.textile +19 -0
- data/Rakefile +24 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/validates_as_cpf.rb +2 -0
- data/lib/validates_as_cpf/cpf.rb +51 -0
- data/lib/validates_as_cpf/cpf_validator.rb +5 -0
- data/test/test_helper.rb +18 -0
- data/test/validates_as_cpf_test.rb +35 -0
- metadata +72 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
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.
|
21
|
+
|
data/README.textile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
h1. ValidatesAsCpf
|
2
|
+
|
3
|
+
This plugin is a rework of original validates_as_cpf created by OSS. You can see it "here":https://projetos.ossystems.com.br/projects/plugonrails/repository/browse/plugins/validates_as_cpf
|
4
|
+
|
5
|
+
h2. Installation
|
6
|
+
|
7
|
+
<pre>$ rails plugin install git://github.com/sobrinho/validates_as_cpf.git</pre>
|
8
|
+
|
9
|
+
See "here":http://github.com/sobrinho/validates_as_cpf/tree/2.3 for older versions of Rails.
|
10
|
+
|
11
|
+
h2. Usage
|
12
|
+
|
13
|
+
<pre>class Person < ActiveRecord::Base
|
14
|
+
validates :document, :cpf => true
|
15
|
+
end</pre>
|
16
|
+
|
17
|
+
h2. License
|
18
|
+
|
19
|
+
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_cpf 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_cpf"
|
16
|
+
gemspec.summary = "CPF validation for ActiveModel"
|
17
|
+
gemspec.email = "gabriel.sobrinho@gmail.com"
|
18
|
+
gemspec.homepage = "http://github.com/sobrinho/validates_as_cpf"
|
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_cpf'
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module CPF
|
2
|
+
BLACK_LIST = %w(12345678909 11111111111 22222222222 33333333333 44444444444
|
3
|
+
55555555555 66666666666 77777777777 88888888888 99999999999
|
4
|
+
00000000000)
|
5
|
+
|
6
|
+
def self.valid?(cpf)
|
7
|
+
cpf = cpf.to_s
|
8
|
+
|
9
|
+
# could be 10 or 11 digits or with mask 999.999.999-99
|
10
|
+
if cpf !~ /^\d{10,11}|\d{3}\.\d{3}\.\d{3}-\d{2}$/
|
11
|
+
return false
|
12
|
+
end
|
13
|
+
|
14
|
+
cpf = cpf.scan(/\d/).collect(&:to_i)
|
15
|
+
cpf.unshift(0) if cpf.length == 10
|
16
|
+
|
17
|
+
# filter black list
|
18
|
+
if BLACK_LIST.include? cpf.join
|
19
|
+
return false
|
20
|
+
end
|
21
|
+
|
22
|
+
# calculate first digit
|
23
|
+
sum = (0..8).inject(0) do |sum, i|
|
24
|
+
sum + cpf[i] * (10 - i)
|
25
|
+
end
|
26
|
+
|
27
|
+
result = sum % 11
|
28
|
+
result = result < 2 ? 0 : 11 - result
|
29
|
+
|
30
|
+
if result != cpf[9]
|
31
|
+
return false
|
32
|
+
end
|
33
|
+
|
34
|
+
# calculate second digit
|
35
|
+
sum = (0..8).inject(0) do |sum, i|
|
36
|
+
sum + cpf[i] * (11 - i)
|
37
|
+
end
|
38
|
+
|
39
|
+
sum += cpf[9] * 2
|
40
|
+
|
41
|
+
result = sum % 11
|
42
|
+
result = result < 2 ? 0 : 11 - result
|
43
|
+
|
44
|
+
result == cpf[10]
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.invalid?(cpf)
|
48
|
+
!valid?(cpf)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_record'
|
5
|
+
require 'validates_as_cpf/cpf_validator'
|
6
|
+
require 'validates_as_cpf/cpf'
|
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 :people 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 ValidatesAsCpfTest < ActiveSupport::TestCase
|
4
|
+
class Person < ActiveRecord::Base
|
5
|
+
validates :document, :cpf => true
|
6
|
+
end
|
7
|
+
|
8
|
+
test "blank values" do
|
9
|
+
assert_equal false, Person.new(:document => '').valid?
|
10
|
+
assert_equal false, Person.new(:document => false).valid?
|
11
|
+
assert_equal false, Person.new(:document => nil).valid?
|
12
|
+
end
|
13
|
+
|
14
|
+
test "black list" do
|
15
|
+
CPF::BLACK_LIST.each do |cpf|
|
16
|
+
assert_equal false, Person.new(:document => cpf).valid?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
test "only digits" do
|
21
|
+
assert Person.new(:document => '18349376473').valid?
|
22
|
+
end
|
23
|
+
|
24
|
+
test "with mask" do
|
25
|
+
assert Person.new(:document => '183.493.764-73').valid?
|
26
|
+
end
|
27
|
+
|
28
|
+
test "integer value" do
|
29
|
+
assert Person.new(:document => 18349376473).valid?
|
30
|
+
end
|
31
|
+
|
32
|
+
test "integer value missing 0" do
|
33
|
+
assert Person.new(:document => 3467622607).valid?
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_as_cpf
|
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-27 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
|
+
files:
|
30
|
+
- MIT-LICENSE
|
31
|
+
- README.textile
|
32
|
+
- Rakefile
|
33
|
+
- VERSION
|
34
|
+
- init.rb
|
35
|
+
- lib/validates_as_cpf.rb
|
36
|
+
- lib/validates_as_cpf/cpf.rb
|
37
|
+
- lib/validates_as_cpf/cpf_validator.rb
|
38
|
+
- test/test_helper.rb
|
39
|
+
- test/validates_as_cpf_test.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/sobrinho/validates_as_cpf
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- --charset=UTF-8
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.3.6
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: CPF validation for ActiveModel
|
70
|
+
test_files:
|
71
|
+
- test/test_helper.rb
|
72
|
+
- test/validates_as_cpf_test.rb
|