Class: MercosurPlateConverter::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/mercosur_plate_converter/converter.rb,
sig/mercosur_plate_converter.rbs

Overview

Main converter class

Constant Summary collapse

FIFTH_TERM_MAP =

Constants

Returns:

  • (Array[String])
%w[A B C D E F G H I J].freeze
MERCOSUR_REGEX =

Returns:

  • (Regexp)
/\A(BR\s?)?[A-Z]{3}[0-9][A-Z][0-9]{2}\z/
LEGACY_REGEX =

Returns:

  • (Regexp)
/\A[A-Z]{3}[0-9]{4}\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plate) ⇒ Converter

Initialize with a plate string

Parameters:

  • plate (String, nil)


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_plateString (readonly)

Attributes

Returns:

  • (String)


22
23
24
# File 'sig/mercosur_plate_converter.rbs', line 22

def original_plate
  @original_plate
end

#plateString (readonly)

Returns the value of attribute plate.

Returns:

  • (String)


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.

Returns:

  • (:mercosur, :legacy, nil)


14
15
16
# File 'lib/mercosur_plate_converter/converter.rb', line 14

def type
  @type
end

Instance Method Details

#convertString

Convert the plate based on detected type

Returns:

  • (String)


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_legacyString

Convert from legacy format to Mercosur

Returns:

  • (String)


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_mercosurString

Convert from Mercosur format to legacy

Returns:

  • (String)


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_typevoid

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


33
34
35
# File 'sig/mercosur_plate_converter.rbs', line 33

def mercosur?
  @type == :mercosur
end

#old_brazilian?Boolean

DEPRECATED: Use legacy? instead

Returns:

  • (Boolean)


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

Parameters:

  • plate (String, nil)

Returns:

  • (String, nil)


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

Returns:

  • (Boolean)


30
31
32
# File 'sig/mercosur_plate_converter.rbs', line 30

def valid?
  !@type.nil?
end

#validate_plate_formatvoid

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_presencevoid

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