Class: Faker::CPF
- Inherits:
-
Object
- Object
- Faker::CPF
- Defined in:
- lib/faker/cpf.rb
Class Method Summary collapse
-
.numeric ⇒ Object
(also: number, numbers)
Generates a valid CPF number with numbers only.
-
.pretty ⇒ Object
(also: cpf, formatted)
Generates a valid CPF number with numbers and separators.
Class Method Details
.numeric ⇒ Object Also known as: number, numbers
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/faker/cpf.rb', line 9 def numeric cpf_root = Array.new(9) { rand(10) } # calculate first digit sum = (0..8).inject(0) do |sum, i| sum + cpf_root[i] * (10 - i) end first_validator = sum % 11 first_validator = first_validator < 2 ? 0 : 11 - first_validator cpf_root << first_validator # calculate second digit sum = (0..8).inject(0) do |sum, i| sum + cpf_root[i] * (11 - i) end sum += first_validator * 2 second_validator = sum % 11 second_validator = second_validator < 2 ? 0 : 11 - second_validator (cpf_root << second_validator).join end |
.pretty ⇒ Object Also known as: cpf, formatted
39 40 41 42 |
# File 'lib/faker/cpf.rb', line 39 def pretty cpf_numbers = numeric "%s.%s.%s-%s" % [cpf_numbers[0..2], cpf_numbers[3..5], cpf_numbers[6..8], cpf_numbers[9..10]] end |