validates_cnpj 1.1.2 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d7f7bf2a713f6e1ef009efd5302209acf3c458c0124bc4516f7f42c18fbc4c50
4
+ data.tar.gz: 2965ec419a36e3ca0e16e046cf95042e196314f59e76c0ebba11a3a1e3cb2d18
5
+ SHA512:
6
+ metadata.gz: 6a91cc9adf37d0459b5b2842825e120b695ed29270b538f7daf70cbf715ff079ba5105ea5085fbeaa2929e053580da9b72408ce9f3b70dcd5a0d9d000bd2298e
7
+ data.tar.gz: bbbd2b21a57737c85f60d190a6ed1fb7cbe694262b95b9d335cf058b07a3c1f191839c585ecb1f2fff11b248809136d4cac784c62e8ed1d1e12e13cdcf1bafab
data/.gitignore CHANGED
@@ -1,5 +1,19 @@
1
1
  *.gem
2
+ *.rbc
2
3
  .bundle
3
- .rvmrc
4
+ .config
5
+ .yardoc
6
+ .coveralls.yml
7
+ *.swp
4
8
  Gemfile.lock
5
- pkg/*
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ tmp
18
+ .*~
19
+ *~
data/.rspec CHANGED
@@ -1 +1 @@
1
- --colour --format documentation
1
+ --colour --format documentation --profile
data/.rubocop.yml ADDED
@@ -0,0 +1,17 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.2
3
+
4
+ Metrics/AbcSize:
5
+ Max: 17
6
+
7
+ Metrics/BlockLength:
8
+ Enabled: false
9
+
10
+ Metrics/LineLength:
11
+ Max: 150
12
+
13
+ Metrics/MethodLength:
14
+ Max: 25
15
+
16
+ Style/Documentation:
17
+ Enabled: false
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ validates_cnpj
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.3
data/.travis.yml ADDED
@@ -0,0 +1,37 @@
1
+ matrix:
2
+ include:
3
+ - rvm: 2.2
4
+ gemfile: gemfiles/Gemfile.rails4
5
+ - rvm: 2.2
6
+ gemfile: gemfiles/Gemfile.rails5
7
+ - rvm: 2.3
8
+ gemfile: gemfiles/Gemfile.rails4
9
+ - rvm: 2.3
10
+ gemfile: gemfiles/Gemfile.rails5
11
+ - rvm: 2.4
12
+ gemfile: gemfiles/Gemfile.rails4
13
+ - rvm: 2.4
14
+ gemfile: gemfiles/Gemfile.rails5
15
+ - rvm: 2.5
16
+ gemfile: gemfiles/Gemfile.rails4
17
+ - rvm: 2.5
18
+ gemfile: gemfiles/Gemfile.rails5
19
+ - rvm: 2.5
20
+ gemfile: gemfiles/Gemfile.rails6
21
+ - rvm: 2.6
22
+ gemfile: gemfiles/Gemfile.rails4
23
+ - rvm: 2.6
24
+ gemfile: gemfiles/Gemfile.rails5
25
+ - rvm: 2.6
26
+ gemfile: gemfiles/Gemfile.rails6
27
+ - rvm: 2.7
28
+ gemfile: gemfiles/Gemfile.rails4
29
+ - rvm: 2.7
30
+ gemfile: gemfiles/Gemfile.rails5
31
+ - rvm: 2.7
32
+ gemfile: gemfiles/Gemfile.rails6
33
+ - rvm: 3.0
34
+ gemfile: gemfiles/Gemfile.rails4
35
+ - rvm: 3.0
36
+ gemfile: gemfiles/Gemfile.rails6
37
+ script: rake complete
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
- source "http://rubygems.org"
1
+ # frozen_string_literal: true
2
+
3
+ source 'http://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in validates_cnpj.gemspec
4
6
  gemspec
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # ValidatesCnpj
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/validates_cnpj.png)](http://badge.fury.io/rb/validates_cnpj) [![Build Status](https://secure.travis-ci.org/plribeiro3000/validates_cnpj.png?branch=master)](http://travis-ci.org/plribeiro3000/validates_cnpj) [![Coverage Status](https://coveralls.io/repos/plribeiro3000/validates_cnpj/badge.png?branch=master)](https://coveralls.io/r/plribeiro3000/validates_cnpj) [![Code Climate](https://codeclimate.com/github/plribeiro3000/validates_cnpj.png)](https://codeclimate.com/github/plribeiro3000/validates_cnpj)
4
+
5
+ Validates cnpj and test it in a simple way.
6
+ Supports ruby 2.2+ and rails 3+.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'validates_cnpj'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install validates_cnpj
21
+
22
+ ## Usage
23
+
24
+ Just use as any other validator:
25
+
26
+ ```ruby
27
+ class Company < ActiveRecord::Base
28
+ validates :cnpj, cnpj: true
29
+ end
30
+ ```
31
+
32
+ To force the attribute to be masked pass option `mask`:
33
+
34
+ ```ruby
35
+ class Company < ActiveRecord::Base
36
+ validates :cnpj, cnpj: { mask: true }
37
+ end
38
+ ```
39
+
40
+ ## Testing
41
+
42
+ Require the matcher:
43
+
44
+ ```ruby
45
+ require 'validates_cnpj/require_a_valid_cnpj_matcher'
46
+ ```
47
+
48
+ Use in your tests:
49
+
50
+ ```ruby
51
+ it { is_expected.to require_a_valid_cnpj } # It will test the attribute :cnpj by default
52
+ it { is_expected.to require_a_valid_cnpj(:id) }
53
+ ```
54
+
55
+ ## Mantainers
56
+ [@plribeiro3000](https://github.com/plribeiro3000)
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
data/Rakefile CHANGED
@@ -1,7 +1,12 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
3
6
 
4
7
  RSpec::Core::RakeTask.new
8
+ RuboCop::RakeTask.new
5
9
 
6
- desc "Default Task"
7
- task :default => [ :spec ]
10
+ desc 'Default Task'
11
+ task default: [:spec]
12
+ task complete: %i[spec rubocop]
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gem 'activesupport', '>= 4.0.0', '< 5.0.0'
4
+
5
+ gemspec :path => '../'
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gem 'activesupport', '>= 5.0.0', '< 6.0.0'
4
+
5
+ gemspec :path => '../'
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activesupport', '>= 6.0.0'
4
+
5
+ gemspec :path => '../'
@@ -1,12 +1,8 @@
1
- require "validates_cnpj/cnpj"
1
+ # frozen_string_literal: true
2
2
 
3
- #Rspec Matchers
4
- require "validates_cnpj/shoulda-matchers/require_a_valid_cnpj_matcher" if defined?(::Shoulda)
5
- require "validates_cnpj/remarkable/require_a_valid_cnpj_matcher" if defined?(::Remarkable)
3
+ require 'validates_cnpj/cnpj_validator'
4
+ require 'validates_cnpj/require_a_valid_cnpj_matcher' if defined?(::Shoulda)
6
5
 
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
12
- end
6
+ module ValidatesCnpj
7
+ autoload :Cnpj, 'validates_cnpj/cnpj'
8
+ end
@@ -1,52 +1,63 @@
1
- class CNPJ
2
- def initialize(number)
3
- number =~ /^(\d{2}\.?\d{3}\.?\d{3}\/?\d{4})-?(\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
1
+ # frozen_string_literal: true
10
2
 
11
- def valid?
12
- return true if @number.nil?
13
- return false unless @pure_number
14
- check_cnpj
15
- end
3
+ module ValidatesCnpj
4
+ class Cnpj
5
+ def initialize(number)
6
+ number =~ %r{^(\d{2}\.?\d{3}\.?\d{3}/?\d{4})-?(\d{2})$}
7
+ @number = number
8
+ @pure_number = Regexp.last_match(1)
9
+ @result = Regexp.last_match(2)
10
+ @cleaned_number = @pure_number.nil? ? nil : @number.gsub(%r{[\./-]}, '')
11
+ format_number! if @pure_number
12
+ end
16
13
 
17
- def number
18
- @number
19
- end
14
+ def valid?
15
+ return true if @number.blank?
16
+ return false unless @pure_number
20
17
 
21
- private
18
+ check_cnpj
19
+ end
22
20
 
23
- def check_cnpj
24
- return false if @cleaned_number.length != 14 or @cleaned_number.scan(/\d/).uniq.length == 1
25
- @result == first_digit_verifier + second_digit_verifier
26
- end
21
+ attr_reader :number
27
22
 
28
- def first_digit_verifier
29
- sum = multiply_and_sum([5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], @pure_number)
30
- digit_verifier(sum%11).to_s
31
- end
23
+ private
32
24
 
33
- def second_digit_verifier
34
- sum = multiply_and_sum([6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], @pure_number + first_digit_verifier)
35
- digit_verifier(sum%11).to_s
36
- end
25
+ def check_cnpj
26
+ return false if (@cleaned_number.length != 14) || (@cleaned_number.scan(/\d/).uniq.length == 1)
37
27
 
38
- def multiply_and_sum(array, number)
39
- multiplied = []
40
- number.scan(/\d{1}/).each_with_index { |e, i| multiplied[i] = e.to_i * array[i] }
41
- multiplied.inject { |s,e| s + e }
42
- end
28
+ @result == first_digit_verifier + second_digit_verifier
29
+ end
43
30
 
44
- def digit_verifier(rest)
45
- rest < 2 ? 0 : 11 - rest
46
- end
31
+ def first_digit_verifier
32
+ sum = multiply_and_sum([5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], @pure_number)
33
+ digit_verifier(sum % 11).to_s
34
+ end
35
+
36
+ def second_digit_verifier
37
+ sum = multiply_and_sum([6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], @pure_number + first_digit_verifier)
38
+ digit_verifier(sum % 11).to_s
39
+ end
40
+
41
+ def multiply_and_sum(array, number)
42
+ multiplied = []
43
+ number.scan(/\d{1}/).each_with_index { |e, i| multiplied[i] = e.to_i * array[i] }
44
+ multiplied.inject { |s, e| s + e }
45
+ end
46
+
47
+ def digit_verifier(rest)
48
+ rest < 2 ? 0 : 11 - rest
49
+ end
50
+
51
+ def format_number!
52
+ @cleaned_number =~ /(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/
53
+
54
+ match1 = Regexp.last_match(1)
55
+ match2 = Regexp.last_match(2)
56
+ match3 = Regexp.last_match(3)
57
+ match4 = Regexp.last_match(4)
58
+ match5 = Regexp.last_match(5)
47
59
 
48
- def format_number!
49
- @cleaned_number =~ /(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/
50
- @number = "#{$1}.#{$2}.#{$3}/#{$4}-#{$5}"
60
+ @number = "#{match1}.#{match2}.#{match3}/#{match4}-#{match5}"
61
+ end
51
62
  end
52
- end
63
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CnpjValidator < ActiveModel::EachValidator
4
+ def validate_each(record, attribute, value)
5
+ cnpj = ValidatesCnpj::Cnpj.new(value)
6
+
7
+ if cnpj.valid?
8
+ record.send("#{attribute}=", cnpj.number) if options[:mask]
9
+ else
10
+ ruby_prior_version_three =
11
+ Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.0.0')
12
+
13
+ if ruby_prior_version_three
14
+ record.errors.add(attribute, :invalid, options)
15
+ else
16
+ record.errors.add(attribute, :invalid, **options)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'shoulda-matchers'
4
+ require 'active_support/core_ext/array/wrap'
5
+
6
+ module Shoulda
7
+ module Matchers
8
+ module ActiveModel
9
+ def require_a_valid_cnpj(attr = :cnpj)
10
+ RequireAValidCnpjMatcher.new(attr)
11
+ end
12
+
13
+ class RequireAValidCnpjMatcher < ValidationMatcher
14
+ def description
15
+ 'require a valid CNPJ number'
16
+ end
17
+
18
+ def matches?(subject)
19
+ @subject = subject
20
+ disallows_value_of('123456') && allows_value_of('51.114.450/0001-46')
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ValidatesCnpj
2
- VERSION = "1.1.2"
4
+ VERSION = '3.1.0'.freeze
3
5
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe CnpjValidator do
6
+ let(:company) { Company.new }
7
+
8
+ context 'when cnpj is invalid' do
9
+ before do
10
+ company.cnpj = 12_345
11
+ allow(I18n).to receive(:t).with(:"activerecord.errors.models.company.attributes.cnpj.invalid",
12
+ default: :"activerecord.errors.messages.invalid").and_return('is invalid')
13
+ end
14
+
15
+ it 'sets object as invalid' do
16
+ expect(company.valid?).to eq(false)
17
+ end
18
+
19
+ it 'sets an error on attribute' do
20
+ company.valid?
21
+ expect(company.errors[:cnpj]).to eq(['is invalid'])
22
+ end
23
+ end
24
+
25
+ context 'when cnpj is valid' do
26
+ before do
27
+ company.cnpj = '37525685000108'
28
+ end
29
+
30
+ it 'sets object as valid' do
31
+ expect(company.valid?).to eq(true)
32
+ end
33
+
34
+ it 'does not set an error on attribute' do
35
+ company.valid?
36
+ expect(company.errors[:cnpj]).to eq([])
37
+ end
38
+ end
39
+
40
+ it 'accepts a nil value' do
41
+ expect(company.valid?).to eq(true)
42
+ end
43
+ end
@@ -1,3 +1,17 @@
1
- class Company < ActiveRecord::Base
2
- validates :cnpj, :cnpj => true
3
- end
1
+ # frozen_string_literal: true
2
+
3
+ class Company
4
+ include ActiveModel::Validations
5
+ include ActiveModel::Conversion
6
+ extend ActiveModel::Naming
7
+
8
+ attr_accessor :cnpj, :name
9
+
10
+ validates :cnpj, cnpj: true
11
+
12
+ def initialize(attributes = {})
13
+ attributes.each do |key, value|
14
+ instance_variable_set("@#{key}", value)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Shoulda::Matchers::ActiveModel::RequireAValidCnpjMatcher do
6
+ let(:company) { Company.new }
7
+
8
+ it 'accepts on cnpj' do
9
+ expect(company).to require_a_valid_cnpj(:cnpj)
10
+ end
11
+
12
+ it 'accepts without a specified attribute' do
13
+ expect(company).to require_a_valid_cnpj
14
+ end
15
+
16
+ it 'rejects on name' do
17
+ expect(company).not_to require_a_valid_cnpj(:name)
18
+ end
19
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,11 +1,15 @@
1
- require "rubygems"
2
- require "rspec"
3
- require "active_record"
1
+ # frozen_string_literal: true
4
2
 
5
- Dir.glob(File.dirname(__FILE__) + "/../lib/**/*.rb").each { |file| require file }
6
- Dir.glob(File.dirname(__FILE__) + "/fake_app/**/*.rb").each { |file| require file }
3
+ require 'rspec'
4
+ require 'active_model'
5
+ require 'coveralls'
6
+ require 'shoulda-matchers'
7
7
 
8
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
8
+ RSpec.configure do |config|
9
+ config.include Shoulda::Matchers::ActiveModel
10
+ end
9
11
 
10
- CreateCompanies.migrate(:up)
11
- CreateAdmins.migrate(:up)
12
+ Coveralls.wear!
13
+
14
+ require File.expand_path('lib/validates_cnpj')
15
+ require File.expand_path('spec/fake_app/company')
@@ -1,91 +1,93 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
- describe CNPJ do
4
- context "should be invalid with" do
5
- it "blank number" do
6
- CNPJ.new('').should_not be_valid
5
+ describe ValidatesCnpj::Cnpj do
6
+ context 'with invalid option' do
7
+ it '04.22A.284/0001-11 as number' do
8
+ expect(ValidatesCnpj::Cnpj.new('04.22A.284/0001-11')).not_to be_valid
7
9
  end
8
10
 
9
- it "04.22A.284/0001-11 as number" do
10
- CNPJ.new('04.22A.284/0001-11').should_not be_valid
11
+ it '04.222-284.0001-11 as number' do
12
+ expect(ValidatesCnpj::Cnpj.new('04.222-284.0001-11')).not_to be_valid
11
13
  end
12
14
 
13
- it "04.222-284.0001-11 as number" do
14
- CNPJ.new('04.222-284.0001-11').should_not be_valid
15
+ it '04222/284/0001-11 as number' do
16
+ expect(ValidatesCnpj::Cnpj.new('04222/284/0001-11')).not_to be_valid
15
17
  end
16
18
 
17
- it "04222/284/0001-11 as number" do
18
- CNPJ.new('04222/284/0001-11').should_not be_valid
19
+ it '69103604020160 as number' do
20
+ expect(ValidatesCnpj::Cnpj.new('69103604020160')).not_to be_valid
19
21
  end
20
22
 
21
- it "69103604020160 as number" do
22
- CNPJ.new('69103604020160').should_not be_valid
23
+ it '00000000000000 as number' do
24
+ expect(ValidatesCnpj::Cnpj.new('00000000000000')).not_to be_valid
23
25
  end
24
26
 
25
- it "00000000000000 as number" do
26
- CNPJ.new('00000000000000').should_not be_valid
27
+ it '69.103.604/0001-61 as number' do
28
+ expect(ValidatesCnpj::Cnpj.new('69.103.604/0001-61')).not_to be_valid
27
29
  end
28
30
 
29
- it "69.103.604/0001-61 as number" do
30
- CNPJ.new('69.103.604/0001-61').should_not be_valid
31
+ it '01618211000264 as number' do
32
+ expect(ValidatesCnpj::Cnpj.new('01618211000264')).not_to be_valid
31
33
  end
32
34
 
33
- it "01618211000264 as number" do
34
- CNPJ.new('01618211000264').should_not be_valid
35
+ it '691036040001-601 as number' do
36
+ expect(ValidatesCnpj::Cnpj.new('691036040001-601')).not_to be_valid
35
37
  end
36
38
 
37
- it "691036040001-601 as number" do
38
- CNPJ.new('691036040001-601').should_not be_valid
39
+ it '69103604000160a as number' do
40
+ expect(ValidatesCnpj::Cnpj.new('69103604000160a')).not_to be_valid
39
41
  end
40
42
 
41
- it "69103604000160a as number" do
42
- CNPJ.new('69103604000160a').should_not be_valid
43
+ it '69103604000160ABC as number' do
44
+ expect(ValidatesCnpj::Cnpj.new('69103604000160ABC')).not_to be_valid
43
45
  end
44
46
 
45
- it "69103604000160ABC as number" do
46
- CNPJ.new('69103604000160ABC').should_not be_valid
47
+ it '6910360400016000 as number' do
48
+ expect(ValidatesCnpj::Cnpj.new('6910360400016000')).not_to be_valid
47
49
  end
50
+ end
48
51
 
49
- it "6910360400016000 as number" do
50
- CNPJ.new('6910360400016000').should_not be_valid
52
+ context 'with valid option' do
53
+ it 'blank number' do
54
+ expect(ValidatesCnpj::Cnpj.new('')).to be_valid
51
55
  end
52
- end
53
56
 
54
- context "should be valid with" do
55
- it "nil as number" do
56
- CNPJ.new(nil).should be_valid
57
+ it 'nil as number' do
58
+ expect(ValidatesCnpj::Cnpj.new(nil)).to be_valid
57
59
  end
58
60
 
59
- it "69103604000160 as number" do
60
- CNPJ.new('69103604000160').should be_valid
61
+ it '69103604000160 as number' do
62
+ expect(ValidatesCnpj::Cnpj.new('69103604000160')).to be_valid
61
63
  end
62
64
 
63
- it "69.103.604/0001-60 as number" do
64
- CNPJ.new('69.103.604/0001-60').should be_valid
65
+ it '69.103.604/0001-60 as number' do
66
+ expect(ValidatesCnpj::Cnpj.new('69.103.604/0001-60')).to be_valid
65
67
  end
66
68
 
67
- it "01518211/000264 as number" do
68
- CNPJ.new('01518211/000264').should be_valid
69
+ it '01518211/000264 as number' do
70
+ expect(ValidatesCnpj::Cnpj.new('01518211/000264')).to be_valid
69
71
  end
70
72
 
71
- it "01.5182110002-64 as number" do
72
- CNPJ.new('01.5182110002-64').should be_valid
73
+ it '01.5182110002-64 as number' do
74
+ expect(ValidatesCnpj::Cnpj.new('01.5182110002-64')).to be_valid
73
75
  end
74
76
 
75
- it "00.000.000/1447-89 as number" do
76
- CNPJ.new('00.000.000/1447-89').should be_valid
77
+ it '00.000.000/1447-89 as number' do
78
+ expect(ValidatesCnpj::Cnpj.new('00.000.000/1447-89')).to be_valid
77
79
  end
78
80
  end
79
81
 
80
- context "with a valid value" do
81
- it "should return it formatted" do
82
- CNPJ.new('69103604000160').number.should == '69.103.604/0001-60'
82
+ context 'with a valid value' do
83
+ it 'returns it formatted' do
84
+ expect(ValidatesCnpj::Cnpj.new('69103604000160').number).to eq('69.103.604/0001-60')
83
85
  end
84
86
  end
85
87
 
86
- context "with an invalid value" do
87
- it "should return as it was" do
88
- CNPJ.new('123456').number.should == '123456'
88
+ context 'with an invalid value' do
89
+ it 'returns as it was' do
90
+ expect(ValidatesCnpj::Cnpj.new('123456').number).to eq('123456')
89
91
  end
90
92
  end
91
- end
93
+ end
@@ -1,28 +1,29 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "validates_cnpj/version"
1
+ # frozen_string_literal: true
4
2
 
5
- Gem::Specification.new do |s|
6
- s.name = "validates_cnpj"
7
- s.version = ValidatesCnpj::VERSION
8
- s.authors = ["Paulo Henrique Lopes Ribeiro"]
9
- s.email = %q{plribeiro3000@gmail.com}
10
- s.homepage = ""
11
- s.summary = %q{CNPJ Validation GEM}
12
- s.description = %q{Validates CNPJ and test it with matchers in a simple way.}
3
+ require File.expand_path('lib/validates_cnpj/version', __dir__)
13
4
 
14
- s.add_dependency("activerecord", ">= 3.0.0")
5
+ Gem::Specification.new do |gem|
6
+ gem.name = 'validates_cnpj'
7
+ gem.version = ValidatesCnpj::VERSION
8
+ gem.authors = 'Paulo Henrique Lopes Ribeiro'
9
+ gem.email = 'plribeiro3000@gmail.com'
10
+ gem.summary = 'Validates CNPJ and test it with matchers in a simple way.'
15
11
 
16
- s.rubyforge_project = "validates_cnpj"
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features,examples,gemfiles}/*`.split("\n")
14
+ gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
15
+ gem.require_paths = %w[lib]
17
16
 
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)
17
+ gem.required_ruby_version = '>= 2.2'
22
18
 
23
- s.add_development_dependency "rake"
24
- s.add_development_dependency "rspec", ">= 2.0.0"
25
- s.add_development_dependency "shoulda-matchers", ">= 1.2.0"
26
- s.add_development_dependency "remarkable_activerecord", "= 4.0.0.alpha4"
27
- s.add_development_dependency "sqlite3"
28
- end
19
+ gem.license = 'MIT'
20
+
21
+ gem.add_development_dependency 'coveralls'
22
+ gem.add_development_dependency 'rake'
23
+ gem.add_development_dependency 'rspec'
24
+ gem.add_development_dependency 'rubocop', '< 0.68'
25
+ gem.add_development_dependency 'rubocop-rspec'
26
+ gem.add_development_dependency 'shoulda-matchers'
27
+
28
+ gem.add_runtime_dependency 'activemodel'
29
+ end
metadata CHANGED
@@ -1,162 +1,172 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_cnpj
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
5
- prerelease:
4
+ version: 3.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Paulo Henrique Lopes Ribeiro
9
- autorequire:
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-07-26 00:00:00.000000000 Z
11
+ date: 2021-04-26 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- name: activerecord
14
+ name: coveralls
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
- version: 3.0.0
22
- type: :runtime
19
+ version: '0'
20
+ type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
- version: 3.0.0
26
+ version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
- version: 2.0.0
47
+ version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
- version: 2.0.0
54
+ version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
- name: shoulda-matchers
56
+ name: rubocop
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - "<"
68
60
  - !ruby/object:Gem::Version
69
- version: 1.2.0
61
+ version: '0.68'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - "<"
76
67
  - !ruby/object:Gem::Version
77
- version: 1.2.0
68
+ version: '0.68'
78
69
  - !ruby/object:Gem::Dependency
79
- name: remarkable_activerecord
70
+ name: rubocop-rspec
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - '='
73
+ - - ">="
84
74
  - !ruby/object:Gem::Version
85
- version: 4.0.0.alpha4
75
+ version: '0'
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - '='
80
+ - - ">="
92
81
  - !ruby/object:Gem::Version
93
- version: 4.0.0.alpha4
82
+ version: '0'
94
83
  - !ruby/object:Gem::Dependency
95
- name: sqlite3
84
+ name: shoulda-matchers
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ! '>='
87
+ - - ">="
100
88
  - !ruby/object:Gem::Version
101
89
  version: '0'
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
- - - ! '>='
94
+ - - ">="
108
95
  - !ruby/object:Gem::Version
109
96
  version: '0'
110
- description: Validates CNPJ and test it with matchers in a simple way.
97
+ - !ruby/object:Gem::Dependency
98
+ name: activemodel
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
111
112
  email: plribeiro3000@gmail.com
112
113
  executables: []
113
114
  extensions: []
114
115
  extra_rdoc_files: []
115
116
  files:
116
- - .gitignore
117
- - .rspec
118
- - .rvmrc
117
+ - ".gitignore"
118
+ - ".rspec"
119
+ - ".rubocop.yml"
120
+ - ".ruby-gemset"
121
+ - ".ruby-version"
122
+ - ".travis.yml"
119
123
  - Gemfile
120
124
  - LICENSE
121
- - README.rdoc
125
+ - README.md
122
126
  - Rakefile
127
+ - gemfiles/Gemfile.rails4
128
+ - gemfiles/Gemfile.rails5
129
+ - gemfiles/Gemfile.rails6
123
130
  - lib/validates_cnpj.rb
124
131
  - 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
132
+ - lib/validates_cnpj/cnpj_validator.rb
133
+ - lib/validates_cnpj/require_a_valid_cnpj_matcher.rb
127
134
  - lib/validates_cnpj/version.rb
128
- - spec/fake_app/admin.rb
135
+ - spec/cnpj_validator_spec.rb
129
136
  - spec/fake_app/company.rb
130
- - spec/fake_app/db/create_admins.rb
131
- - spec/fake_app/db/create_companies.rb
137
+ - spec/shoulda/matchers/active_model/require_a_valid_cnpj_matcher_spec.rb
132
138
  - spec/spec_helper.rb
133
139
  - 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
- - spec/validates_cnpj_spec.rb
137
140
  - validates_cnpj.gemspec
138
- homepage: ''
139
- licenses: []
140
- post_install_message:
141
+ homepage:
142
+ licenses:
143
+ - MIT
144
+ metadata: {}
145
+ post_install_message:
141
146
  rdoc_options: []
142
147
  require_paths:
143
148
  - lib
144
149
  required_ruby_version: !ruby/object:Gem::Requirement
145
- none: false
146
150
  requirements:
147
- - - ! '>='
151
+ - - ">="
148
152
  - !ruby/object:Gem::Version
149
- version: '0'
153
+ version: '2.2'
150
154
  required_rubygems_version: !ruby/object:Gem::Requirement
151
- none: false
152
155
  requirements:
153
- - - ! '>='
156
+ - - ">="
154
157
  - !ruby/object:Gem::Version
155
158
  version: '0'
156
159
  requirements: []
157
- rubyforge_project: validates_cnpj
158
- rubygems_version: 1.8.24
159
- signing_key:
160
- specification_version: 3
161
- summary: CNPJ Validation GEM
162
- test_files: []
160
+ rubygems_version: 3.1.6
161
+ signing_key:
162
+ specification_version: 4
163
+ summary: Validates CNPJ and test it with matchers in a simple way.
164
+ test_files:
165
+ - gemfiles/Gemfile.rails4
166
+ - gemfiles/Gemfile.rails5
167
+ - gemfiles/Gemfile.rails6
168
+ - spec/cnpj_validator_spec.rb
169
+ - spec/fake_app/company.rb
170
+ - spec/shoulda/matchers/active_model/require_a_valid_cnpj_matcher_spec.rb
171
+ - spec/spec_helper.rb
172
+ - spec/validates_cnpj/cnpj_spec.rb
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,2 +0,0 @@
1
- class Admin < ActiveRecord::Base
2
- end
@@ -1,11 +0,0 @@
1
- class CreateAdmins < ActiveRecord::Migration
2
- def self.up
3
- create_table :admins do |u|
4
- u.string :cnpj
5
- end
6
- end
7
-
8
- def self.down
9
- drop_table :admins
10
- end
11
- end
@@ -1,12 +0,0 @@
1
- class CreateCompanies < ActiveRecord::Migration
2
- def self.up
3
- create_table :companies do |c|
4
- c.string :cnpj
5
- c.string :name
6
- end
7
- end
8
-
9
- def self.down
10
- drop_table :companies
11
- end
12
- 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
@@ -1,20 +0,0 @@
1
- require 'spec_helper'
2
- require 'shoulda-matchers'
3
-
4
- describe Shoulda::Matchers::ActiveModel::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
@@ -1,40 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe CnpjValidator do
4
- context "when cnpj is invalid" do
5
- before :each do
6
- @company = Company.new(:cnpj => "12345")
7
- I18n.stub(:t).with(:"activerecord.errors.models.company.attributes.cnpj.invalid",
8
- :default => :"activerecord.errors.messages.invalid").and_return("is invalid")
9
- end
10
-
11
- it "should set object as invalid" do
12
- @company.valid?.should be_false
13
- end
14
-
15
- it "should set an error on attribute" do
16
- @company.valid?
17
- @company.errors[:cnpj].should == ['is invalid']
18
- end
19
- end
20
-
21
- context "when cnpj is valid" do
22
- before :each do
23
- @company = Company.new(:cnpj => "37525685000108")
24
- end
25
-
26
- it "should set object as valid" do
27
- @company.valid?.should be_true
28
- end
29
-
30
- it "should not set an error on attribute" do
31
- @company.valid?
32
- @company.errors[:cnpj].should be_blank
33
- end
34
- end
35
-
36
- it "should accept a nil value" do
37
- @company = Company.new(:cnpj => nil)
38
- @company.valid?.should be_true
39
- end
40
- end