validates_cpf 0.2.2 → 1.0.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.
- data/.gitignore +1 -1
- data/LICENSE +20 -0
- data/README.rdoc +10 -9
- data/lib/validates_cpf/remarkable/require_a_valid_cpf_matcher.rb +29 -0
- data/lib/validates_cpf/shoulda-matchers/{validate_as_cpf_matcher.rb → require_a_valid_cpf_matcher.rb} +5 -13
- data/lib/validates_cpf/version.rb +1 -1
- data/lib/validates_cpf.rb +4 -1
- data/spec/fake_app/db/create_admins.rb +3 -2
- data/spec/fake_app/db/create_users.rb +1 -0
- data/spec/validates_cpf/remarkable/require_a_valid_cpf_matcher_spec.rb +20 -0
- data/spec/validates_cpf/shoulda-matchers/require_a_valid_cpf_matcher_spec.rb +20 -0
- data/spec/validates_cpf_spec.rb +2 -2
- data/validates_cpf.gemspec +2 -2
- metadata +52 -21
- data/lib/validates_cpf/remarkable/validate_as_cpf_matcher.rb +0 -34
- data/spec/remarkable/validate_as_cpf_matcher_spec.rb +0 -26
- data/spec/shoulda-matchers/validate_as_cpf_matcher_spec.rb +0 -26
data/.gitignore
CHANGED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
(The MIT License)
|
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 NONINFRINGEMENT.
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
18
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
19
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
20
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= ValidatesCPF {<img src="https://secure.travis-ci.org/plribeiro3000/validates_cpf.png" />}[http://travis-ci.org/plribeiro3000/validates_cpf]
|
2
2
|
|
3
|
-
Rails gem to validate CPF. Don't forget to check {ValidatesCnpj}[https://github.com/plribeiro3000/validates_cnpj]
|
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
4
|
|
5
5
|
== Install
|
6
6
|
|
@@ -10,23 +10,24 @@ gem install validates_cpf
|
|
10
10
|
|
11
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
12
|
|
13
|
+
```ruby
|
13
14
|
class User < ActiveRecord::Base
|
14
15
|
validates :cpf, :cpf => true
|
15
16
|
end
|
17
|
+
```
|
16
18
|
|
17
19
|
== Test
|
18
20
|
|
19
|
-
This gem has matchers for shoulda-matchers and remarkable.
|
20
|
-
|
21
|
-
If you are using shoulda-matchers, add this line to your spec_helper.rb :
|
22
|
-
require "validates_cpf/shoulda-matchers/validate_as_cpf_matcher"
|
23
|
-
If you are using remarkable, add this line to your spec_helper.rb :
|
24
|
-
require "validates_cpf/remarkable/validate_as_cpf_matcher"
|
21
|
+
This gem has buitin matchers for shoulda-matchers and remarkable.
|
25
22
|
|
26
23
|
=== How?
|
27
24
|
|
28
|
-
You should use
|
25
|
+
You should use require_a_valid_cpf(:attribute) or skip the attribute part if the attribute name is cpf.
|
29
26
|
|
30
27
|
== Goal
|
31
28
|
|
32
|
-
This project is based on brcpfcnpj gem and his intention it to mantain a cleaner code to validate CPF and easy matchers to test it.
|
29
|
+
This project is based on brcpfcnpj gem and his intention it to mantain a cleaner code to validate CPF and easy matchers to test it.
|
30
|
+
|
31
|
+
== Contribute
|
32
|
+
|
33
|
+
Fork the project and send me a Pull Request. =D
|
@@ -0,0 +1,29 @@
|
|
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
|
@@ -3,27 +3,23 @@ require "shoulda-matchers"
|
|
3
3
|
module Shoulda
|
4
4
|
module Matchers
|
5
5
|
module ActiveModel
|
6
|
-
def
|
7
|
-
|
6
|
+
def require_a_valid_cpf(attr = :cpf)
|
7
|
+
RequireAValidCpfMatcher.new(attr)
|
8
8
|
end
|
9
9
|
|
10
|
-
class
|
10
|
+
class RequireAValidCpfMatcher < ValidationMatcher
|
11
11
|
def initialize(attribute)
|
12
12
|
@attribute = attribute
|
13
13
|
end
|
14
14
|
|
15
15
|
def description
|
16
|
-
"
|
17
|
-
end
|
18
|
-
|
19
|
-
def failure_message
|
20
|
-
"expected #{@attribute} to be validated as a valid CPF number"
|
16
|
+
"require a valid CPF number"
|
21
17
|
end
|
22
18
|
|
23
19
|
def matches?(subject)
|
24
20
|
super(subject)
|
25
21
|
|
26
|
-
disallows_invalid_value and allows_valid_value
|
22
|
+
disallows_invalid_value and allows_valid_value
|
27
23
|
end
|
28
24
|
|
29
25
|
private
|
@@ -35,10 +31,6 @@ module Shoulda
|
|
35
31
|
def allows_valid_value
|
36
32
|
allows_value_of("897.546.112-20")
|
37
33
|
end
|
38
|
-
|
39
|
-
def allows_nil_value
|
40
|
-
allows_value_of(nil)
|
41
|
-
end
|
42
34
|
end
|
43
35
|
end
|
44
36
|
end
|
data/lib/validates_cpf.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
-
require "active_model"
|
2
1
|
require "validates_cpf/cpf"
|
3
2
|
|
3
|
+
#Rspec Matchers
|
4
|
+
require "validates_cpf/shoulda-matchers/require_a_valid_cpf_matcher" if defined?(::Shoulda)
|
5
|
+
require "validates_cpf/remarkable/require_a_valid_cpf_matcher" if defined?(::Remarkable)
|
6
|
+
|
4
7
|
class CpfValidator < ActiveModel::EachValidator
|
5
8
|
def validate_each(record, attribute, value)
|
6
9
|
record.errors[attribute] << I18n.t("errors.messages.invalid") unless CPF.new(value).valid?
|
@@ -0,0 +1,20 @@
|
|
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
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'shoulda-matchers'
|
3
|
+
|
4
|
+
describe Shoulda::Matchers::ActiveModel::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
|
data/spec/validates_cpf_spec.rb
CHANGED
@@ -10,7 +10,7 @@ describe CpfValidator do
|
|
10
10
|
@user.valid?.should be_false
|
11
11
|
end
|
12
12
|
|
13
|
-
it "should set an error on attribute" do
|
13
|
+
it "should set an error message on attribute" do
|
14
14
|
@user.valid?
|
15
15
|
@user.errors[:cpf].should == ['is invalid']
|
16
16
|
end
|
@@ -25,7 +25,7 @@ describe CpfValidator do
|
|
25
25
|
@user.valid?.should be_true
|
26
26
|
end
|
27
27
|
|
28
|
-
it "should not set an error on attribute" do
|
28
|
+
it "should not set an error message on attribute" do
|
29
29
|
@user.valid?
|
30
30
|
@user.errors[:cpf].should be_blank
|
31
31
|
end
|
data/validates_cpf.gemspec
CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
|
|
22
22
|
|
23
23
|
s.add_development_dependency "rake"
|
24
24
|
s.add_development_dependency "rspec", ">= 2.0.0"
|
25
|
-
s.add_development_dependency "shoulda-matchers", ">= 1.
|
26
|
-
s.add_development_dependency "remarkable_activerecord", "
|
25
|
+
s.add_development_dependency "shoulda-matchers", ">= 1.2.0"
|
26
|
+
s.add_development_dependency "remarkable_activerecord", ">= 4.0.0.alpha4"
|
27
27
|
s.add_development_dependency "sqlite3"
|
28
28
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_cpf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: 3.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: rake
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ! '>='
|
@@ -32,10 +37,15 @@ dependencies:
|
|
32
37
|
version: '0'
|
33
38
|
type: :development
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: rspec
|
38
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
39
49
|
none: false
|
40
50
|
requirements:
|
41
51
|
- - ! '>='
|
@@ -43,32 +53,47 @@ dependencies:
|
|
43
53
|
version: 2.0.0
|
44
54
|
type: :development
|
45
55
|
prerelease: false
|
46
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.0.0
|
47
62
|
- !ruby/object:Gem::Dependency
|
48
63
|
name: shoulda-matchers
|
49
|
-
requirement:
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
50
65
|
none: false
|
51
66
|
requirements:
|
52
67
|
- - ! '>='
|
53
68
|
- !ruby/object:Gem::Version
|
54
|
-
version: 1.
|
69
|
+
version: 1.2.0
|
55
70
|
type: :development
|
56
71
|
prerelease: false
|
57
|
-
version_requirements:
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.2.0
|
58
78
|
- !ruby/object:Gem::Dependency
|
59
79
|
name: remarkable_activerecord
|
60
|
-
requirement:
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
61
81
|
none: false
|
62
82
|
requirements:
|
63
|
-
- -
|
83
|
+
- - ! '>='
|
64
84
|
- !ruby/object:Gem::Version
|
65
85
|
version: 4.0.0.alpha4
|
66
86
|
type: :development
|
67
87
|
prerelease: false
|
68
|
-
version_requirements:
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 4.0.0.alpha4
|
69
94
|
- !ruby/object:Gem::Dependency
|
70
95
|
name: sqlite3
|
71
|
-
requirement:
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
72
97
|
none: false
|
73
98
|
requirements:
|
74
99
|
- - ! '>='
|
@@ -76,7 +101,12 @@ dependencies:
|
|
76
101
|
version: '0'
|
77
102
|
type: :development
|
78
103
|
prerelease: false
|
79
|
-
version_requirements:
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
80
110
|
description: Validates CPF and test it with matchers in a simple way.
|
81
111
|
email: plribeiro3000@gmail.com
|
82
112
|
executables: []
|
@@ -86,21 +116,22 @@ files:
|
|
86
116
|
- .gitignore
|
87
117
|
- .rspec
|
88
118
|
- Gemfile
|
119
|
+
- LICENSE
|
89
120
|
- README.rdoc
|
90
121
|
- Rakefile
|
91
122
|
- lib/validates_cpf.rb
|
92
123
|
- lib/validates_cpf/cpf.rb
|
93
|
-
- lib/validates_cpf/remarkable/
|
94
|
-
- lib/validates_cpf/shoulda-matchers/
|
124
|
+
- lib/validates_cpf/remarkable/require_a_valid_cpf_matcher.rb
|
125
|
+
- lib/validates_cpf/shoulda-matchers/require_a_valid_cpf_matcher.rb
|
95
126
|
- lib/validates_cpf/version.rb
|
96
127
|
- spec/fake_app/admin.rb
|
97
128
|
- spec/fake_app/db/create_admins.rb
|
98
129
|
- spec/fake_app/db/create_users.rb
|
99
130
|
- spec/fake_app/user.rb
|
100
|
-
- spec/remarkable/validate_as_cpf_matcher_spec.rb
|
101
|
-
- spec/shoulda-matchers/validate_as_cpf_matcher_spec.rb
|
102
131
|
- spec/spec_helper.rb
|
103
132
|
- spec/validates_cpf/cpf_spec.rb
|
133
|
+
- spec/validates_cpf/remarkable/require_a_valid_cpf_matcher_spec.rb
|
134
|
+
- spec/validates_cpf/shoulda-matchers/require_a_valid_cpf_matcher_spec.rb
|
104
135
|
- spec/validates_cpf_spec.rb
|
105
136
|
- validates_cpf.gemspec
|
106
137
|
homepage: ''
|
@@ -123,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
154
|
version: '0'
|
124
155
|
requirements: []
|
125
156
|
rubyforge_project: validates_cpf
|
126
|
-
rubygems_version: 1.8.
|
157
|
+
rubygems_version: 1.8.24
|
127
158
|
signing_key:
|
128
159
|
specification_version: 3
|
129
160
|
summary: CPF Validation GEM
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'remarkable/active_record'
|
2
|
-
|
3
|
-
module Remarkable
|
4
|
-
module ActiveRecord
|
5
|
-
module Matchers
|
6
|
-
class ValidateAsCpfMatcher < Remarkable::ActiveRecord::Base
|
7
|
-
arguments :cpf
|
8
|
-
|
9
|
-
collection_assertions :cpf_valid?, :allow_nil?, :formatted_number?
|
10
|
-
|
11
|
-
protected
|
12
|
-
|
13
|
-
def cpf_valid?
|
14
|
-
@subject.cpf = '123456'
|
15
|
-
@subject.valid?.errors[:cpf].should == ['is invalid']
|
16
|
-
end
|
17
|
-
|
18
|
-
def allow_nil?
|
19
|
-
@subject.cpf = nil
|
20
|
-
@subject.valid?.errors[:cpf].should == []
|
21
|
-
end
|
22
|
-
|
23
|
-
def formatted_number?
|
24
|
-
@subject.cpf = '55658208394'
|
25
|
-
@subject.valid?.cpf.should == '556.582.083-94'
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def validate_as_cpf(*args, &block)
|
30
|
-
ValidateAsCpfMatcher.new(*args, &block).spec(self)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'remarkable/active_record'
|
3
|
-
|
4
|
-
describe Remarkable::ActiveRecord::Matchers::ValidateAsCpfMatcher do
|
5
|
-
context "on a attribute which validates cpf" do
|
6
|
-
it "should require a valid CPF" do
|
7
|
-
@user = User.new(:cpf => '123456')
|
8
|
-
@user.should validate_as_cpf(:cpf)
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should allow a nil value" do
|
12
|
-
@user = User.new(:cpf => nil)
|
13
|
-
@user.should validate_as_cpf(:cpf)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
context "on a attribute which not validates cpf" do
|
18
|
-
before do
|
19
|
-
@user = Admin.new(:cpf => '123456')
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should not require a valid CPF" do
|
23
|
-
@user.should_not validate_as_cpf(:cpf)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'shoulda-matchers'
|
3
|
-
|
4
|
-
describe Shoulda::Matchers::ActiveModel::ValidateAsCpfMatcher do
|
5
|
-
context "on a attribute which validates cpf" do
|
6
|
-
it "should require a valid CPF" do
|
7
|
-
@user = User.new(:cpf => '123456')
|
8
|
-
@user.should validate_as_cpf(:cpf)
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should allow a nil value" do
|
12
|
-
@user = User.new(:cpf => nil)
|
13
|
-
@user.should validate_as_cpf(:cpf)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
context "on a attribute which not validates cpf" do
|
18
|
-
before do
|
19
|
-
@user = Admin.new(:cpf => '123456')
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should not require a valid CPF" do
|
23
|
-
@user.should_not validate_as_cpf(:cpf)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|