Class: ValidatesIdentity::BrCpf::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/validates_identity/br_cpf/validator.rb

Constant Summary collapse

VALIDATION_REGULAR_EXPRESSION =
/^(\d{3}\.?\d{3}\.?\d{3})-?(\d{2})$/.freeze
FORMAT_REGULAR_EXPRESSION =
/(\d{3})(\d{3})(\d{3})(\d{2})/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Validator

Returns a new instance of Validator.



11
12
13
# File 'lib/validates_identity/br_cpf/validator.rb', line 11

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



9
10
11
# File 'lib/validates_identity/br_cpf/validator.rb', line 9

def value
  @value
end

Instance Method Details

#formattedObject



24
25
26
27
28
29
# File 'lib/validates_identity/br_cpf/validator.rb', line 24

def formatted
  return if number.nil?

  result = FORMAT_REGULAR_EXPRESSION.match(striped_value)
  "#{result[1]}.#{result[2]}.#{result[3]}-#{result[4]}"
end

#valid?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
# File 'lib/validates_identity/br_cpf/validator.rb', line 15

def valid?
  return true if value.blank?
  return false unless number
  return false if striped_value.length != 11
  return false if striped_value.scan(/\d/).uniq.length == 1

  verifier_digits == "#{first_digit_verifier}#{second_digit_verifier}"
end