Class: LicenseValidator::Validations::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Defined in:
lib/license_validator/validations/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver) ⇒ Base

Returns a new instance of Base.

Parameters:



30
31
32
33
34
35
36
# File 'lib/license_validator/validations/base.rb', line 30

def initialize(driver)
  @errors = ActiveModel::Errors.new(self)
  @warnings = ActiveModel::Errors.new(self)
  @infos = ActiveModel::Errors.new(self)

  @driver = driver
end

Instance Attribute Details

#driverLicenseValidator::Driver



9
10
11
# File 'lib/license_validator/validations/base.rb', line 9

def driver
  @driver
end

#errorsActiveModel::Errors

Returns:

  • (ActiveModel::Errors)


11
12
13
# File 'lib/license_validator/validations/base.rb', line 11

def errors
  @errors
end

#infosActiveModel::Errors

Returns:

  • (ActiveModel::Errors)


13
14
15
# File 'lib/license_validator/validations/base.rb', line 13

def infos
  @infos
end

#warningsActiveModel::Errors

Returns:

  • (ActiveModel::Errors)


15
16
17
# File 'lib/license_validator/validations/base.rb', line 15

def warnings
  @warnings
end

Class Method Details

.human_attribute_name(attr, _options = {}) ⇒ Object

Necessary for ActiveModel::Errors api.rubyonrails.org/classes/ActiveModel/Errors.html



24
25
26
# File 'lib/license_validator/validations/base.rb', line 24

def human_attribute_name(attr, _options = {})
  attr
end

.lookup_ancestorsObject

Necessary for ActiveModel::Errors api.rubyonrails.org/classes/ActiveModel/Errors.html



19
20
21
# File 'lib/license_validator/validations/base.rb', line 19

def lookup_ancestors
  [self]
end

Instance Method Details

#read_attribute_for_validation(attr) ⇒ Object

Necessary for ActiveModel::Errors api.rubyonrails.org/classes/ActiveModel/Errors.html



39
40
41
# File 'lib/license_validator/validations/base.rb', line 39

def read_attribute_for_validation(attr)
  __send__(attr)
end

#validateObject

Common validations



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/license_validator/validations/base.rb', line 44

def validate
  return if driver.dob.nil?

  age = driver.age

  if age < 21
    warnings.add(:dob, 'Drivers under 21 do not qualify for an interstate CDL.')
  elsif age < 23
    warnings.add(
      :dob,
      'Drivers under 23 are underage for all markets unless underwriters have specifically approved.'
    )
  elsif age > 65
    warnings.add(
      :dob,
      [
        'Drivers is older than 65, check underwriting guidelines to determine if they should be submitted',
        'for approval.'
      ].join(' ')
    )
  end

  return unless driver.bad

  warnings.add(:bad, 'Driver is on the bad drivers list.')
end

#validation_resultsHash

Returns:

  • (Hash)


72
73
74
75
76
77
78
79
# File 'lib/license_validator/validations/base.rb', line 72

def validation_results
  {
    errors: errors,
    warnings: warnings,
    infos: infos,
    validated_with: driver.attributes
  }
end