cpf_validation 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 44e798b0702deee65891486926665c619e72dc5b
4
+ data.tar.gz: 6ff70cdebba5238991ffe71351bb90282f27abfa
5
+ SHA512:
6
+ metadata.gz: ec5a273da964f91dd6c9de5f5d2fc7f4d7d61b367af834e5aa36d61759144608a52c0201b84c291bdf39479006b2d56a5d8ec7036160bd08ef4c96efb99929ff
7
+ data.tar.gz: 8406da63f7fae56373c3f1b216601d54208d6b9ec142306746c424e131ae8125c6fe57dca4b18c2beef67d060119084ac0aae25cba63cad90b6f31f457ccc2a1
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in validates_cpf_cnpj.gemspec
4
+ gemspec
5
+
6
+ gem 'sqlite3'
data/README.rdoc ADDED
@@ -0,0 +1,55 @@
1
+ = CpfValidation
2
+
3
+ == Description
4
+
5
+ CPF validation for ActiveModel and Rails.
6
+
7
+ == Installation
8
+
9
+ As gem:
10
+
11
+ # in Gemfile
12
+ gem 'cpf_validation'
13
+
14
+ # Run bundler
15
+ $ bundle install
16
+
17
+ == Usage
18
+
19
+ Validating a CPF attribute:
20
+
21
+ class Patient < ActiveRecord::Base
22
+ validates_cpf :cpf_attr
23
+ # or
24
+ validates :cpf_attr, :cpf => true
25
+ end
26
+
27
+ Validating an attribute that can store CPF:
28
+
29
+ class Customer < ActiveRecord::Base
30
+ validates_cpf :cpf_attr
31
+ # or
32
+ validates :cpf_attr, :cpf => true
33
+ end
34
+
35
+ Regular validation options:
36
+
37
+ :allow_nil - Allows a nil value to be valid
38
+ :allow_blank - Allows a nil or empty string value to be valid
39
+ :if - Executes validation when :if evaluates true
40
+ :unless - Executes validation when :unless evaluates false
41
+ :on - Specifies validation context (e.g :save, :create or :update). Default is :save
42
+
43
+ == Contributing
44
+
45
+ Feel free to fork, fix and send me a pull request.
46
+
47
+ == Maintainers
48
+
49
+ * {Gabriel Lidenor}[https://github.com/GabrielLidenor]
50
+
51
+ == License
52
+
53
+ Released under the MIT license:
54
+
55
+ * http://www.opensource.org/licenses/MIT
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :test => :spec
@@ -0,0 +1,26 @@
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 = "cpf_validation"
7
+ s.version = ValidatesCpf::VERSION
8
+ s.authors = ["Gabriel Lidenor"]
9
+ s.email = ["gabriellidenor@arvantech.com"]
10
+ s.homepage = "https://github.com/GabrielLidenor/cpf_validation"
11
+ s.summary = %q{CPF ActiveModel validation}
12
+ s.description = %q{CPF validation for ActiveModel and Rails}
13
+
14
+ s.rubyforge_project = "validates_cpf"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rake"
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency "activerecord"
24
+
25
+ s.add_runtime_dependency 'activemodel', '~> 3.0', '>= 3.0.0'
26
+ end
data/lib/.DS_Store ADDED
Binary file
@@ -0,0 +1,29 @@
1
+ require 'active_model'
2
+ require 'validates_cpf/cpf'
3
+
4
+ module ActiveModel
5
+ module Validations
6
+ class CpfValidator < ActiveModel::EachValidator
7
+ include ValidatesCpf
8
+
9
+ def validate_each(record, attr_name, value)
10
+ return if (options[:allow_nil] and value.nil?) or (options[:allow_blank] and value.blank?)
11
+ return if (options[:if] == false) or (options[:unless] == true)
12
+ return if (options[:on].to_s == 'create' and not record.new_record?) or (options[:on].to_s == 'update' and record.new_record?)
13
+
14
+ if value.to_s.gsub(/[^0-9]/, '').length <= 11
15
+ if (not value.to_s.match(/\A\d{11}\z/) and not value.to_s.match(/\A\d{3}\.\d{3}\.\d{3}\-\d{2}\z/)) or not Cpf.valid?(value)
16
+ record.errors.add(attr_name)
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ module HelperMethods
23
+ def validates_cpf(*attr_names)
24
+ raise ArgumentError, "You need to supply at least one attribute" if attr_names.empty?
25
+ validates_with CpfValidator, _merge_attributes(attr_names)
26
+ end
27
+ end
28
+ end
29
+ end
Binary file
@@ -0,0 +1,28 @@
1
+ module ValidatesCpf
2
+ module Cpf
3
+ @@invalid_cpfs = %w{12345678909 11111111111 22222222222 33333333333 44444444444 55555555555 66666666666 77777777777 88888888888 99999999999 00000000000}
4
+
5
+ def self.valid?(value)
6
+ value.gsub!(/[^0-9]/, '')
7
+
8
+ return false if @@invalid_cpfs.member?(value)
9
+
10
+ digit = value.slice(-2, 2)
11
+ control = ''
12
+ if value.size == 11
13
+ factor = 0
14
+ 2.times do |i|
15
+ sum = 0
16
+ 9.times do |j|
17
+ sum += value.slice(j, 1).to_i * (10 + i - j)
18
+ end
19
+ sum += (factor * 2) if i == 1
20
+ factor = (sum * 10) % 11
21
+ factor = 0 if factor == 10
22
+ control << factor.to_s
23
+ end
24
+ end
25
+ control == digit
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module ValidatesCpf
2
+ VERSION = "0.1.0"
3
+ end
data/spec/.DS_Store ADDED
Binary file
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesCpf do
4
+ describe 'validates_cpf' do
5
+ it 'should raise an ArgumentError when no attribute is informed' do
6
+ person = Person.new
7
+ expect{person.validates_cpf}.to raise_exception(ArgumentError, 'You need to supply at least one attribute')
8
+ end
9
+
10
+ context 'should be invalid when' do
11
+ invalid_cpfs = %w{1234567890 12345678901 ABC45678901 123.456.789-01 800337.878-83 800337878-83}
12
+
13
+ invalid_cpfs.each do |cpf|
14
+ it "value is #{cpf}" do
15
+ person = Person.new(:code => cpf)
16
+ person.validates_cpf(:code)
17
+ expect(person.errors).to_not be_empty
18
+ end
19
+ end
20
+
21
+ it 'value is nil' do
22
+ person = Person.new(:code => nil)
23
+ person.validates_cpf(:code)
24
+ expect(person.errors).to_not be_empty
25
+ end
26
+
27
+ it 'value is empty' do
28
+ person = Person.new(:code => '')
29
+ person.validates_cpf(:code)
30
+ expect(person.errors).to_not be_empty
31
+ end
32
+
33
+ # This numbers will be considered valid by the algorithm but is known as not valid on real world, so they should be blocked
34
+ blocked_cpfs = %w{12345678909 11111111111 22222222222 33333333333 44444444444 55555555555 66666666666 77777777777 88888888888 99999999999 00000000000}
35
+
36
+ blocked_cpfs.each do |cpf|
37
+ it "is a well know invalid number: #{cpf}" do
38
+ person = Person.new(:code => cpf)
39
+ person.validates_cpf(:code)
40
+ expect(person.errors).to_not be_empty
41
+ end
42
+ end
43
+ end
44
+
45
+ context 'should be valid when' do
46
+ it 'value is 80033787883' do
47
+ person = Person.new(:code => '80033787883')
48
+ person.validates_cpf(:code)
49
+ expect(person.errors).to be_empty
50
+ end
51
+
52
+ it 'value is 800.337.878-83' do
53
+ person = Person.new(:code => '800.337.878-83')
54
+ person.validates_cpf(:code)
55
+ expect(person.errors).to be_empty
56
+ end
57
+
58
+ it 'value is nil and :allow_nil or :allow_blank is true' do
59
+ person = Person.new(:code => nil)
60
+ person.validates_cpf(:code, :allow_nil => true)
61
+ expect(person.errors).to be_empty
62
+ person.validates_cpf(:code, :allow_blank => true)
63
+ expect(person.errors).to be_empty
64
+ end
65
+
66
+ it 'value is empty and :allow_blank is true' do
67
+ person = Person.new(:code => '')
68
+ person.validates_cpf(:code, :allow_blank => true)
69
+ expect(person.errors).to be_empty
70
+ person.validates_cpf(:code, :allow_nil => true)
71
+ expect(person.errors).to_not be_empty
72
+ end
73
+
74
+ it ':if option evaluates false' do
75
+ person = Person.new(:code => '12345678901')
76
+ person.validates_cpf(:code, :if => false)
77
+ expect(person.errors).to be_empty
78
+ end
79
+
80
+ it ':unless option evaluates true' do
81
+ person = Person.new(:code => '12345678901')
82
+ person.validates_cpf(:code, :unless => true)
83
+ expect(person.errors).to be_empty
84
+ end
85
+
86
+ it ':on option is :update and the model instance is a new record' do
87
+ person = Person.new(:code => '12345678901')
88
+ person.validates_cpf(:code, :on => :update)
89
+ expect(person.errors).to be_empty
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,18 @@
1
+ require 'validates_cpf'
2
+ require 'active_record'
3
+
4
+ ActiveRecord::Base.establish_connection(
5
+ :adapter => 'sqlite3',
6
+ :database => ':memory:'
7
+ )
8
+
9
+ ActiveRecord::Schema.define do
10
+ self.verbose = false
11
+
12
+ create_table :people do |t|
13
+ t.string :code
14
+ end
15
+ end
16
+
17
+ class Person < ActiveRecord::Base
18
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cpf_validation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Lidenor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 3.0.0
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '3.0'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 3.0.0
75
+ description: CPF validation for ActiveModel and Rails
76
+ email:
77
+ - gabriellidenor@arvantech.com
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - ".DS_Store"
83
+ - ".gitignore"
84
+ - ".rspec"
85
+ - Gemfile
86
+ - README.rdoc
87
+ - Rakefile
88
+ - cpf_validaton.gemspec
89
+ - lib/.DS_Store
90
+ - lib/validates_cpf.rb
91
+ - lib/validates_cpf/.DS_Store
92
+ - lib/validates_cpf/cpf.rb
93
+ - lib/validates_cpf/version.rb
94
+ - spec/.DS_Store
95
+ - spec/lib/validates_cpf_spec.rb
96
+ - spec/spec_helper.rb
97
+ homepage: https://github.com/GabrielLidenor/cpf_validation
98
+ licenses: []
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project: validates_cpf
116
+ rubygems_version: 2.4.5
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: CPF ActiveModel validation
120
+ test_files:
121
+ - spec/lib/validates_cpf_spec.rb
122
+ - spec/spec_helper.rb