validates_cpf 1.1.3 → 2.0.0.rc1
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/.gitignore +16 -2
- data/.rspec +1 -1
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +17 -0
- data/README.md +44 -0
- data/gemfiles/Gemfile.rails3 +5 -0
- data/gemfiles/Gemfile.rails4 +5 -0
- data/lib/cpf_validator.rb +6 -0
- data/lib/require_a_valid_cpf_matcher.rb +23 -0
- data/lib/validates_cpf/cpf.rb +52 -50
- data/lib/validates_cpf/version.rb +2 -2
- data/lib/validates_cpf.rb +4 -10
- data/spec/fake_app/admin.rb +2 -1
- data/spec/fake_app/user.rb +13 -1
- data/spec/{validates_cpf/shoulda-matchers/require_a_valid_cpf_matcher_spec.rb → require_a_valid_cpf_matcher_spec.rb} +3 -3
- data/spec/spec_helper.rb +6 -9
- data/spec/validates_cpf/cpf_spec.rb +22 -22
- data/spec/validates_cpf_spec.rb +19 -17
- data/validates_cpf.gemspec +20 -22
- metadata +40 -61
- data/README.rdoc +0 -29
- data/lib/validates_cpf/remarkable/require_a_valid_cpf_matcher.rb +0 -29
- data/lib/validates_cpf/shoulda-matchers/require_a_valid_cpf_matcher.rb +0 -37
- data/spec/fake_app/db/create_admins.rb +0 -12
- data/spec/fake_app/db/create_users.rb +0 -12
- data/spec/validates_cpf/remarkable/require_a_valid_cpf_matcher_spec.rb +0 -20
data/.gitignore
CHANGED
data/.rspec
CHANGED
@@ -1 +1 @@
|
|
1
|
-
--colour --format documentation
|
1
|
+
--colour --format documentation --profile
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
validates_cpf
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.9.3
|
data/.travis.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
rvm:
|
2
|
+
- 1.8.7
|
3
|
+
- 1.9.2
|
4
|
+
- 1.9.3
|
5
|
+
- jruby
|
6
|
+
- ree
|
7
|
+
gemfile:
|
8
|
+
- gemfiles/Gemfile.rails3
|
9
|
+
- gemfiles/Gemfile.rails4
|
10
|
+
matrix:
|
11
|
+
exclude:
|
12
|
+
- rvm: 1.8.7
|
13
|
+
gemfile: gemfiles/Gemfile.rails4
|
14
|
+
- rvm: 1.9.2
|
15
|
+
gemfile: gemfiles/Gemfile.rails4
|
16
|
+
- rvm: ree
|
17
|
+
gemfile: gemfiles/Gemfile.rails4
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# ValidatesCPF
|
2
|
+
|
3
|
+
[](http://badge.fury.io/rb/validates_cpf) [](http://travis-ci.org/plribeiro3000/validates_cpf) [](https://gemnasium.com/plribeiro3000/validates_cpf) [](https://coveralls.io/r/plribeiro3000/validates_cpf) [](https://codeclimate.com/github/plribeiro3000/validates_cpf)
|
4
|
+
|
5
|
+
Validates cpf and test it in a simple way
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'validates_cpf'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install validates_cpf
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Just use as any other validator:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class User < ActiveRecord::Base
|
27
|
+
validates :cpf, :cpf => true
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
## Notes
|
32
|
+
|
33
|
+
It will load a macher to test automatically if the gem is below shoulda-matchers.
|
34
|
+
|
35
|
+
## Mantainers
|
36
|
+
[@plribeiro3000](https://github.com/plribeiro3000)
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
1. Fork it
|
41
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
42
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
43
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
44
|
+
5. Create new Pull Request
|
@@ -0,0 +1,6 @@
|
|
1
|
+
class CpfValidator < ActiveModel::EachValidator
|
2
|
+
def validate_each(record, attribute, value)
|
3
|
+
key = :"activerecord.errors.models.#{record.class.name.underscore}.attributes.#{attribute}.invalid"
|
4
|
+
record.errors.add(attribute, I18n.t(key, :default => :'activerecord.errors.messages.invalid')) unless ValidatesCpf::Cpf.new(value).valid?
|
5
|
+
end
|
6
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'shoulda-matchers'
|
2
|
+
require 'active_support/core_ext/array/wrap'
|
3
|
+
|
4
|
+
module Shoulda
|
5
|
+
module Matchers
|
6
|
+
module ActiveModel
|
7
|
+
def require_a_valid_cpf(attr = :cpf)
|
8
|
+
RequireAValidCpfMatcher.new(attr)
|
9
|
+
end
|
10
|
+
|
11
|
+
class RequireAValidCpfMatcher < ValidationMatcher
|
12
|
+
def description
|
13
|
+
'require a valid CPF number'
|
14
|
+
end
|
15
|
+
|
16
|
+
def matches?(subject)
|
17
|
+
@subject = subject
|
18
|
+
disallows_value_of('123456') && allows_value_of('897.546.112-20')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/validates_cpf/cpf.rb
CHANGED
@@ -1,52 +1,54 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
1
|
+
module ValidatesCpf
|
2
|
+
class Cpf
|
3
|
+
def initialize(number)
|
4
|
+
number =~ /^(\d{3}\.?\d{3}\.?\d{3})-?(\d{2})$/
|
5
|
+
@number = number
|
6
|
+
@pure_number = $1
|
7
|
+
@result = $2
|
8
|
+
@cleaned_number = @pure_number.nil? ? nil : @number.gsub(/[\.-]/, "")
|
9
|
+
format_number! if @pure_number
|
10
|
+
end
|
11
|
+
|
12
|
+
def valid?
|
13
|
+
return true if @number.nil?
|
14
|
+
return false unless @pure_number
|
15
|
+
check_cpf
|
16
|
+
end
|
17
|
+
|
18
|
+
def number
|
19
|
+
@number
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def check_cpf
|
25
|
+
return false if @cleaned_number.length != 11 or @cleaned_number.scan(/\d/).uniq.length == 1
|
26
|
+
@result == first_digit_verifier + second_digit_verifier
|
27
|
+
end
|
28
|
+
|
29
|
+
def first_digit_verifier
|
30
|
+
sum = multiply_and_sum([10, 9, 8, 7, 6, 5, 4, 3, 2], @pure_number)
|
31
|
+
digit_verifier(sum%11).to_s
|
32
|
+
end
|
33
|
+
|
34
|
+
def second_digit_verifier
|
35
|
+
sum = multiply_and_sum([11, 10, 9, 8, 7, 6, 5, 4, 3, 2], @pure_number + first_digit_verifier)
|
36
|
+
digit_verifier(sum%11).to_s
|
37
|
+
end
|
38
|
+
|
39
|
+
def multiply_and_sum(array, number)
|
40
|
+
multiplied = []
|
41
|
+
number.scan(/\d{1}/).each_with_index { |e, i| multiplied[i] = e.to_i * array[i] }
|
42
|
+
multiplied.inject { |s,e| s + e }
|
43
|
+
end
|
44
|
+
|
45
|
+
def digit_verifier(rest)
|
46
|
+
rest < 2 ? 0 : 11 - rest
|
47
|
+
end
|
48
|
+
|
49
|
+
def format_number!
|
50
|
+
@cleaned_number =~ /(\d{3})(\d{3})(\d{3})(\d{2})/
|
51
|
+
@number = "#{$1}.#{$2}.#{$3}-#{$4}"
|
52
|
+
end
|
51
53
|
end
|
52
54
|
end
|
@@ -1,3 +1,3 @@
|
|
1
1
|
module ValidatesCpf
|
2
|
-
VERSION =
|
3
|
-
end
|
2
|
+
VERSION = '2.0.0.rc1'
|
3
|
+
end
|
data/lib/validates_cpf.rb
CHANGED
@@ -1,12 +1,6 @@
|
|
1
|
-
require
|
1
|
+
require 'cpf_validator'
|
2
|
+
require 'require_a_valid_cpf_matcher' if defined?(::Shoulda)
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
require "validates_cpf/remarkable/require_a_valid_cpf_matcher" if defined?(::Remarkable)
|
6
|
-
|
7
|
-
class CpfValidator < ActiveModel::EachValidator
|
8
|
-
def validate_each(record, attribute, value)
|
9
|
-
key = :"activerecord.errors.models.#{record.class.name.downcase}.attributes.#{attribute.to_s}.invalid"
|
10
|
-
record.errors[attribute] << I18n.t(key, :default => :"activerecord.errors.messages.invalid") unless CPF.new(value).valid?
|
11
|
-
end
|
4
|
+
module ValidatesCpf
|
5
|
+
autoload :Cpf, 'validates_cpf/cpf'
|
12
6
|
end
|
data/spec/fake_app/admin.rb
CHANGED
data/spec/fake_app/user.rb
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
-
class User
|
1
|
+
class User
|
2
|
+
include ActiveModel::Validations
|
3
|
+
include ActiveModel::Conversion
|
4
|
+
extend ActiveModel::Naming
|
5
|
+
|
6
|
+
attr_accessor :cpf, :name
|
7
|
+
|
2
8
|
validates :cpf, :cpf => true
|
9
|
+
|
10
|
+
def initialize(attributes = {})
|
11
|
+
attributes.each do |key, value|
|
12
|
+
instance_variable_set("@#{key}", value)
|
13
|
+
end
|
14
|
+
end
|
3
15
|
end
|
@@ -6,15 +6,15 @@ describe Shoulda::Matchers::ActiveModel::RequireAValidCpfMatcher do
|
|
6
6
|
@user = User.new
|
7
7
|
end
|
8
8
|
|
9
|
-
it
|
9
|
+
it 'should accept on cpf' do
|
10
10
|
@user.should require_a_valid_cpf(:cpf)
|
11
11
|
end
|
12
12
|
|
13
|
-
it
|
13
|
+
it 'should accept without a specified attribute' do
|
14
14
|
@user.should require_a_valid_cpf
|
15
15
|
end
|
16
16
|
|
17
|
-
it
|
17
|
+
it 'should reject on name' do
|
18
18
|
@user.should_not require_a_valid_cpf(:name)
|
19
19
|
end
|
20
20
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,11 +1,8 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require 'rspec'
|
2
|
+
require 'active_model'
|
3
|
+
require 'coveralls'
|
4
4
|
|
5
|
-
|
6
|
-
Dir.glob(File.dirname(__FILE__) + "/fake_app/**/*.rb").each { |file| require file }
|
5
|
+
Coveralls.wear!
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
CreateUsers.migrate(:up)
|
11
|
-
CreateAdmins.migrate(:up)
|
7
|
+
Dir.glob(File.dirname(__FILE__) + '/../lib/**/*.rb').each { |file| require file }
|
8
|
+
Dir.glob(File.dirname(__FILE__) + '/fake_app/*.rb').each { |file| require file }
|
@@ -1,95 +1,95 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe ValidatesCpf::Cpf do
|
4
4
|
context "should be invalid with" do
|
5
5
|
it "blank number" do
|
6
|
-
|
6
|
+
ValidatesCpf::Cpf.new('').should_not be_valid
|
7
7
|
end
|
8
8
|
|
9
9
|
it "345.65.67.3 as number" do
|
10
|
-
|
10
|
+
ValidatesCpf::Cpf.new('345.65.67.3').should_not be_valid
|
11
11
|
end
|
12
12
|
|
13
13
|
it "567.765-87698 as number" do
|
14
|
-
|
14
|
+
ValidatesCpf::Cpf.new('567.765-87698').should_not be_valid
|
15
15
|
end
|
16
16
|
|
17
17
|
it "345456-654-01 as number" do
|
18
|
-
|
18
|
+
ValidatesCpf::Cpf.new('345456-654-01').should_not be_valid
|
19
19
|
end
|
20
20
|
|
21
21
|
it "123456 as number" do
|
22
|
-
|
22
|
+
ValidatesCpf::Cpf.new('123456').should_not be_valid
|
23
23
|
end
|
24
24
|
|
25
25
|
it "23342345699 as number" do
|
26
|
-
|
26
|
+
ValidatesCpf::Cpf.new('23342345699').should_not be_valid
|
27
27
|
end
|
28
28
|
|
29
29
|
it "34.543.567-98 as number" do
|
30
|
-
|
30
|
+
ValidatesCpf::Cpf.new('34.543.567-98').should_not be_valid
|
31
31
|
end
|
32
32
|
|
33
33
|
it "456.676456-87 as number" do
|
34
|
-
|
34
|
+
ValidatesCpf::Cpf.new('456.676456-87').should_not be_valid
|
35
35
|
end
|
36
36
|
|
37
37
|
it "333333333-33 as number" do
|
38
|
-
|
38
|
+
ValidatesCpf::Cpf.new('333333333-33').should_not be_valid
|
39
39
|
end
|
40
40
|
|
41
41
|
it "00000000000 as number" do
|
42
|
-
|
42
|
+
ValidatesCpf::Cpf.new('00000000000').should_not be_valid
|
43
43
|
end
|
44
44
|
|
45
45
|
it "000.000.000-00 as number" do
|
46
|
-
|
46
|
+
ValidatesCpf::Cpf.new('000.000.000-00').should_not be_valid
|
47
47
|
end
|
48
48
|
|
49
49
|
it "111.444.777-3500 as number" do
|
50
|
-
|
50
|
+
ValidatesCpf::Cpf.new('111.444.777-3500').should_not be_valid
|
51
51
|
end
|
52
52
|
|
53
53
|
it "11144477735AB as number" do
|
54
|
-
|
54
|
+
ValidatesCpf::Cpf.new('11144477735AB').should_not be_valid
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
58
|
context "should be valid with" do
|
59
59
|
it "nil as number" do
|
60
|
-
|
60
|
+
ValidatesCpf::Cpf.new(nil).should be_valid
|
61
61
|
end
|
62
62
|
|
63
63
|
it "111.444.777-35 as number" do
|
64
|
-
|
64
|
+
ValidatesCpf::Cpf.new('111.444.777-35').should be_valid
|
65
65
|
end
|
66
66
|
|
67
67
|
it "11144477735 as number" do
|
68
|
-
|
68
|
+
ValidatesCpf::Cpf.new('11144477735').should be_valid
|
69
69
|
end
|
70
70
|
|
71
71
|
it "111.444777-35 as number" do
|
72
|
-
|
72
|
+
ValidatesCpf::Cpf.new('111.444777-35').should be_valid
|
73
73
|
end
|
74
74
|
|
75
75
|
it "111444.777-35 as number" do
|
76
|
-
|
76
|
+
ValidatesCpf::Cpf.new('111444.777-35').should be_valid
|
77
77
|
end
|
78
78
|
|
79
79
|
it "111.444.77735 as number" do
|
80
|
-
|
80
|
+
ValidatesCpf::Cpf.new('111.444.77735').should be_valid
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
84
84
|
context "with a valid value" do
|
85
85
|
it "should return it formatted" do
|
86
|
-
|
86
|
+
ValidatesCpf::Cpf.new('11144477735').number.should == '111.444.777-35'
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
90
90
|
context "with an invalid value" do
|
91
91
|
it "should return as it was" do
|
92
|
-
|
92
|
+
ValidatesCpf::Cpf.new('123456').number.should == '123456'
|
93
93
|
end
|
94
94
|
end
|
95
95
|
end
|
data/spec/validates_cpf_spec.rb
CHANGED
@@ -1,39 +1,41 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe CpfValidator do
|
4
|
-
context
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
context 'when cpf is invalid' do
|
5
|
+
context 'on a non namespaced model' do
|
6
|
+
before :each do
|
7
|
+
@user = User.new(:cpf => '12345')
|
8
|
+
I18n.stub(:t).with(:'activerecord.errors.models.user.attributes.cpf.invalid',
|
9
|
+
:default => :'activerecord.errors.messages.invalid').and_return('is invalid')
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
it 'should set object as invalid' do
|
13
|
+
@user.valid?.should be_false
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
it 'should set an error message on attribute' do
|
17
|
+
@user.valid?
|
18
|
+
@user.errors[:cpf].should == ['is invalid']
|
19
|
+
end
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
21
|
-
context
|
23
|
+
context 'when cpf is valid' do
|
22
24
|
before :each do
|
23
|
-
@user = User.new(:cpf =>
|
25
|
+
@user = User.new(:cpf => '11144477735')
|
24
26
|
end
|
25
27
|
|
26
|
-
it
|
28
|
+
it 'should set object as valid' do
|
27
29
|
@user.valid?.should be_true
|
28
30
|
end
|
29
31
|
|
30
|
-
it
|
32
|
+
it 'should not set an error message on attribute' do
|
31
33
|
@user.valid?
|
32
34
|
@user.errors[:cpf].should be_blank
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
36
|
-
it
|
38
|
+
it 'should accept a nil value' do
|
37
39
|
@user = User.new(:cpf => nil)
|
38
40
|
@user.valid?.should be_true
|
39
41
|
end
|
data/validates_cpf.gemspec
CHANGED
@@ -1,26 +1,24 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
require "validates_cpf/version"
|
2
|
+
require File.expand_path('../lib/validates_cpf/version', __FILE__)
|
4
3
|
|
5
|
-
Gem::Specification.new do |
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
s.summary = %q{CPF Validation GEM}
|
12
|
-
s.description = %q{Validates CPF and test it with matchers in a simple way.}
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = 'validates_cpf'
|
6
|
+
gem.version = ValidatesCpf::VERSION
|
7
|
+
gem.authors = %q{Paulo Henrique Lopes Ribeiro}
|
8
|
+
gem.email = %q{plribeiro3000@gmail.com}
|
9
|
+
gem.summary = %q{Validates CPF and test it with matchers in a simple way.}
|
13
10
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
gem.files = `git ls-files`.split("\n")
|
12
|
+
gem.test_files = `git ls-files -- {test,spec,features,examples,gemfiles}/*`.split("\n")
|
13
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
14
|
+
gem.require_paths = %w(lib)
|
18
15
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
16
|
+
gem.license = 'MIT'
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rake'
|
19
|
+
gem.add_development_dependency 'rspec'
|
20
|
+
gem.add_development_dependency 'shoulda-matchers', '2.0.0'
|
21
|
+
gem.add_development_dependency 'coveralls'
|
22
|
+
|
23
|
+
gem.add_runtime_dependency 'activemodel'
|
24
|
+
end
|
metadata
CHANGED
@@ -1,48 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_cpf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease:
|
4
|
+
version: 2.0.0.rc1
|
5
|
+
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Paulo Henrique Lopes Ribeiro
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-09-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: activemodel
|
16
|
-
requirement: !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: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ! '>='
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: 3.0.0
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
|
-
name: activerecord
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
|
-
requirements:
|
35
|
-
- - ! '>='
|
36
|
-
- !ruby/object:Gem::Version
|
37
|
-
version: 3.0.0
|
38
|
-
type: :development
|
39
|
-
prerelease: false
|
40
|
-
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
|
-
requirements:
|
43
|
-
- - ! '>='
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version: 3.0.0
|
46
14
|
- !ruby/object:Gem::Dependency
|
47
15
|
name: rake
|
48
16
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,7 +34,7 @@ dependencies:
|
|
66
34
|
requirements:
|
67
35
|
- - ! '>='
|
68
36
|
- !ruby/object:Gem::Version
|
69
|
-
version:
|
37
|
+
version: '0'
|
70
38
|
type: :development
|
71
39
|
prerelease: false
|
72
40
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -74,31 +42,31 @@ dependencies:
|
|
74
42
|
requirements:
|
75
43
|
- - ! '>='
|
76
44
|
- !ruby/object:Gem::Version
|
77
|
-
version:
|
45
|
+
version: '0'
|
78
46
|
- !ruby/object:Gem::Dependency
|
79
47
|
name: shoulda-matchers
|
80
48
|
requirement: !ruby/object:Gem::Requirement
|
81
49
|
none: false
|
82
50
|
requirements:
|
83
|
-
- -
|
51
|
+
- - '='
|
84
52
|
- !ruby/object:Gem::Version
|
85
|
-
version:
|
53
|
+
version: 2.0.0
|
86
54
|
type: :development
|
87
55
|
prerelease: false
|
88
56
|
version_requirements: !ruby/object:Gem::Requirement
|
89
57
|
none: false
|
90
58
|
requirements:
|
91
|
-
- -
|
59
|
+
- - '='
|
92
60
|
- !ruby/object:Gem::Version
|
93
|
-
version:
|
61
|
+
version: 2.0.0
|
94
62
|
- !ruby/object:Gem::Dependency
|
95
|
-
name:
|
63
|
+
name: coveralls
|
96
64
|
requirement: !ruby/object:Gem::Requirement
|
97
65
|
none: false
|
98
66
|
requirements:
|
99
67
|
- - ! '>='
|
100
68
|
- !ruby/object:Gem::Version
|
101
|
-
version:
|
69
|
+
version: '0'
|
102
70
|
type: :development
|
103
71
|
prerelease: false
|
104
72
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -106,16 +74,16 @@ dependencies:
|
|
106
74
|
requirements:
|
107
75
|
- - ! '>='
|
108
76
|
- !ruby/object:Gem::Version
|
109
|
-
version:
|
77
|
+
version: '0'
|
110
78
|
- !ruby/object:Gem::Dependency
|
111
|
-
name:
|
79
|
+
name: activemodel
|
112
80
|
requirement: !ruby/object:Gem::Requirement
|
113
81
|
none: false
|
114
82
|
requirements:
|
115
83
|
- - ! '>='
|
116
84
|
- !ruby/object:Gem::Version
|
117
85
|
version: '0'
|
118
|
-
type: :
|
86
|
+
type: :runtime
|
119
87
|
prerelease: false
|
120
88
|
version_requirements: !ruby/object:Gem::Requirement
|
121
89
|
none: false
|
@@ -123,7 +91,7 @@ dependencies:
|
|
123
91
|
- - ! '>='
|
124
92
|
- !ruby/object:Gem::Version
|
125
93
|
version: '0'
|
126
|
-
description:
|
94
|
+
description:
|
127
95
|
email: plribeiro3000@gmail.com
|
128
96
|
executables: []
|
129
97
|
extensions: []
|
@@ -131,27 +99,30 @@ extra_rdoc_files: []
|
|
131
99
|
files:
|
132
100
|
- .gitignore
|
133
101
|
- .rspec
|
102
|
+
- .ruby-gemset
|
103
|
+
- .ruby-version
|
104
|
+
- .travis.yml
|
134
105
|
- Gemfile
|
135
106
|
- LICENSE
|
136
|
-
- README.
|
107
|
+
- README.md
|
137
108
|
- Rakefile
|
109
|
+
- gemfiles/Gemfile.rails3
|
110
|
+
- gemfiles/Gemfile.rails4
|
111
|
+
- lib/cpf_validator.rb
|
112
|
+
- lib/require_a_valid_cpf_matcher.rb
|
138
113
|
- lib/validates_cpf.rb
|
139
114
|
- lib/validates_cpf/cpf.rb
|
140
|
-
- lib/validates_cpf/remarkable/require_a_valid_cpf_matcher.rb
|
141
|
-
- lib/validates_cpf/shoulda-matchers/require_a_valid_cpf_matcher.rb
|
142
115
|
- lib/validates_cpf/version.rb
|
143
116
|
- spec/fake_app/admin.rb
|
144
|
-
- spec/fake_app/db/create_admins.rb
|
145
|
-
- spec/fake_app/db/create_users.rb
|
146
117
|
- spec/fake_app/user.rb
|
118
|
+
- spec/require_a_valid_cpf_matcher_spec.rb
|
147
119
|
- spec/spec_helper.rb
|
148
120
|
- spec/validates_cpf/cpf_spec.rb
|
149
|
-
- spec/validates_cpf/remarkable/require_a_valid_cpf_matcher_spec.rb
|
150
|
-
- spec/validates_cpf/shoulda-matchers/require_a_valid_cpf_matcher_spec.rb
|
151
121
|
- spec/validates_cpf_spec.rb
|
152
122
|
- validates_cpf.gemspec
|
153
|
-
homepage:
|
154
|
-
licenses:
|
123
|
+
homepage:
|
124
|
+
licenses:
|
125
|
+
- MIT
|
155
126
|
post_install_message:
|
156
127
|
rdoc_options: []
|
157
128
|
require_paths:
|
@@ -165,13 +136,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
165
136
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
166
137
|
none: false
|
167
138
|
requirements:
|
168
|
-
- - ! '
|
139
|
+
- - ! '>'
|
169
140
|
- !ruby/object:Gem::Version
|
170
|
-
version:
|
141
|
+
version: 1.3.1
|
171
142
|
requirements: []
|
172
143
|
rubyforge_project:
|
173
|
-
rubygems_version: 1.8.
|
144
|
+
rubygems_version: 1.8.25
|
174
145
|
signing_key:
|
175
146
|
specification_version: 3
|
176
|
-
summary: CPF
|
177
|
-
test_files:
|
147
|
+
summary: Validates CPF and test it with matchers in a simple way.
|
148
|
+
test_files:
|
149
|
+
- gemfiles/Gemfile.rails3
|
150
|
+
- gemfiles/Gemfile.rails4
|
151
|
+
- spec/fake_app/admin.rb
|
152
|
+
- spec/fake_app/user.rb
|
153
|
+
- spec/require_a_valid_cpf_matcher_spec.rb
|
154
|
+
- spec/spec_helper.rb
|
155
|
+
- spec/validates_cpf/cpf_spec.rb
|
156
|
+
- spec/validates_cpf_spec.rb
|
data/README.rdoc
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
= ValidatesCPF {<img src="https://secure.travis-ci.org/plribeiro3000/validates_cpf.png" />}[http://travis-ci.org/plribeiro3000/validates_cpf]
|
2
|
-
|
3
|
-
Rails gem to validate CPF. Don't forget to check {ValidatesCnpj}[https://github.com/plribeiro3000/validates_cnpj], {ValidatesTelephone}[https://github.com/plribeiro3000/validates_telephone] and {ValidatesHost}[https://github.com/plribeiro3000/validates_host].
|
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
|
-
|
18
|
-
== Test
|
19
|
-
|
20
|
-
This gem has builtin matchers for shoulda-matchers and remarkable.
|
21
|
-
|
22
|
-
== Notes
|
23
|
-
|
24
|
-
Since version 1.0.0, it isn't necessary to load any file inside your spec_helper anymore.
|
25
|
-
|
26
|
-
|
27
|
-
== Contribute
|
28
|
-
|
29
|
-
Fork the project and send me a Pull Request. =D
|
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'remarkable/active_model'
|
2
|
-
|
3
|
-
module Remarkable
|
4
|
-
module ActiveModel
|
5
|
-
module Matchers
|
6
|
-
class RequireAValidCpfMatcher < Remarkable::ActiveModel::Base
|
7
|
-
arguments :cpf
|
8
|
-
|
9
|
-
collection_assertions :disallow_invalid_value, :allow_valid_value
|
10
|
-
|
11
|
-
protected
|
12
|
-
|
13
|
-
def disallow_invalid_value
|
14
|
-
@subject.cpf = '123456'
|
15
|
-
@subject.valid?.errors[:cpf].should == ['is invalid']
|
16
|
-
end
|
17
|
-
|
18
|
-
def allow_valid_value
|
19
|
-
@subject.cpf = '764.579.437-21'
|
20
|
-
@subject.valid?.errors[:cpf].should == []
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def require_a_valid_cpf(*args, &block)
|
25
|
-
RequireAValidCpfMatcher.new(*args, &block).spec(self)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
require "shoulda-matchers"
|
2
|
-
|
3
|
-
module Shoulda
|
4
|
-
module Matchers
|
5
|
-
module ActiveModel
|
6
|
-
def require_a_valid_cpf(attr = :cpf)
|
7
|
-
RequireAValidCpfMatcher.new(attr)
|
8
|
-
end
|
9
|
-
|
10
|
-
class RequireAValidCpfMatcher < ValidationMatcher
|
11
|
-
def initialize(attribute)
|
12
|
-
@attribute = attribute
|
13
|
-
end
|
14
|
-
|
15
|
-
def description
|
16
|
-
"require a valid CPF number"
|
17
|
-
end
|
18
|
-
|
19
|
-
def matches?(subject)
|
20
|
-
super(subject)
|
21
|
-
|
22
|
-
disallows_invalid_value and allows_valid_value
|
23
|
-
end
|
24
|
-
|
25
|
-
private
|
26
|
-
|
27
|
-
def disallows_invalid_value
|
28
|
-
disallows_value_of("123456")
|
29
|
-
end
|
30
|
-
|
31
|
-
def allows_valid_value
|
32
|
-
allows_value_of("897.546.112-20")
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'remarkable/active_model'
|
3
|
-
|
4
|
-
describe Remarkable::ActiveModel::Matchers::RequireAValidCpfMatcher do
|
5
|
-
before :each do
|
6
|
-
@user = User.new
|
7
|
-
end
|
8
|
-
|
9
|
-
it "should accept on cpf" do
|
10
|
-
@user.should require_a_valid_cpf(:cpf)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should accept without a specified attribute" do
|
14
|
-
@user.should require_a_valid_cpf
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should reject on name" do
|
18
|
-
@user.should_not require_a_valid_cpf(:name)
|
19
|
-
end
|
20
|
-
end
|