Module: BrazilianUtils::LicensePlateUtils
- Defined in:
- lib/brazilian-utils/license-plate-utils.rb
Overview
Utilities for formatting, validating, and generating Brazilian license plates.
Brazilian license plates come in two formats:
- Old format: LLLNNNN (3 letters + 4 numbers) e.g., "ABC-1234"
- Mercosul format: LLLNLNN (3 letters + 1 number + 1 letter + 2 numbers) e.g., "ABC1D23"
The Mercosul format was introduced in 2018 as part of a standardization effort across Mercosul countries.
Constant Summary collapse
- OLD_FORMAT_PATTERN =
Pattern for old format license plates (LLLNNNN)
/^[A-Za-z]{3}[0-9]{4}$/.freeze
- MERCOSUL_PATTERN =
Pattern for Mercosul format license plates (LLLNLNN)
/^[A-Z]{3}\d[A-Z]\d{2}$/.freeze
Class Method Summary collapse
-
.convert_to_mercosul(license_plate) ⇒ String?
Converts an old pattern license plate (LLLNNNN) to Mercosul format (LLLNLNN).
-
.format_license_plate(license_plate) ⇒ String?
(also: format)
Formats a license plate into the correct pattern.
-
.generate(format = 'LLLNLNN') ⇒ String?
Generates a valid license plate in the given format.
-
.get_format(license_plate) ⇒ String?
Returns the format of a license plate.
-
.is_valid(license_plate, type = nil) ⇒ Boolean
(also: valid?)
Returns if a Brazilian license plate number is valid.
-
.parse(value) ⇒ String
Removes separators from a license plate, upper-cases it and caps it to 7 characters.
-
.remove_symbols(license_plate_number) ⇒ String
Removes the dash (-) symbol from a license plate string.
Class Method Details
.convert_to_mercosul(license_plate) ⇒ String?
Converts an old pattern license plate (LLLNNNN) to Mercosul format (LLLNLNN).
The conversion replaces the first digit (position 4) with its corresponding letter (0→A, 1→B, 2→C, ..., 9→J).
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/brazilian-utils/license-plate-utils.rb', line 38 def self.convert_to_mercosul(license_plate) return '' unless license_plate.is_a?(String) clean = remove_symbols(license_plate).upcase return '' unless valid_old_format?(clean) chars = clean.chars # Convert the 5th character (index 4) - the first digit after the letters # 0→A, 1→B, 2→C, etc. chars[4] = ('A'.ord + chars[4].to_i).chr chars.join end |
.format_license_plate(license_plate) ⇒ String? Also known as: format
Formats a license plate into the correct pattern.
This function receives a license plate in any pattern (LLLNNNN or LLLNLNN) and returns a formatted version:
- Old format: adds dash (ABC-1234)
- Mercosul format: uppercase without dash (ABC1D34)
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/brazilian-utils/license-plate-utils.rb', line 76 def self.format_license_plate(license_plate) return nil unless license_plate.is_a?(String) clean = remove_symbols(license_plate).upcase if valid_old_format?(clean) # Old format: add dash after 3rd character "#{clean[0..2]}-#{clean[3..-1]}" elsif valid_mercosul?(clean) # Mercosul format: just uppercase, no dash clean else nil end end |
.generate(format = 'LLLNLNN') ⇒ String?
Generates a valid license plate in the given format.
In case no format is provided, it will return a license plate in the Mercosul format.
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/brazilian-utils/license-plate-utils.rb', line 227 def self.generate(format = 'LLLNLNN') return nil unless format.is_a?(String) format_upper = format.upcase return nil unless ['LLLNLNN', 'LLLNNNN'].include?(format_upper) generated = '' format_upper.each_char do |char| if char == 'L' # Generate random uppercase letter generated += ('A'..'Z').to_a.sample else # char == 'N' # Generate random digit generated += rand(0..9).to_s end end generated end |
.get_format(license_plate) ⇒ String?
Returns the format of a license plate.
Returns 'LLLNNNN' for the old pattern and 'LLLNLNN' for the Mercosul one.
191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/brazilian-utils/license-plate-utils.rb', line 191 def self.get_format(license_plate) return nil unless license_plate.is_a?(String) clean = remove_symbols(license_plate) if valid_old_format?(clean) 'LLLNNNN' elsif valid_mercosul?(clean) 'LLLNLNN' else nil end end |
.is_valid(license_plate, type = nil) ⇒ Boolean Also known as: valid?
Returns if a Brazilian license plate number is valid.
It does not verify if the plate actually exists, only validates the format.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/brazilian-utils/license-plate-utils.rb', line 128 def self.is_valid(license_plate, type = nil) return false unless license_plate.is_a?(String) clean = remove_symbols(license_plate) type_str = type.to_s if type case type_str when 'old_format' valid_old_format?(clean) when 'mercosul' valid_mercosul?(clean) else valid_old_format?(clean) || valid_mercosul?(clean) end end |
.parse(value) ⇒ String
Removes separators from a license plate, upper-cases it and caps it to 7 characters.
293 294 295 296 297 |
# File 'lib/brazilian-utils/license-plate-utils.rb', line 293 def self.parse(value) return '' unless value.is_a?(String) remove_symbols(value).upcase[0, 7] end |
.remove_symbols(license_plate_number) ⇒ String
Removes the dash (-) symbol from a license plate string.
165 166 167 168 169 |
# File 'lib/brazilian-utils/license-plate-utils.rb', line 165 def self.remove_symbols(license_plate_number) return '' unless license_plate_number.is_a?(String) license_plate_number.gsub('-', '') end |