Class: LicenseValidator::Validations::StateMb

Inherits:
Base
  • Object
show all
Defined in:
lib/license_validator/validations/state_mb.rb

Instance Attribute Summary

Attributes inherited from Base

#driver, #errors, #infos, #warnings

Instance Method Summary collapse

Methods inherited from Base

human_attribute_name, #initialize, lookup_ancestors, #read_attribute_for_validation, #validation_results

Constructor Details

This class inherits a constructor from LicenseValidator::Validations::Base

Instance Method Details

#validateObject

See Also:

  • super


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/license_validator/validations/state_mb.rb', line 7

def validate
  super

  if driver.first_name.blank?
    errors.add(:first_name, 'First name required for MB.')

    return
  end

  if driver.last_name.blank?
    errors.add(:last_name, 'Last name required for MB.')

    return
  end

  if driver.dob.blank?
    errors.add(:dob, 'D.O.B. required for MB.')

    return
  end

  expected_date_of_birth = (100 - driver.dob.strftime('%y').to_i).to_s.rjust(2, '0')[-2..]

  last_name = driver.last_name[0, 5].ljust(5, '*')
  last_name.gsub!('*', '\\*')

  # rubocop:disable Style/ArrayFirstLast
  first_initial = driver.first_name[0]
  # rubocop:enable Style/ArrayFirstLast

  regex = Regexp.new("\\A#{last_name}#{first_initial}[a-z*]#{expected_date_of_birth}[A-Z0-9]{3}\\z", 'i')

  return if driver.license_num.match?(regex)

  errors.add(
    :license_num,
    [
      'License number requires 1 alphabetic, 4 alphanumeric or asterisk, 1 alphabetic, 1 alphabetic or asterisk,',
      '2 alphanumeric, 1 numeric, 2 alphanumeric, and 1 numeric',
      'for MB'
    ].join(' ')
  )
end