Class: BrDocuments::CnpjCpf::Cpf

Inherits:
Object
  • Object
show all
Includes:
Document
Defined in:
lib/br_documents/cnpj_cpf/cpf.rb

Instance Method Summary collapse

Methods included from BrDocuments::Commons::Mod11

#generate_digital_check, #reduce_weights

Constructor Details

#initialize(number) ⇒ Cpf

Returns a new instance of Cpf.



8
9
10
# File 'lib/br_documents/cnpj_cpf/cpf.rb', line 8

def initialize(number)
  @number = number
end

Instance Method Details

#formattedObject



20
21
22
# File 'lib/br_documents/cnpj_cpf/cpf.rb', line 20

def formatted
  @number.sub(/(\d{3})(\d{3})(\d{3})(\d{2})/, "\\1.\\2.\\3-\\4")
end

#valid?Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'lib/br_documents/cnpj_cpf/cpf.rb', line 12

def valid?
  regex = /^(\d{3}\.\d{3}\.\d{3}-\d{2})$|^(\d{11})$/
  weight10 = [10, 9, 8, 7, 6, 5, 4, 3, 2]
  weight11 = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2]

  valid_number?(@number, regex, weight10, weight11)
end