Class: BrDocuments::CnpjCpf::Cnpj
- Inherits:
-
Object
- Object
- BrDocuments::CnpjCpf::Cnpj
- Includes:
- Document
- Defined in:
- lib/br_documents/cnpj_cpf/cnpj.rb
Instance Method Summary collapse
- #formatted ⇒ Object
-
#initialize(number) ⇒ Cnpj
constructor
A new instance of Cnpj.
- #valid? ⇒ Boolean
Methods included from BrDocuments::Commons::Mod11
#generate_digital_check, #reduce_weights
Constructor Details
#initialize(number) ⇒ Cnpj
Returns a new instance of Cnpj.
8 9 10 |
# File 'lib/br_documents/cnpj_cpf/cnpj.rb', line 8 def initialize(number) @number = number end |
Instance Method Details
#formatted ⇒ Object
20 21 22 |
# File 'lib/br_documents/cnpj_cpf/cnpj.rb', line 20 def formatted @number.sub(/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/, "\\1.\\2.\\3/\\4-\\5") end |
#valid? ⇒ Boolean
12 13 14 15 16 17 18 |
# File 'lib/br_documents/cnpj_cpf/cnpj.rb', line 12 def valid? regex = /^(\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2})$|^(\d{14})$/ weight13 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2] weight14 = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2] valid_number?(@number, regex, weight13, weight14) end |