cpf_validator 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in foo.gemspec
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2009-2011 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,17 @@
1
+ h1. ValidatesAsCpf
2
+
3
+ This project is based on O.S. Systems works "here":https://projetos.ossystems.com.br/projects/plugonrails/repository/browse/plugins/validates_as_cpf
4
+
5
+ h2. Installation
6
+
7
+ <pre>gem "validates_as_cpf"</pre>
8
+
9
+ h2. Usage
10
+
11
+ <pre>class Person < ActiveRecord::Base
12
+ validates :document, :cpf => true
13
+ end</pre>
14
+
15
+ h2. License
16
+
17
+ Copyright (c) 2009-2011 Gabriel Sobrinho, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ task :default => :test
7
+
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << 'test'
10
+ t.pattern = 'test/**/*_test.rb'
11
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |s|
3
+ s.name = "cpf_validator"
4
+ s.version = "0.2.0"
5
+ s.platform = Gem::Platform::RUBY
6
+ s.authors = ["Gabriel Sobrinho"]
7
+ s.email = ["gabriel.sobrinho@gmail.com"]
8
+ s.homepage = "https://github.com/sobrinho/cpf_validator"
9
+ s.summary = %q{CPF validation for ActiveModel}
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14
+ s.require_paths = ["lib"]
15
+
16
+ s.add_dependency(%q<activemodel>, [">= 3.0"])
17
+ end
@@ -0,0 +1,7 @@
1
+ class CpfValidator < ActiveModel::EachValidator
2
+ autoload :Cpf, 'cpf_validator/cpf'
3
+
4
+ def validate_each(record, attribute, value)
5
+ record.errors.add(attribute, options[:message]) unless Cpf.valid?(value)
6
+ end
7
+ end
@@ -0,0 +1,50 @@
1
+ class CpfValidator::Cpf
2
+ def self.valid?(number)
3
+ new(number).valid?
4
+ end
5
+
6
+ attr_reader :number
7
+
8
+ def initialize(number)
9
+ @number = number.to_s
10
+ end
11
+
12
+ def digits
13
+ @digits ||= number.scan(/\d/).map(&:to_i)
14
+ end
15
+
16
+ def plain
17
+ @plain ||= digits.join
18
+ end
19
+
20
+ def valid?
21
+ formatted? && !black_listed? && digits_matches?
22
+ end
23
+
24
+ private
25
+
26
+ def formatted?
27
+ plain =~ /^\d{11}|\d{3}\.\d{3}\.\d{3}-\d{2}$/
28
+ end
29
+
30
+ def black_listed?
31
+ plain =~ /^12345678909|(\d)\1{10}$/
32
+ end
33
+
34
+ def digits_matches?
35
+ digit_matches?(9) && digit_matches?(10)
36
+ end
37
+
38
+ def digit_matches?(digit)
39
+ sum = 0
40
+
41
+ digit.times do |i|
42
+ sum += digits[i] * (digit + 1 - i)
43
+ end
44
+
45
+ result = sum % 11
46
+ result = result < 2 ? 0 : 11 - result
47
+
48
+ result == digits[digit]
49
+ end
50
+ end
@@ -0,0 +1,43 @@
1
+ require 'test_helper'
2
+
3
+ class CpfValidatorTest < Test::Unit::TestCase
4
+ def test_black_list
5
+ %w(12345678909 00000000000 11111111111 22222222222
6
+ 33333333333 44444444444 55555555555 66666666666
7
+ 77777777777 88888888888 99999999999).each do |number|
8
+ assert_invalid number
9
+ end
10
+ end
11
+
12
+ def test_invalid
13
+ assert_invalid '617.274.462-52'
14
+ assert_invalid '235.462.542-34'
15
+ assert_invalid '234.624.576-57'
16
+ end
17
+
18
+ def test_masked
19
+ assert_valid '262.249.648-66'
20
+ assert_valid '124.713.257-93'
21
+ assert_valid '691.844.380-10'
22
+ end
23
+
24
+ def test_unmasked
25
+ assert_valid '26224964866'
26
+ assert_valid '12471325793'
27
+ assert_valid '69184438010'
28
+ end
29
+
30
+ protected
31
+
32
+ def assert_valid(number)
33
+ assert document(number).valid?
34
+ end
35
+
36
+ def assert_invalid(number)
37
+ assert document(number).invalid?
38
+ end
39
+
40
+ def document(number)
41
+ Document.new(number)
42
+ end
43
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_model'
4
+ require 'cpf_validator'
5
+
6
+ class Document < Struct.new(:number)
7
+ include ActiveModel::Validations
8
+ validates :number, :cpf => true
9
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cpf_validator
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.2.0
6
+ platform: ruby
7
+ authors:
8
+ - Gabriel Sobrinho
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-14 00:00:00 -03:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activemodel
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "3.0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ description:
28
+ email:
29
+ - gabriel.sobrinho@gmail.com
30
+ executables: []
31
+
32
+ extensions: []
33
+
34
+ extra_rdoc_files: []
35
+
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - MIT-LICENSE
40
+ - README.textile
41
+ - Rakefile
42
+ - cpf_validator.gemspec
43
+ - lib/cpf_validator.rb
44
+ - lib/cpf_validator/cpf.rb
45
+ - test/cpf_validator_test.rb
46
+ - test/test_helper.rb
47
+ has_rdoc: true
48
+ homepage: https://github.com/sobrinho/cpf_validator
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.5.2
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: CPF validation for ActiveModel
75
+ test_files:
76
+ - test/cpf_validator_test.rb
77
+ - test/test_helper.rb