Module: ValidatesCpfCnpj::Cnpj
- Defined in:
- lib/validates_cpf_cnpj/cnpj.rb
Class Method Summary collapse
Class Method Details
.valid?(value) ⇒ Boolean
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/validates_cpf_cnpj/cnpj.rb', line 3 def self.valid?(value) value.gsub!(/[^0-9]/, '') digit = value.slice(-2, 2) control = '' if value.size == 14 factor = 0 2.times do |i| sum = 0; 12.times do |j| sum += value.slice(j, 1).to_i * ((11 + i - j) % 8 + 2) end sum += factor * 2 if i == 1 factor = 11 - sum % 11 factor = 0 if factor > 9 control << factor.to_s end end control == digit end |