Class: BR::CNPJ
- Inherits:
-
Object
- Object
- BR::CNPJ
- Defined in:
- lib/br/cnpj.rb,
ext/CNPJ/ruby_cnpj.c
Instance Attribute Summary collapse
-
#digit ⇒ Object
readonly
Returns the value of attribute digit.
-
#filial ⇒ Object
readonly
Returns the value of attribute filial.
-
#radix ⇒ Object
readonly
Returns the value of attribute radix.
-
#valid ⇒ Object
readonly
Returns the value of attribute valid.
Class Method Summary collapse
-
.format(cnpj) ⇒ Object
(also: formatar)
Format unformated Cnpj.
- .orig_valid? ⇒ Object
-
.unformat(cnpj) ⇒ Object
(also: desformatar)
Unformat formated Cnpj.
-
.valid?(ruby_cnpj) ⇒ Boolean
Returns true if Cnpj is valid Retorns false if Cnpj is invalid.
Instance Method Summary collapse
- #initialize(*args) ⇒ Object constructor
- #is_cgc? ⇒ Boolean
- #to_i ⇒ Object
-
#to_s ⇒ Object
Return the CNPJ with 14 characters.
-
#valid? ⇒ Boolean
Return if the instance CNPJ is valid?.
Constructor Details
#initialize(*args) ⇒ Object
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 |
# File 'ext/CNPJ/ruby_cnpj.c', line 4
static VALUE t_init_cnpj(int argc, VALUE *argv, VALUE self)
{
int radix = 0;
int filial = 0;
int verif = 0;
VALUE valid = Qnil;
if (argc == 2) {
radix = FIX2INT(argv[0]);
if( radix >= 100000000 || radix < 0 )
rb_raise(rb_eArgError, "radix should be greater than -1 or lesser than 10_000_000");
filial = NUM2INT(argv[1]);
if( filial >= 10000 || filial < 1)
rb_raise(rb_eArgError, "filial should be greater than 0 or lesser than 10_000");
verif = calculate_digit(radix,filial);
valid = Qtrue;
}
else if (argc == 1) {
lldiv_t v;
long long cnpj = 0;
if (rb_class_of(argv[0]) == rb_cString)
cnpj = NUM2LL(rb_str_to_inum(argv[0], 10, 0));
else
if (rb_class_of(argv[0]) == rb_cFixnum ||
rb_class_of(argv[0]) == rb_cBignum)
cnpj = NUM2LL(argv[0]);
v = lldiv(cnpj, (long long)100);
verif = (int)v.rem;
v = lldiv(v.quot, (long long)10000);
filial = (int)v.rem;
radix = (int)v.quot;
valid = (verif == calculate_digit(radix,filial)) ? Qtrue : Qfalse;
}
rb_iv_set(self, "@radix", INT2FIX(radix));
rb_iv_set(self, "@filial", INT2FIX(filial));
rb_iv_set(self, "@digit", INT2FIX(verif));
rb_iv_set(self, "@valid", valid);
return self;
}
|
Instance Attribute Details
#digit ⇒ Object (readonly)
Returns the value of attribute digit.
49 50 51 |
# File 'lib/br/cnpj.rb', line 49 def digit @digit end |
#filial ⇒ Object (readonly)
Returns the value of attribute filial.
49 50 51 |
# File 'lib/br/cnpj.rb', line 49 def filial @filial end |
#radix ⇒ Object (readonly)
Returns the value of attribute radix.
49 50 51 |
# File 'lib/br/cnpj.rb', line 49 def radix @radix end |
#valid ⇒ Object (readonly)
Returns the value of attribute valid.
49 50 51 |
# File 'lib/br/cnpj.rb', line 49 def valid @valid end |
Class Method Details
.format(cnpj) ⇒ Object Also known as: formatar
Format unformated Cnpj
Cnpj.format(191) # => ‘00.000.000/0001-91’ Cnpj.formatar(30553786000135) # => “30.553.786/0001-35”
14 15 16 17 |
# File 'lib/br/cnpj.rb', line 14 def format(cnpj) cnpj = cnpj.to_s.rjust(14, "0") "%s.%s.%s/%s-%s" % [cnpj[0,2], cnpj[2,3], cnpj[5,3], cnpj[8,4], cnpj[12,2]] end |
.orig_valid? ⇒ Object
7 |
# File 'lib/br/cnpj.rb', line 7 alias_method :orig_valid?, :valid? |
.unformat(cnpj) ⇒ Object Also known as: desformatar
Unformat formated Cnpj
Cnpj.desformatar(“02.716.485/0001-40”) # => “02716485000140” Cnpj.unformat(“00.086.001/0001-04”) # => “00086001000104”
27 28 29 |
# File 'lib/br/cnpj.rb', line 27 def unformat(cnpj) cnpj.gsub(/\D/, '') end |
.valid?(ruby_cnpj) ⇒ Boolean
Returns true if Cnpj is valid Retorns false if Cnpj is invalid
CNPJ.valid?(191) # => true CNPJ.valid?(123456789101) # => false
40 41 42 43 44 45 46 |
# File 'lib/br/cnpj.rb', line 40 def valid?(cnpj) if cnpj.is_a? String orig_valid?(cnpj.to_i) else orig_valid?(cnpj) end end |
Instance Method Details
#is_cgc? ⇒ Boolean
48 49 50 51 52 53 54 55 |
# File 'ext/CNPJ/ruby_cnpj.c', line 48
static VALUE rb_is_cgc(VALUE self){
long long radix=0;
int filial = 0, verify_digit = 0;
radix = NUM2INT(rb_iv_get(self, "@radix"));
filial = NUM2INT(rb_iv_get(self, "@filial"));
verify_digit = NUM2INT(rb_iv_get(self, "@digit"));
return is_cgc((long long)radix * 1000000 + filial * 100 + verify_digit) ? Qtrue : Qfalse;
}
|
#to_i ⇒ Object
59 60 61 |
# File 'lib/br/cnpj.rb', line 59 def to_i @radix * 1_000_000 + @filial * 100 + @digit end |
#to_s ⇒ Object
Return the CNPJ with 14 characters
CNPJ.new(191).to_s # => ‘00000000000191’
67 68 69 |
# File 'lib/br/cnpj.rb', line 67 def to_s "%014d" % to_i end |
#valid? ⇒ Boolean
Return if the instance CNPJ is valid?
Cnpj.new(191).valid? # => true
55 56 57 |
# File 'lib/br/cnpj.rb', line 55 def valid? @valid end |