Class: UkAccountValidator::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/uk_account_validator/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_number = nil, sort_code = nil) ⇒ Validator

Returns a new instance of Validator.



7
8
9
10
# File 'lib/uk_account_validator/validator.rb', line 7

def initialize( = nil, sort_code = nil)
  @account_number = 
  @sort_code      = sort_code
end

Instance Attribute Details

#account_numberObject

Returns the value of attribute account_number.



5
6
7
# File 'lib/uk_account_validator/validator.rb', line 5

def 
  @account_number
end

#sort_codeObject



12
13
14
# File 'lib/uk_account_validator/validator.rb', line 12

def sort_code
  @sort_code.gsub('-', '')
end

Instance Method Details

#exception_class(exception_strings) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/uk_account_validator/validator.rb', line 63

def exception_class(exception_strings)
  case
  when exception_strings.include?('1')
    Exception1
  when exception_strings.include?('2') && exception_strings.include?('9')
    Exception29
  when exception_strings.include?('3')
    Exception3
  when exception_strings.include?('4')
    Exception4
  when exception_strings.include?('5')
    Exception5
  when exception_strings.include?('6')
    Exception6
  when exception_strings.include?('7')
    Exception7
  when exception_strings.include?('8')
    Exception8
  when exception_strings.include?('10') && exception_strings.include?('11')
    Exception10
  when exception_strings.include?('12') && exception_strings.include?('13')
    Exception12
  when exception_strings.include?('14')
    Exception14
  else
    BaseException
  end
end

#modulus_validator(modulus) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/uk_account_validator/validator.rb', line 20

def modulus_validator(modulus)
  case modulus
  when 'MOD10'
    Validators::Modulus10
  when 'MOD11'
    Validators::Modulus11
  when 'DBLAL'
    Validators::DoubleAlternate
  else
    fail NotImplementedError
  end
end

#modulus_weightsObject



16
17
18
# File 'lib/uk_account_validator/validator.rb', line 16

def modulus_weights
  @modulus_weights ||= UkAccountValidator.modulus_weights_table.find(sort_code)
end

#valid?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/uk_account_validator/validator.rb', line 33

def valid?
  return false unless valid_format?

  exceptions = modulus_weights.map(&:exception)
  exception_class = self.exception_class(exceptions)

  results = modulus_weights.each_with_index.map do |modulus_weight, i|
    exception = exception_class.new(modulus_weight, , sort_code, i + 1)

    @account_number = exception.

    modulus_validator(modulus_weight.modulus).new(
      , sort_code, modulus_weight, exception
    ).valid?
  end

  return results.any? if exception_class.allow_any?

  results.all?
end

#valid_format?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
# File 'lib/uk_account_validator/validator.rb', line 54

def valid_format?
  return false if  =~ /\D/
  return false if .length < 6
  return false if .length > 10
  return false if sort_code.length != 6

  return true
end