Class: MercosurPlateConverter::Converter
- Inherits:
-
Object
- Object
- MercosurPlateConverter::Converter
- Defined in:
- lib/mercosur_plate_converter/converter.rb,
sig/mercosur_plate_converter.rbs
Overview
Main converter class
Constant Summary collapse
- FIFTH_TERM_MAP =
Constants
%w[A B C D E F G H I J].freeze
- MERCOSUR_REGEX =
/\A(BR\s?)?[A-Z]{3}[0-9][A-Z][0-9]{2}\z/- LEGACY_REGEX =
/\A[A-Z]{3}[0-9]{4}\z/
Instance Attribute Summary collapse
-
#original_plate ⇒ String
readonly
Attributes.
-
#plate ⇒ String
readonly
Returns the value of attribute plate.
-
#type ⇒ :mercosur, ...
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#convert ⇒ String
Convert the plate based on detected type.
-
#convert_from_legacy ⇒ String
Convert from legacy format to Mercosur.
-
#convert_from_mercosur ⇒ String
Convert from Mercosur format to legacy.
-
#detect_type ⇒ void
Detect the type of the plate.
-
#initialize(plate) ⇒ Converter
constructor
Initialize with a plate string.
-
#legacy? ⇒ Boolean
Check if the plate is in legacy format.
-
#mercosur? ⇒ Boolean
Check if the plate is in Mercosur format.
-
#old_brazilian? ⇒ Boolean
DEPRECATED: Use legacy? instead.
-
#sanitize_plate(plate) ⇒ String?
Sanitize the input plate string.
-
#valid? ⇒ Boolean
Check if the plate is valid.
-
#validate_plate_format ⇒ void
Validate the plate format.
-
#validate_plate_presence ⇒ void
Validate presence of the plate.
Constructor Details
#initialize(plate) ⇒ Converter
Initialize with a plate string
27 28 29 30 31 32 33 34 35 |
# File 'sig/mercosur_plate_converter.rbs', line 27 def initialize(plate) @original_plate = sanitize_plate(plate) @plate = @original_plate.dup validate_plate_presence detect_type validate_plate_format convert end |
Instance Attribute Details
#original_plate ⇒ String (readonly)
Attributes
22 23 24 |
# File 'sig/mercosur_plate_converter.rbs', line 22 def original_plate @original_plate end |
#plate ⇒ String (readonly)
Returns the value of attribute plate.
14 15 16 |
# File 'lib/mercosur_plate_converter/converter.rb', line 14 def plate @plate end |
#type ⇒ :mercosur, ... (readonly)
Returns the value of attribute type.
14 15 16 |
# File 'lib/mercosur_plate_converter/converter.rb', line 14 def type @type end |
Instance Method Details
#convert ⇒ String
Convert the plate based on detected type
44 45 46 47 48 49 50 51 52 53 |
# File 'sig/mercosur_plate_converter.rbs', line 44 def convert case @type when :mercosur convert_from_mercosur when :legacy convert_from_legacy else @plate end end |
#convert_from_legacy ⇒ String
Convert from legacy format to Mercosur
47 48 49 50 51 52 |
# File 'sig/mercosur_plate_converter.rbs', line 47 def convert_from_legacy # Convert 5th position from number to letter digit = @plate[4].to_i @plate[4] = FIFTH_TERM_MAP[digit] if digit < FIFTH_TERM_MAP.size @plate end |
#convert_from_mercosur ⇒ String
Convert from Mercosur format to legacy
50 51 52 53 54 55 56 |
# File 'sig/mercosur_plate_converter.rbs', line 50 def convert_from_mercosur # Remove optional BR prefix and convert 5th position from letter to number @plate = @plate.delete_prefix("BR") letter_index = FIFTH_TERM_MAP_REVERSE[@plate[4]] @plate[4] = letter_index.to_s if letter_index @plate end |
#detect_type ⇒ void
This method returns an undefined value.
Detect the type of the plate
53 54 55 56 57 58 59 |
# File 'sig/mercosur_plate_converter.rbs', line 53 def detect_type @type = if @original_plate.match?(MERCOSUR_REGEX) :mercosur elsif @original_plate.match?(LEGACY_REGEX) :legacy end end |
#legacy? ⇒ Boolean
Check if the plate is in legacy format
36 37 38 |
# File 'sig/mercosur_plate_converter.rbs', line 36 def legacy? @type == :legacy end |
#mercosur? ⇒ Boolean
Check if the plate is in Mercosur format
33 34 35 |
# File 'sig/mercosur_plate_converter.rbs', line 33 def mercosur? @type == :mercosur end |
#old_brazilian? ⇒ Boolean
DEPRECATED: Use legacy? instead
39 40 41 42 |
# File 'sig/mercosur_plate_converter.rbs', line 39 def old_brazilian? warn "[DEPRECATION] `old_brazilian?` is deprecated. Use `legacy?` instead." legacy? end |
#sanitize_plate(plate) ⇒ String?
Sanitize the input plate string
56 57 58 59 60 |
# File 'sig/mercosur_plate_converter.rbs', line 56 def sanitize_plate(plate) return unless plate plate.to_s.upcase.delete("^A-Z0-9") end |
#valid? ⇒ Boolean
Check if the plate is valid
30 31 32 |
# File 'sig/mercosur_plate_converter.rbs', line 30 def valid? !@type.nil? end |
#validate_plate_format ⇒ void
This method returns an undefined value.
Validate the plate format
62 63 64 |
# File 'sig/mercosur_plate_converter.rbs', line 62 def validate_plate_format raise InvalidPlateError, "Invalid plate #{@plate}" unless @type end |
#validate_plate_presence ⇒ void
This method returns an undefined value.
Validate presence of the plate
59 60 61 |
# File 'sig/mercosur_plate_converter.rbs', line 59 def validate_plate_presence raise MissingPlateError, "Missing plate" if @plate.nil? || @plate.empty? end |