Module: BrazilianUtils::CPFUtils
- Defined in:
- lib/brazilian-utils/cpf-utils.rb
Class Method Summary collapse
-
.display(cpf) ⇒ String?
Formats a CPF for display with visual aid symbols (legacy method).
-
.format_cpf(cpf) ⇒ String?
Formats a CPF for display with visual aid symbols.
-
.generate ⇒ String
Generates a random valid CPF digit string.
-
.parse(value) ⇒ String
Removes CPF formatting and keeps only digits, capped to 11 digits.
-
.remove_symbols(dirty) ⇒ String
Removes specific symbols from a CPF string.
-
.sieve(dirty) ⇒ String
Removes specific symbols from a CPF (Brazilian Individual Taxpayer Number) string.
-
.valid?(cpf) ⇒ Boolean
Returns whether or not the verifying checksum digits of the given CPF match its base number.
-
.validate(cpf) ⇒ Boolean
Validates the checksum digits of a CPF.
Class Method Details
.display(cpf) ⇒ String?
This method should not be used in new code and is only provided for backward compatibility. Use format_cpf instead.
Formats a CPF for display with visual aid symbols (legacy method).
This function takes a numbers-only CPF string as input and adds standard formatting visual aid symbols for display.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/brazilian-utils/cpf-utils.rb', line 54 def self.display(cpf) return nil unless cpf.to_s.match?(/^\d{11}$/) return nil if cpf.chars.uniq.length == 1 format('%s.%s.%s-%s', cpf[0..2], cpf[3..5], cpf[6..8], cpf[9..10]) end |
.format_cpf(cpf) ⇒ String?
Formats a CPF for display with visual aid symbols.
This function takes a numbers-only CPF string as input and adds standard formatting visual aid symbols for display.
77 78 79 80 81 82 83 84 85 |
# File 'lib/brazilian-utils/cpf-utils.rb', line 77 def self.format_cpf(cpf) return nil unless valid?(cpf) format('%s.%s.%s-%s', cpf[0..2], cpf[3..5], cpf[6..8], cpf[9..10]) end |
.generate ⇒ String
Generates a random valid CPF digit string.
This function generates a random valid CPF string.
140 141 142 143 |
# File 'lib/brazilian-utils/cpf-utils.rb', line 140 def self.generate base = format('%09d', rand(1..999_999_998)) base + checksum(base) end |
.parse(value) ⇒ String
Removes CPF formatting and keeps only digits, capped to 11 digits.
199 200 201 202 203 |
# File 'lib/brazilian-utils/cpf-utils.rb', line 199 def self.parse(value) return '' unless value.is_a?(String) || value.is_a?(Integer) value.to_s.gsub(/\D/, '')[0, 11] end |
.remove_symbols(dirty) ⇒ String
Removes specific symbols from a CPF string.
This function is an alias for the sieve function, offering a more descriptive name.
35 36 37 |
# File 'lib/brazilian-utils/cpf-utils.rb', line 35 def self.remove_symbols(dirty) sieve(dirty) end |
.sieve(dirty) ⇒ String
This method should not be used in new code and is only provided for backward compatibility. Use remove_symbols instead.
Removes specific symbols from a CPF (Brazilian Individual Taxpayer Number) string.
This function takes a CPF string as input and removes all occurrences of the '.', '-' characters from it.
20 21 22 |
# File 'lib/brazilian-utils/cpf-utils.rb', line 20 def self.sieve(dirty) dirty.to_s.delete('.-') end |
.valid?(cpf) ⇒ Boolean
Returns whether or not the verifying checksum digits of the given CPF match its base number.
This function does not verify the existence of the CPF; it only validates the format of the string.
127 128 129 |
# File 'lib/brazilian-utils/cpf-utils.rb', line 127 def self.valid?(cpf) cpf.is_a?(String) && validate(cpf) end |
.validate(cpf) ⇒ Boolean
This method should not be used in new code and is only provided for backward compatibility. Use valid? instead.
Validates the checksum digits of a CPF.
This function checks whether the verifying checksum digits of the given CPF match its base number. The input should be a digit string of the proper length.
104 105 106 107 108 109 110 111 |
# File 'lib/brazilian-utils/cpf-utils.rb', line 104 def self.validate(cpf) return false unless cpf.to_s.match?(/^\d{11}$/) return false if cpf.chars.uniq.length == 1 (0..1).all? do |i| hashdigit(cpf, i + 10) == cpf[9 + i].to_i end end |