Class: BR::CPF

Inherits:
Object
  • Object
show all
Extended by:
CPFWrapper
Defined in:
lib/br/cpf.rb,
lib/br/cpf/version.rb

Constant Summary collapse

CPF_MIN =
1
CPF_MAX =
999999999
VERSION =
"1.0.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raiz) ⇒ CPF

Returns a new instance of CPF.



13
14
15
# File 'lib/br/cpf.rb', line 13

def initialize(raiz)
  self.raiz = raiz
end

Instance Attribute Details

#cpfObject (readonly)

Returns the value of attribute cpf.



11
12
13
# File 'lib/br/cpf.rb', line 11

def cpf
  @cpf
end

#verifObject (readonly)

Returns the value of attribute verif.



11
12
13
# File 'lib/br/cpf.rb', line 11

def verif
  @verif
end

Class Method Details

.valid?(cpf) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/br/cpf.rb', line 33

def self.valid?(cpf)
  if cpf.is_a? String
    is_valid(cpf.to_i)
  else
    is_valid(cpf)
  end
end

Instance Method Details

#raizObject



29
30
31
# File 'lib/br/cpf.rb', line 29

def raiz
  @raiz
end

#raiz=(raiz) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
# File 'lib/br/cpf.rb', line 21

def raiz=(raiz)
  @raiz = raiz.to_i
  raise ArgumentError, "raiz should be between #{CPF_MIN} and #{CPF_MAX}" if @raiz < CPF_MIN or @raiz > CPF_MAX
  @cpf = self.class.calc_check_digit(@raiz)
  @verif = @cpf % 100
  @valid = true
end

#to_iObject



41
42
43
# File 'lib/br/cpf.rb', line 41

def to_i
  @cpf
end

#to_sObject



45
46
47
# File 'lib/br/cpf.rb', line 45

def to_s
  "%011d" % @cpf
end

#valid?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/br/cpf.rb', line 17

def valid?
  @valid
end