Class: CpfCnpjPlus::Formatter::Cpf

Inherits:
Object
  • Object
show all
Defined in:
lib/cpf_cnpj_plus/formatter/cpf.rb

Overview

Responsável por formatar números de CPF para exibição. Aplica a máscara padrão XXX.XXX.XXX-XX a partir de uma string numérica.

Class Method Summary collapse

Class Method Details

.format(cpf, strict: false) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/cpf_cnpj_plus/formatter/cpf.rb', line 8

def self.format(cpf, strict: false)
  cleaned = cpf.to_s.gsub(/[^0-9]/, "")
  return nil unless cleaned.length == 11
  return nil if strict && !Validator::Cpf.valid?(cleaned)

  "#{cleaned[0..2]}.#{cleaned[3..5]}.#{cleaned[6..8]}-#{cleaned[9..10]}"
end