validates_cnpj 1.1.3 → 2.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 +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/cnpj_validator.rb +5 -0
- data/lib/require_a_valid_cnpj_matcher.rb +23 -0
- data/lib/validates_cnpj/cnpj.rb +52 -50
- data/lib/validates_cnpj/version.rb +2 -2
- data/lib/validates_cnpj.rb +4 -10
- data/spec/fake_app/admin.rb +2 -1
- data/spec/fake_app/company.rb +13 -1
- data/spec/spec_helper.rb +6 -9
- data/spec/{validates_cnpj/shoulda-matchers/validate_as_cnpj_matcher_spec.rb → validate_as_cnpj_matcher_spec.rb} +0 -0
- data/spec/validates_cnpj/cnpj_spec.rb +47 -47
- data/validates_cnpj.gemspec +19 -20
- metadata +43 -45
- data/.rvmrc +0 -1
- data/README.rdoc +0 -27
- data/lib/validates_cnpj/remarkable/require_a_valid_cnpj_matcher.rb +0 -29
- data/lib/validates_cnpj/shoulda-matchers/require_a_valid_cnpj_matcher.rb +0 -37
- data/spec/fake_app/db/create_admins.rb +0 -11
- data/spec/fake_app/db/create_companies.rb +0 -12
- data/spec/validates_cnpj/remarkable/validate_as_cnpj_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
|
+
1.9.3
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
validates_cnpj
|
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
|
+
# ValidatesCnpj
|
2
|
+
|
3
|
+
[](http://badge.fury.io/rb/validates_cnpj) [](http://travis-ci.org/plribeiro3000/validates_cnpj) [](https://gemnasium.com/plribeiro3000/validates_cnpj) [](https://coveralls.io/r/plribeiro3000/validates_cnpj) [](https://codeclimate.com/github/plribeiro3000/validates_cnpj)
|
4
|
+
|
5
|
+
Validates cnpj and test it in a simple way
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'validates_cnpj'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install validates_cnpj
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Just use as any other validator:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class User < ActiveRecord::Base
|
27
|
+
validates :cnpj, :cnpj => 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,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_cnpj(attr = :cnpj)
|
8
|
+
RequireAValidCnpjMatcher.new(attr)
|
9
|
+
end
|
10
|
+
|
11
|
+
class RequireAValidCnpjMatcher < ValidationMatcher
|
12
|
+
def description
|
13
|
+
'require a valid CNPJ number'
|
14
|
+
end
|
15
|
+
|
16
|
+
def matches?(subject)
|
17
|
+
@subject = subject
|
18
|
+
disallows_value_of('123456') && allows_value_of('51.114.450/0001-46')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/validates_cnpj/cnpj.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 ValidatesCnpj
|
2
|
+
class Cnpj
|
3
|
+
def initialize(number)
|
4
|
+
number =~ /^(\d{2}\.?\d{3}\.?\d{3}\/?\d{4})-?(\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.blank?
|
14
|
+
return false unless @pure_number
|
15
|
+
check_cnpj
|
16
|
+
end
|
17
|
+
|
18
|
+
def number
|
19
|
+
@number
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def check_cnpj
|
25
|
+
return false if @cleaned_number.length != 14 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([5, 4, 3, 2, 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([6, 5, 4, 3, 2, 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{2})(\d{3})(\d{3})(\d{4})(\d{2})/
|
51
|
+
@number = "#{$1}.#{$2}.#{$3}/#{$4}-#{$5}"
|
52
|
+
end
|
51
53
|
end
|
52
54
|
end
|
@@ -1,3 +1,3 @@
|
|
1
1
|
module ValidatesCnpj
|
2
|
-
VERSION =
|
3
|
-
end
|
2
|
+
VERSION = '2.0.0'
|
3
|
+
end
|
data/lib/validates_cnpj.rb
CHANGED
@@ -1,12 +1,6 @@
|
|
1
|
-
require
|
1
|
+
require 'cnpj_validator'
|
2
|
+
require 'require_a_valid_cnpj_matcher' if defined?(::Shoulda)
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
require "validates_cnpj/remarkable/require_a_valid_cnpj_matcher" if defined?(::Remarkable)
|
6
|
-
|
7
|
-
class CnpjValidator < 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 CNPJ.new(value).valid?
|
11
|
-
end
|
4
|
+
module ValidatesCnpj
|
5
|
+
autoload :Cnpj, 'validates_cnpj/cnpj'
|
12
6
|
end
|
data/spec/fake_app/admin.rb
CHANGED
data/spec/fake_app/company.rb
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
-
class Company
|
1
|
+
class Company
|
2
|
+
include ActiveModel::Validations
|
3
|
+
include ActiveModel::Conversion
|
4
|
+
extend ActiveModel::Naming
|
5
|
+
|
6
|
+
attr_accessor :cnpj, :name
|
7
|
+
|
2
8
|
validates :cnpj, :cnpj => 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
|
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
|
-
CreateCompanies.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 }
|
File without changes
|
@@ -1,91 +1,91 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
4
|
-
context
|
5
|
-
it
|
6
|
-
|
3
|
+
describe ValidatesCnpj::Cnpj do
|
4
|
+
context 'should be invalid with' do
|
5
|
+
it '04.22A.284/0001-11 as number' do
|
6
|
+
ValidatesCnpj::Cnpj.new('04.22A.284/0001-11').should_not be_valid
|
7
7
|
end
|
8
8
|
|
9
|
-
it
|
10
|
-
|
9
|
+
it '04.222-284.0001-11 as number' do
|
10
|
+
ValidatesCnpj::Cnpj.new('04.222-284.0001-11').should_not be_valid
|
11
11
|
end
|
12
12
|
|
13
|
-
it
|
14
|
-
|
13
|
+
it '04222/284/0001-11 as number' do
|
14
|
+
ValidatesCnpj::Cnpj.new('04222/284/0001-11').should_not be_valid
|
15
15
|
end
|
16
16
|
|
17
|
-
it
|
18
|
-
|
17
|
+
it '69103604020160 as number' do
|
18
|
+
ValidatesCnpj::Cnpj.new('69103604020160').should_not be_valid
|
19
19
|
end
|
20
20
|
|
21
|
-
it
|
22
|
-
|
21
|
+
it '00000000000000 as number' do
|
22
|
+
ValidatesCnpj::Cnpj.new('00000000000000').should_not be_valid
|
23
23
|
end
|
24
24
|
|
25
|
-
it
|
26
|
-
|
25
|
+
it '69.103.604/0001-61 as number' do
|
26
|
+
ValidatesCnpj::Cnpj.new('69.103.604/0001-61').should_not be_valid
|
27
27
|
end
|
28
28
|
|
29
|
-
it
|
30
|
-
|
29
|
+
it '01618211000264 as number' do
|
30
|
+
ValidatesCnpj::Cnpj.new('01618211000264').should_not be_valid
|
31
31
|
end
|
32
32
|
|
33
|
-
it
|
34
|
-
|
33
|
+
it '691036040001-601 as number' do
|
34
|
+
ValidatesCnpj::Cnpj.new('691036040001-601').should_not be_valid
|
35
35
|
end
|
36
36
|
|
37
|
-
it
|
38
|
-
|
37
|
+
it '69103604000160a as number' do
|
38
|
+
ValidatesCnpj::Cnpj.new('69103604000160a').should_not be_valid
|
39
39
|
end
|
40
40
|
|
41
|
-
it
|
42
|
-
|
41
|
+
it '69103604000160ABC as number' do
|
42
|
+
ValidatesCnpj::Cnpj.new('69103604000160ABC').should_not be_valid
|
43
43
|
end
|
44
44
|
|
45
|
-
it
|
46
|
-
|
47
|
-
end
|
48
|
-
|
49
|
-
it "6910360400016000 as number" do
|
50
|
-
CNPJ.new('6910360400016000').should_not be_valid
|
45
|
+
it '6910360400016000 as number' do
|
46
|
+
ValidatesCnpj::Cnpj.new('6910360400016000').should_not be_valid
|
51
47
|
end
|
52
48
|
end
|
53
49
|
|
54
|
-
context
|
55
|
-
it
|
56
|
-
|
50
|
+
context 'should be valid with' do
|
51
|
+
it 'blank number' do
|
52
|
+
ValidatesCnpj::Cnpj.new('').should be_valid
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'nil as number' do
|
56
|
+
ValidatesCnpj::Cnpj.new(nil).should be_valid
|
57
57
|
end
|
58
58
|
|
59
|
-
it
|
60
|
-
|
59
|
+
it '69103604000160 as number' do
|
60
|
+
ValidatesCnpj::Cnpj.new('69103604000160').should be_valid
|
61
61
|
end
|
62
62
|
|
63
|
-
it
|
64
|
-
|
63
|
+
it '69.103.604/0001-60 as number' do
|
64
|
+
ValidatesCnpj::Cnpj.new('69.103.604/0001-60').should be_valid
|
65
65
|
end
|
66
66
|
|
67
|
-
it
|
68
|
-
|
67
|
+
it '01518211/000264 as number' do
|
68
|
+
ValidatesCnpj::Cnpj.new('01518211/000264').should be_valid
|
69
69
|
end
|
70
70
|
|
71
|
-
it
|
72
|
-
|
71
|
+
it '01.5182110002-64 as number' do
|
72
|
+
ValidatesCnpj::Cnpj.new('01.5182110002-64').should be_valid
|
73
73
|
end
|
74
74
|
|
75
|
-
it
|
76
|
-
|
75
|
+
it '00.000.000/1447-89 as number' do
|
76
|
+
ValidatesCnpj::Cnpj.new('00.000.000/1447-89').should be_valid
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
80
|
-
context
|
81
|
-
it
|
82
|
-
|
80
|
+
context 'with a valid value' do
|
81
|
+
it 'should return it formatted' do
|
82
|
+
ValidatesCnpj::Cnpj.new('69103604000160').number.should == '69.103.604/0001-60'
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
|
-
context
|
87
|
-
it
|
88
|
-
|
86
|
+
context 'with an invalid value' do
|
87
|
+
it 'should return as it was' do
|
88
|
+
ValidatesCnpj::Cnpj.new('123456').number.should == '123456'
|
89
89
|
end
|
90
90
|
end
|
91
91
|
end
|
data/validates_cnpj.gemspec
CHANGED
@@ -1,25 +1,24 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
require "validates_cnpj/version"
|
2
|
+
require File.expand_path('../lib/validates_cnpj/version', __FILE__)
|
4
3
|
|
5
|
-
Gem::Specification.new do |
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
s.summary = %q{CNPJ Validation GEM}
|
12
|
-
s.description = %q{Validates CNPJ and test it with matchers in a simple way.}
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = 'validates_cnpj'
|
6
|
+
gem.version = ValidatesCnpj::VERSION
|
7
|
+
gem.authors = %q{Paulo Henrique Lopes Ribeiro}
|
8
|
+
gem.email = %q{plribeiro3000@gmail.com}
|
9
|
+
gem.summary = %q{Validates CNPJ 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}/*`.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
|
-
|
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'
|
25
24
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_cnpj
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,24 +9,8 @@ authors:
|
|
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
14
|
- !ruby/object:Gem::Dependency
|
31
15
|
name: rake
|
32
16
|
requirement: !ruby/object:Gem::Requirement
|
@@ -50,7 +34,7 @@ dependencies:
|
|
50
34
|
requirements:
|
51
35
|
- - ! '>='
|
52
36
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
37
|
+
version: '0'
|
54
38
|
type: :development
|
55
39
|
prerelease: false
|
56
40
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -58,48 +42,48 @@ dependencies:
|
|
58
42
|
requirements:
|
59
43
|
- - ! '>='
|
60
44
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
45
|
+
version: '0'
|
62
46
|
- !ruby/object:Gem::Dependency
|
63
47
|
name: shoulda-matchers
|
64
48
|
requirement: !ruby/object:Gem::Requirement
|
65
49
|
none: false
|
66
50
|
requirements:
|
67
|
-
- -
|
51
|
+
- - '='
|
68
52
|
- !ruby/object:Gem::Version
|
69
|
-
version:
|
53
|
+
version: 2.0.0
|
70
54
|
type: :development
|
71
55
|
prerelease: false
|
72
56
|
version_requirements: !ruby/object:Gem::Requirement
|
73
57
|
none: false
|
74
58
|
requirements:
|
75
|
-
- -
|
59
|
+
- - '='
|
76
60
|
- !ruby/object:Gem::Version
|
77
|
-
version:
|
61
|
+
version: 2.0.0
|
78
62
|
- !ruby/object:Gem::Dependency
|
79
|
-
name:
|
63
|
+
name: coveralls
|
80
64
|
requirement: !ruby/object:Gem::Requirement
|
81
65
|
none: false
|
82
66
|
requirements:
|
83
|
-
- - '
|
67
|
+
- - ! '>='
|
84
68
|
- !ruby/object:Gem::Version
|
85
|
-
version:
|
69
|
+
version: '0'
|
86
70
|
type: :development
|
87
71
|
prerelease: false
|
88
72
|
version_requirements: !ruby/object:Gem::Requirement
|
89
73
|
none: false
|
90
74
|
requirements:
|
91
|
-
- - '
|
75
|
+
- - ! '>='
|
92
76
|
- !ruby/object:Gem::Version
|
93
|
-
version:
|
77
|
+
version: '0'
|
94
78
|
- !ruby/object:Gem::Dependency
|
95
|
-
name:
|
79
|
+
name: activemodel
|
96
80
|
requirement: !ruby/object:Gem::Requirement
|
97
81
|
none: false
|
98
82
|
requirements:
|
99
83
|
- - ! '>='
|
100
84
|
- !ruby/object:Gem::Version
|
101
85
|
version: '0'
|
102
|
-
type: :
|
86
|
+
type: :runtime
|
103
87
|
prerelease: false
|
104
88
|
version_requirements: !ruby/object:Gem::Requirement
|
105
89
|
none: false
|
@@ -107,7 +91,7 @@ dependencies:
|
|
107
91
|
- - ! '>='
|
108
92
|
- !ruby/object:Gem::Version
|
109
93
|
version: '0'
|
110
|
-
description:
|
94
|
+
description:
|
111
95
|
email: plribeiro3000@gmail.com
|
112
96
|
executables: []
|
113
97
|
extensions: []
|
@@ -115,28 +99,30 @@ extra_rdoc_files: []
|
|
115
99
|
files:
|
116
100
|
- .gitignore
|
117
101
|
- .rspec
|
118
|
-
- .
|
102
|
+
- .ruby-gemset
|
103
|
+
- .ruby-version
|
104
|
+
- .travis.yml
|
119
105
|
- Gemfile
|
120
106
|
- LICENSE
|
121
|
-
- README.
|
107
|
+
- README.md
|
122
108
|
- Rakefile
|
109
|
+
- gemfiles/Gemfile.rails3
|
110
|
+
- gemfiles/Gemfile.rails4
|
111
|
+
- lib/cnpj_validator.rb
|
112
|
+
- lib/require_a_valid_cnpj_matcher.rb
|
123
113
|
- lib/validates_cnpj.rb
|
124
114
|
- lib/validates_cnpj/cnpj.rb
|
125
|
-
- lib/validates_cnpj/remarkable/require_a_valid_cnpj_matcher.rb
|
126
|
-
- lib/validates_cnpj/shoulda-matchers/require_a_valid_cnpj_matcher.rb
|
127
115
|
- lib/validates_cnpj/version.rb
|
128
116
|
- spec/fake_app/admin.rb
|
129
117
|
- spec/fake_app/company.rb
|
130
|
-
- spec/fake_app/db/create_admins.rb
|
131
|
-
- spec/fake_app/db/create_companies.rb
|
132
118
|
- spec/spec_helper.rb
|
119
|
+
- spec/validate_as_cnpj_matcher_spec.rb
|
133
120
|
- spec/validates_cnpj/cnpj_spec.rb
|
134
|
-
- spec/validates_cnpj/remarkable/validate_as_cnpj_matcher_spec.rb
|
135
|
-
- spec/validates_cnpj/shoulda-matchers/validate_as_cnpj_matcher_spec.rb
|
136
121
|
- spec/validates_cnpj_spec.rb
|
137
122
|
- validates_cnpj.gemspec
|
138
|
-
homepage:
|
139
|
-
licenses:
|
123
|
+
homepage:
|
124
|
+
licenses:
|
125
|
+
- MIT
|
140
126
|
post_install_message:
|
141
127
|
rdoc_options: []
|
142
128
|
require_paths:
|
@@ -147,16 +133,28 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
147
133
|
- - ! '>='
|
148
134
|
- !ruby/object:Gem::Version
|
149
135
|
version: '0'
|
136
|
+
segments:
|
137
|
+
- 0
|
138
|
+
hash: -2611803438277467943
|
150
139
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
140
|
none: false
|
152
141
|
requirements:
|
153
142
|
- - ! '>='
|
154
143
|
- !ruby/object:Gem::Version
|
155
144
|
version: '0'
|
145
|
+
segments:
|
146
|
+
- 0
|
147
|
+
hash: -2611803438277467943
|
156
148
|
requirements: []
|
157
149
|
rubyforge_project:
|
158
|
-
rubygems_version: 1.8.
|
150
|
+
rubygems_version: 1.8.25
|
159
151
|
signing_key:
|
160
152
|
specification_version: 3
|
161
|
-
summary: CNPJ
|
162
|
-
test_files:
|
153
|
+
summary: Validates CNPJ and test it with matchers in a simple way.
|
154
|
+
test_files:
|
155
|
+
- spec/fake_app/admin.rb
|
156
|
+
- spec/fake_app/company.rb
|
157
|
+
- spec/spec_helper.rb
|
158
|
+
- spec/validate_as_cnpj_matcher_spec.rb
|
159
|
+
- spec/validates_cnpj/cnpj_spec.rb
|
160
|
+
- spec/validates_cnpj_spec.rb
|
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm use 1.9.3-p194@validates_cnpj
|
data/README.rdoc
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
= ValidatesCNPJ {<img src="https://secure.travis-ci.org/plribeiro3000/validates_cnpj.png" />}[http://travis-ci.org/plribeiro3000/validates_cnpj]
|
2
|
-
|
3
|
-
Rails gem to validate CNPJ. Don't forget to check {ValidatesCpf}[https://github.com/plribeiro3000/validates_cpf], {ValidatesTelephone}[https://github.com/plribeiro3000/validates_telephone] and {ValidatesHost}[https://github.com/plribeiro3000/validates_host].
|
4
|
-
|
5
|
-
== Install
|
6
|
-
|
7
|
-
gem install validates_cnpj
|
8
|
-
|
9
|
-
== Usage
|
10
|
-
|
11
|
-
Lets say you have a model with "cnpj" string column that you want to be a valid CNPJ. Just add this to your model:
|
12
|
-
|
13
|
-
class User < ActiveRecord::Base
|
14
|
-
validates :cnpj, :cnpj => true
|
15
|
-
end
|
16
|
-
|
17
|
-
== Test
|
18
|
-
|
19
|
-
This gem has builtin matchers for shoulda-matchers and remarkable.
|
20
|
-
|
21
|
-
== Notes
|
22
|
-
|
23
|
-
Since version 1.0.0, it isn't necessary to load any file inside your spec_helper anymore.
|
24
|
-
|
25
|
-
== Contribute
|
26
|
-
|
27
|
-
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 RequireAValidCnpjMatcher < Remarkable::ActiveModel::Base
|
7
|
-
arguments :cnpj
|
8
|
-
|
9
|
-
collection_assertions :disallow_invalid_value, :allow_valid_value
|
10
|
-
|
11
|
-
protected
|
12
|
-
|
13
|
-
def disallow_invalid_value
|
14
|
-
@subject.cnpj = '123456'
|
15
|
-
@subject.valid?.errors[:cnpj].should == ['is invalid']
|
16
|
-
end
|
17
|
-
|
18
|
-
def allow_valid_value
|
19
|
-
@subject.cnpj = nil
|
20
|
-
@subject.valid?.errors[:cnpj].should == []
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def require_a_valid_cnpj(*args, &block)
|
25
|
-
RequireAValidCnpjMatcher.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_cnpj(attr = :cnpj)
|
7
|
-
RequireAValidCnpjMatcher.new(attr)
|
8
|
-
end
|
9
|
-
|
10
|
-
class RequireAValidCnpjMatcher < ValidationMatcher
|
11
|
-
def initialize(attribute)
|
12
|
-
@attribute = attribute
|
13
|
-
end
|
14
|
-
|
15
|
-
def description
|
16
|
-
"require a valid CNPJ 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("51.114.450/0001-46")
|
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::RequireAValidCnpjMatcher do
|
5
|
-
before :each do
|
6
|
-
@company = Company.new
|
7
|
-
end
|
8
|
-
|
9
|
-
it "should accept on cnpj" do
|
10
|
-
@company.should require_a_valid_cnpj(:cnpj)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should accept without a specified attribute" do
|
14
|
-
@company.should require_a_valid_cnpj
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should reject on name" do
|
18
|
-
@company.should_not require_a_valid_cnpj(:name)
|
19
|
-
end
|
20
|
-
end
|