Class: ValidatesIdentity::PeDni::Validator

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

Constant Summary collapse

VALIDATION_REGULAR_EXPRESSION =
/^(\d{8})-?([a-zA-Z0-9]{1})$/.freeze
VERIFIER_DIGITS =
%w[6 7 8 9 0 1 1 2 3 4 5].freeze
VERIFIER_LETTERS =
%w[K A B C D E F G H I J].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Validator

Returns a new instance of Validator.



12
13
14
# File 'lib/validates_identity/pe_dni/validator.rb', line 12

def initialize(value)
  @value = value.to_s
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



10
11
12
# File 'lib/validates_identity/pe_dni/validator.rb', line 10

def value
  @value
end

Instance Method Details

#formattedObject



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

def formatted
  return if result.nil?

  "#{number}-#{verifier_digit}"
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  return true if value.blank?
  return false unless number

  verifier_digit == VERIFIER_LETTERS[calculated_verifier_digit_position] ||
    verifier_digit == VERIFIER_DIGITS[calculated_verifier_digit_position]
end