validates_cpf 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ .rvmrc
4
+ Gemfile.lock
5
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,19 @@
1
+ = ValidatesCPF
2
+
3
+ Rails gem to validate CPF
4
+
5
+ == Install
6
+
7
+ gem install validates_cpf
8
+
9
+ == Usage
10
+
11
+ Lets say you have a model with "cpf" string column that you want to be a valid CPF. Just add this to your model:
12
+
13
+ class User < ActiveRecord::Base
14
+ validates :cpf, :cpf => true
15
+ end
16
+
17
+ == Special Thanks
18
+
19
+ This project is based on brcpfcnpj gem and his intention it to mantain a cleaner code to validate CPF and macros for test it.
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,9 @@
1
+ require "active_model"
2
+ require "validates_cpf/version"
3
+ require "validates_cpf/cpf"
4
+
5
+ class CpfValidator < ActiveModel::EachValidator
6
+ def validate_each(record, attribute, value)
7
+ record.errors[attribute] << I18n.t("errors.messages.invalid") unless CPF.new(value).valid?
8
+ end
9
+ end
@@ -0,0 +1,50 @@
1
+ class CPF
2
+ def initialize(number)
3
+ number =~ /^(\d{3}\.?\d{3}\.?\d{3})-?(\d{2})$/
4
+ @number = number
5
+ @pure_number = $1
6
+ @result = $2
7
+ @cleaned_number = @pure_number.nil? ? nil : @number.gsub(/[\.-]/, "")
8
+ format_number! if @pure_number
9
+ end
10
+
11
+ def valid?
12
+ return true if @number.nil?
13
+ return false unless @pure_number
14
+ check_cpf
15
+ end
16
+
17
+ def number
18
+ @number
19
+ end
20
+
21
+ def check_cpf
22
+ return false if @cleaned_number.length != 11 or @cleaned_number.scan(/\d/).uniq.length == 1
23
+ @result == first_digit_verifier + second_digit_verifier
24
+ end
25
+
26
+ def first_digit_verifier
27
+ sum = multiply_and_sum([10, 9, 8, 7, 6, 5, 4, 3, 2], @pure_number)
28
+ digit_verifier(sum%11).to_s
29
+ end
30
+
31
+ def second_digit_verifier
32
+ sum = multiply_and_sum([11, 10, 9, 8, 7, 6, 5, 4, 3, 2], @pure_number + first_digit_verifier)
33
+ digit_verifier(sum%11).to_s
34
+ end
35
+
36
+ def multiply_and_sum(array, number)
37
+ multiplied = []
38
+ number.scan(/\d{1}/).each_with_index { |e, i| multiplied[i] = e.to_i * array[i] }
39
+ multiplied.inject { |s,e| s + e }
40
+ end
41
+
42
+ def digit_verifier(rest)
43
+ rest < 2 ? 0 : 11 - rest
44
+ end
45
+
46
+ def format_number!
47
+ @cleaned_number =~ /(\d{3})(\d{3})(\d{3})(\d{2})/
48
+ @number = "#{$1}.#{$2}.#{$3}-#{$4}"
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module ValidatesCpf
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :users do |u|
4
+ u.string :cpf
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ drop_table :users
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ validates :cpf, :cpf => true
3
+ end
@@ -0,0 +1,10 @@
1
+ require "rubygems"
2
+ require "rspec"
3
+ require "active_record"
4
+
5
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/validates_cpf")
6
+
7
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
8
+ require File.dirname(__FILE__) + "/fake_app/db/create_users"
9
+
10
+ CreateUsers.migrate(:up)
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + "/../../lib/validates_cpf/cpf")
4
+
5
+ describe CPF do
6
+ context "should be invalid with" do
7
+ it "blank number" do
8
+ CPF.new('').should_not be_valid
9
+ end
10
+
11
+ it "345.65.67.3 as number" do
12
+ CPF.new('345.65.67.3').should_not be_valid
13
+ end
14
+
15
+ it "567.765-87698 as number" do
16
+ CPF.new('567.765-87698').should_not be_valid
17
+ end
18
+
19
+ it "345456-654-01 as number" do
20
+ CPF.new('345456-654-01').should_not be_valid
21
+ end
22
+
23
+ it "123456 as number" do
24
+ CPF.new('123456').should_not be_valid
25
+ end
26
+
27
+ it "23342345699 as number" do
28
+ CPF.new('23342345699').should_not be_valid
29
+ end
30
+
31
+ it "34.543.567-98 as number" do
32
+ CPF.new('34.543.567-98').should_not be_valid
33
+ end
34
+
35
+ it "456.676456-87 as number" do
36
+ CPF.new('456.676456-87').should_not be_valid
37
+ end
38
+
39
+ it "333333333-33 as number" do
40
+ CPF.new('333333333-33').should_not be_valid
41
+ end
42
+
43
+ it "00000000000 as number" do
44
+ CPF.new('00000000000').should_not be_valid
45
+ end
46
+
47
+ it "000.000.000-00 as number" do
48
+ CPF.new('000.000.000-00').should_not be_valid
49
+ end
50
+
51
+ it "111.444.777-3500 as number" do
52
+ CPF.new('111.444.777-3500').should_not be_valid
53
+ end
54
+
55
+ it "11144477735AB as number" do
56
+ CPF.new('11144477735AB').should_not be_valid
57
+ end
58
+ end
59
+
60
+ context "should be valid with" do
61
+ it "nil as number" do
62
+ CPF.new(nil).should be_valid
63
+ end
64
+
65
+ it "111.444.777-35 as number" do
66
+ CPF.new('111.444.777-35').should be_valid
67
+ end
68
+
69
+ it "11144477735 as number" do
70
+ CPF.new('11144477735').should be_valid
71
+ end
72
+
73
+ it "111.444777-35 as number" do
74
+ CPF.new('111.444777-35').should be_valid
75
+ end
76
+
77
+ it "111444.777-35 as number" do
78
+ CPF.new('111444.777-35').should be_valid
79
+ end
80
+
81
+ it "111.444.77735 as number" do
82
+ CPF.new('111.444.77735').should be_valid
83
+ end
84
+ end
85
+
86
+ context "with a valid value" do
87
+ it "should return it formatted" do
88
+ CPF.new('11144477735').number.should == '111.444.777-35'
89
+ end
90
+ end
91
+
92
+ context "with an invalid value" do
93
+ it "should return as it was" do
94
+ CPF.new('123456').number.should == '123456'
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+ require File.expand_path(File.dirname(__FILE__) + "/fake_app/user")
3
+
4
+ describe CpfValidator do
5
+ context "when cpf is invalid" do
6
+ before :each do
7
+ @user = User.new(:cpf => "12345")
8
+ end
9
+
10
+ it "should set object as invalid" do
11
+ @user.valid?.should be_false
12
+ end
13
+
14
+ it "should set an error on attribute" do
15
+ @user.valid?
16
+ @user.errors[:cpf].should == ['is invalid']
17
+ end
18
+ end
19
+
20
+ context "when cpf is valid" do
21
+ before :each do
22
+ @user = User.new(:cpf => "11144477735")
23
+ end
24
+
25
+ it "should set object as valid" do
26
+ @user.valid?.should be_true
27
+ end
28
+
29
+ it "should not set an error on attribute" do
30
+ @user.valid?
31
+ @user.errors[:cpf].should be_blank
32
+ end
33
+ end
34
+
35
+ it "should accept a nil value" do
36
+ @user = User.new(:cpf => nil)
37
+ @user.valid?.should be_true
38
+ end
39
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "validates_cpf/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "validates_cpf"
7
+ s.version = ValidatesCpf::VERSION
8
+ s.authors = ["Paulo Henrique Lopes Ribeiro"]
9
+ s.email = %q{plribeiro3000@gmail.com}
10
+ s.homepage = ""
11
+ s.summary = %q{CPF Validation GEM}
12
+ s.description = %q{Validates CPF, the simple way.}
13
+
14
+ s.add_dependency("activerecord", ">= 3.0.0")
15
+
16
+ s.rubyforge_project = "validates_cpf"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = %w(lib)
22
+
23
+ s.add_development_dependency "rspec"
24
+ s.add_development_dependency "sqlite3"
25
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_cpf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Paulo Henrique Lopes Ribeiro
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &8077760 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *8077760
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &8076240 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *8076240
36
+ - !ruby/object:Gem::Dependency
37
+ name: sqlite3
38
+ requirement: &8075700 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *8075700
47
+ description: Validates CPF, the simple way.
48
+ email: plribeiro3000@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - README.rdoc
57
+ - Rakefile
58
+ - lib/validates_cpf.rb
59
+ - lib/validates_cpf/cpf.rb
60
+ - lib/validates_cpf/version.rb
61
+ - spec/fake_app/db/create_users.rb
62
+ - spec/fake_app/user.rb
63
+ - spec/spec_helper.rb
64
+ - spec/validates_cpf/cpf_spec.rb
65
+ - spec/validates_cpf_spec.rb
66
+ - validates_cpf.gemspec
67
+ homepage: ''
68
+ licenses: []
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project: validates_cpf
87
+ rubygems_version: 1.8.10
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: CPF Validation GEM
91
+ test_files: []