Module: BrazilianUtils::RENAVAMUtils
- Defined in:
- lib/brazilian-utils/renavam-utils.rb
Overview
Utilities for validating Brazilian RENAVAM numbers.
RENAVAM (Registro Nacional de Veículos Automotores) is an 11-digit identification number for motor vehicles in Brazil.
The last digit is a verification digit calculated from the first 10 digits.
Constant Summary collapse
- DV_WEIGHTS =
Weights used for verification digit calculation Applied to the first 10 digits in reverse order
[2, 3, 4, 5, 6, 7, 8, 9, 2, 3].freeze
Class Method Summary collapse
-
.generate ⇒ String
Generates a valid random RENAVAM (11 digits: 10 base digits plus the check digit), unformatted.
-
.is_valid_renavam(renavam) ⇒ Boolean
(also: is_valid, valid?)
Validates the Brazilian vehicle registration number (RENAVAM).
Class Method Details
.generate ⇒ String
Generates a valid random RENAVAM (11 digits: 10 base digits plus the check digit), unformatted.
117 118 119 120 121 122 123 124 125 |
# File 'lib/brazilian-utils/renavam-utils.rb', line 117 def self.generate loop do base = 10.times.map { rand(0..9) }.join next if base.chars.uniq.length == 1 renavam = base + calculate_renavam_dv(base).to_s return renavam if is_valid_renavam(renavam) end end |
.is_valid_renavam(renavam) ⇒ Boolean Also known as: is_valid, valid?
Validates the Brazilian vehicle registration number (RENAVAM).
This function takes a RENAVAM string and checks if it is valid. A valid RENAVAM consists of exactly 11 digits, with the last digit as a verification digit calculated from the previous 10 digits.
41 42 43 44 45 |
# File 'lib/brazilian-utils/renavam-utils.rb', line 41 def self.is_valid_renavam(renavam) return false unless validate_renavam_format(renavam) calculate_renavam_dv(renavam) == renavam[-1].to_i end |