Class: CPF
- Inherits:
-
Object
show all
- Defined in:
- lib/cpf.rb,
lib/cpf/formatter.rb,
lib/cpf_cnpj/version.rb,
lib/cpf/verifier_digit.rb
Defined Under Namespace
Classes: Formatter, VerifierDigit
Constant Summary
collapse
- REGEX =
/\A\d{3}\.\d{3}\.\d{3}-\d{2}\Z/.freeze
- VALIDATION_SIZE_REGEX =
/^\d{11}$/.freeze
- NUMBER_SIZE =
9
- DENYLIST =
%w[
00000000000
11111111111
22222222222
33333333333
44444444444
55555555555
66666666666
77777777777
88888888888
99999999999
12345678909
].freeze
- VERSION =
CpfCnpj::VERSION
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(number, strict = false) ⇒ CPF
Returns a new instance of CPF.
46
47
48
49
|
# File 'lib/cpf.rb', line 46
def initialize(number, strict = false)
@number = number.to_s
@strict = strict
end
|
Instance Attribute Details
#number ⇒ Object
Returns the value of attribute number.
8
9
10
|
# File 'lib/cpf.rb', line 8
def number
@number
end
|
#strict ⇒ Object
Returns the value of attribute strict.
8
9
10
|
# File 'lib/cpf.rb', line 8
def strict
@strict
end
|
Class Method Details
28
29
30
|
# File 'lib/cpf.rb', line 28
def self.format(number)
new(number).formatted
end
|
.generate(formatted = false) ⇒ Object
36
37
38
39
40
41
42
43
44
|
# File 'lib/cpf.rb', line 36
def self.generate(formatted = false)
numbers = Array(0..9)
digits = Array.new(NUMBER_SIZE) { numbers.sample }
digits << VerifierDigit.generate(digits)
digits << VerifierDigit.generate(digits)
cpf = new(digits.join)
formatted ? cpf.formatted : cpf.stripped
end
|
.valid?(number, strict: false) ⇒ Boolean
32
33
34
|
# File 'lib/cpf.rb', line 32
def self.valid?(number, strict: false)
new(number, strict).valid?
end
|
Instance Method Details
#==(other) ⇒ Object
Also known as:
eql?
85
86
87
|
# File 'lib/cpf.rb', line 85
def ==(other)
super || (other.instance_of?(self.class) && other.stripped == stripped)
end
|
62
63
64
|
# File 'lib/cpf.rb', line 62
def formatted
@formatted ||= Formatter.format(number)
end
|
#number_without_verifier ⇒ Object
81
82
83
|
# File 'lib/cpf.rb', line 81
def number_without_verifier
numbers[0...NUMBER_SIZE].join
end
|
#stripped ⇒ Object
58
59
60
|
# File 'lib/cpf.rb', line 58
def stripped
@stripped ||= Formatter.strip(number, strict)
end
|