br-cnpj 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.rdoc +11 -0
  2. data/lib/br/cnpj.rb +50 -7
  3. data/spec/br-cnpj_spec.rb +108 -16
  4. metadata +3 -3
data/README.rdoc CHANGED
@@ -25,6 +25,17 @@ Lib implementada em C (gera uma lib compilada nativamente) que calcula e valida
25
25
  BR::CNPJ.new(raiz, filial).to_s
26
26
  => '00000000000191'
27
27
 
28
+ BR::CNPJ.new(191).valid?
29
+ => true
30
+
31
+ BR::CNPJ.new(1234567891011).valid?
32
+ => false
33
+
34
+ BR::CNPJ.format(191)
35
+ => '00.000.000/0001-91'
36
+
37
+ BR::CNPJ.unformat('00.000.000/0001-91')
38
+ => '00000000000191'
28
39
 
29
40
 
30
41
  == Copyright
data/lib/br/cnpj.rb CHANGED
@@ -3,23 +3,66 @@ require File.join(File.dirname(__FILE__), '../CNPJ')
3
3
  module BR
4
4
  class CNPJ
5
5
  class << self
6
+
6
7
  alias_method :orig_valid?, :valid?
8
+
9
+ # Formata o Cnpj desformatado
10
+ #
11
+ # Cnpj.formatar(191) # => '00.000.000/0001-91'
12
+ # Cnpj.formatar(30553786000135) # => "30.553.786/0001-35"
13
+ #
14
+ def format(cnpj)
15
+ cnpj = cnpj.to_s.rjust(14, "0")
16
+ "%s.%s.%s/%s-%s" % [cnpj[0,2], cnpj[2,3], cnpj[5,3], cnpj[8,4], cnpj[12,2]]
17
+ end
18
+ alias_method :formatar, :format
19
+
20
+ # Desformata o Cnpj formatado
21
+ #
22
+ # Cnpj.desformatar("02.716.485/0001-40") # => "02716485000140"
23
+ # Cnpj.unformat("00.086.001/0001-04") # => "00086001000104"
24
+ #
25
+ def unformat(cnpj)
26
+ cnpj.gsub(/\D/, '')
27
+ end
28
+ alias_method :desformatar, :unformat
29
+
30
+ # Retorna true se o Cnpj for válido
31
+ # Retorna falso se o Cnpj for inválido
32
+ #
33
+ # CNPJ.valid?(191) # => true
34
+ # CNPJ.valid?(123456789101) # => false
35
+ #
36
+ def valid?(cnpj)
37
+ if cnpj.is_a? String
38
+ orig_valid?(cnpj.to_i)
39
+ else
40
+ orig_valid?(cnpj)
41
+ end
42
+ end
43
+
7
44
  end
45
+ attr_reader :filial, :valid, :raiz, :verif
8
46
 
9
- def self.valid?(cnpj)
10
- if cnpj.is_a? String
11
- orig_valid?(cnpj.to_i)
12
- else
13
- orig_valid?(cnpj)
14
- end
47
+ # Return if the instance CNPJ is valid?
48
+ #
49
+ # Cnpj.new(191).valid? # => true
50
+ #
51
+ def valid?
52
+ valid
15
53
  end
16
-
54
+
17
55
  def to_i
18
56
  @raiz * 1_000_000 + @filial * 100 + @verif
19
57
  end
20
58
 
59
+ # Return the CNPJ with 14 characters
60
+ #
61
+ # CNPJ.new(191).to_s # => '00000000000191'
62
+ #
21
63
  def to_s
22
64
  "%014d" % to_i
23
65
  end
66
+
24
67
  end
25
68
  end
data/spec/br-cnpj_spec.rb CHANGED
@@ -1,37 +1,129 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe BR::CNPJ do
3
+ module BR
4
+
5
+ describe CNPJ do
6
+
7
+ context 'when validate instances' do
8
+
9
+ it 'should return true for the cnpj valid' do
10
+ VALID_CNPJS_INTEGER.each do |valid_cnpj|
11
+ CNPJ.new(valid_cnpj).valid?.should be_true
12
+ end
13
+ end
14
+
15
+ it 'should return false the cnpj invalid' do
16
+ INVALID_CNPJS_INTEGER.each do |invalid_cnpj|
17
+ CNPJ.new(invalid_cnpj).valid?.should be_false
18
+ end
19
+ end
4
20
 
5
- context "when Fixnum" do
21
+ end
22
+
23
+ context "when Fixnum" do
6
24
 
7
- it "should return true for valid cnpjs" do
8
- VALID_CNPJS_INTEGER.each do |valid_cnpj|
9
- BR::CNPJ.valid?(valid_cnpj).should be_true
25
+ it "should return true for valid cnpjs" do
26
+ VALID_CNPJS_INTEGER.each do |valid_cnpj|
27
+ CNPJ.valid?(valid_cnpj).should be_true
28
+ end
29
+ end
30
+
31
+ it "should return false for invalid cnpjs" do
32
+ INVALID_CNPJS_INTEGER.each do |invalid_cnpj|
33
+ CNPJ.valid?(invalid_cnpj).should be_false
34
+ end
10
35
  end
36
+
11
37
  end
38
+
39
+ context "when String" do
12
40
 
13
- it "should return false for invalid cnpjs" do
14
- INVALID_CNPJS_INTEGER.each do |invalid_cnpj|
15
- BR::CNPJ.valid?(invalid_cnpj).should be_false
41
+ it "should return true for valid cnpjs" do
42
+ VALID_CNPJS_STRING.each do |valid_cnpj|
43
+ CNPJ.valid?(valid_cnpj).should be_true
44
+ end
16
45
  end
46
+
47
+ it "should return false for invalid cnpjs" do
48
+ INVALID_CNPJS_STRING.each do |invalid_cnpj|
49
+ CNPJ.valid?(invalid_cnpj).should be_false
50
+ end
51
+ end
52
+
17
53
  end
54
+
55
+ context 'when format cnpj' do
18
56
 
19
- end
57
+ it 'should format the cnpj put zero in the left' do
58
+ CNPJ.format(191).should eql('00.000.000/0001-91')
59
+ end
20
60
 
21
- context "when String" do
61
+ it 'should format the cnpj put zero in the left (cnpj filial)' do
62
+ CNPJ.format(272).should eql('00.000.000/0002-72')
63
+ end
64
+
65
+ it "should format the cnpj put period and stuff" do
66
+ CNPJ.formatar(30553786000135).should eql("30.553.786/0001-35")
67
+ end
22
68
 
23
- it "should return true for valid cnpjs" do
24
- VALID_CNPJS_STRING.each do |valid_cnpj|
25
- BR::CNPJ.valid?(valid_cnpj).should be_true
69
+ it "should format the cnpj" do
70
+ CNPJ.formatar(14712911000156).should eql("14.712.911/0001-56")
26
71
  end
72
+
27
73
  end
28
74
 
29
- it "should return false for invalid cnpjs" do
30
- INVALID_CNPJS_STRING.each do |invalid_cnpj|
31
- BR::CNPJ.valid?(invalid_cnpj).should be_false
75
+ context 'when unformat cnpj' do
76
+
77
+ it "should return only numbers in String" do
78
+ CNPJ.unformat("02.716.485/0001-40").should == "02716485000140"
32
79
  end
80
+
81
+ it "should remove the chars '.', '/' and '-'" do
82
+ CNPJ.unformat("71.153.638/0001-00").should == "71153638000100"
83
+ end
84
+
85
+ it "should remove the chars with '-'" do
86
+ CNPJ.desformatar("47.580.835/0001-21").should == "47580835000121"
87
+ end
88
+
89
+ it "should remove the chars with '/'" do
90
+ CNPJ.desformatar("98.819.832/0001-32").should == "98819832000132"
91
+ end
92
+
93
+ it "should retorn an String" do
94
+ CNPJ.desformatar("00.022.559/0001-13").should be_kind_of(String)
95
+ end
96
+
33
97
  end
98
+
99
+ context 'when to_s' do
100
+
101
+ it "should return a String" do
102
+ CNPJ.new(191).to_s.should be_kind_of(String)
103
+ end
104
+
105
+ it 'should return a String with 14 chars' do
106
+ CNPJ.new(191).to_s.should have(14).items
107
+ end
108
+
109
+ it 'should return the CNPJ in string' do
110
+ CNPJ.new(272).to_s.should == '00000000000272'
111
+ end
112
+
113
+ it 'should return the CNPJ in String' do
114
+ CNPJ.new(191).to_s.should == '00000000000191'
115
+ end
116
+
117
+ end
34
118
 
119
+ context 'when to_i' do
120
+
121
+ it 'should return a Fixnum' do
122
+ CNPJ.new('191').to_i.should be_kind_of(Fixnum)
123
+ end
124
+
125
+ end
126
+
35
127
  end
36
128
 
37
129
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 5
9
- version: 0.1.5
8
+ - 6
9
+ version: 0.1.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Bruno Coimbra
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-16 00:00:00 -03:00
17
+ date: 2010-04-19 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency