class Validator

Constants

RENAVAM_SIZE
VALIDATION_SEQUENCE

Attributes

calculated_digit[R]
renavam[R]
splitted[R]
validated_sequence[R]
verifying_digit[R]

Public Class Methods

new(renavam) click to toggle source
# File lib/renavam/validator.rb, line 9
def initialize(renavam)
  @renavam = renavam
end
validate(renavam) click to toggle source
# File lib/renavam/validator.rb, line 13
def self.validate(renavam)
  new(renavam).validate
end

Public Instance Methods

validate() click to toggle source
# File lib/renavam/validator.rb, line 17
def validate
  setup(renavam)

  return false unless format_valid?
  return true if calculated_digit == verifying_digit

  false
end

Private Instance Methods

calculate_digit() click to toggle source
# File lib/renavam/validator.rb, line 51
def calculate_digit
  verifying_number = (validated_sequence * 10) % 11

  verifying_number == 10 ? 0 : verifying_number
end
format_valid?() click to toggle source
# File lib/renavam/validator.rb, line 35
def format_valid?
  renavam.to_s !~ /\D/ &&
    splitted.size == RENAVAM_SIZE &&
    !validated_sequence.zero?
end
setup(renavam) click to toggle source
# File lib/renavam/validator.rb, line 28
def setup(renavam)
  @splitted = renavam.to_s.chars
  @verifying_digit = splitted.last.to_i
  @validated_sequence = validate_sequence
  @calculated_digit = calculate_digit
end
validate_sequence() click to toggle source
# File lib/renavam/validator.rb, line 41
def validate_sequence
  sum = 0
  index = 0
  while index < VALIDATION_SEQUENCE.length
    sum += splitted[index].to_i * VALIDATION_SEQUENCE[index].to_i
    index += 1
  end
  sum
end