Class: UkAccountValidator::Validators::StandardModulus

Inherits:
BaseValidator
  • Object
show all
Defined in:
lib/uk_account_validator/validators/standard_modulus.rb

Overview

Perform validation for sort codes with MOD10

Direct Known Subclasses

Modulus10, Modulus11

Constant Summary

Constants included from NumberIndices

NumberIndices::NUMBER_INDEX

Instance Attribute Summary

Attributes inherited from BaseValidator

#account_number, #exception, #modulus_weight, #sort_code

Instance Method Summary collapse

Methods inherited from BaseValidator

#applying_exceptions, #initialize

Constructor Details

This class inherits a constructor from UkAccountValidator::Validators::BaseValidator

Instance Method Details

#modulusObject



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

def modulus
  fail NotImplementedError
end

#valid?Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/uk_account_validator/validators/standard_modulus.rb', line 9

def valid?
  test_string = sort_code + 

  test_digits = test_string.split(//).map(&:to_i)

  total = applying_exceptions(test_digits) do
    NUMBER_INDEX.each_pair.reduce(0) do |t, pair|
      weight, index = pair

      t + modulus_weight.send(weight) * test_digits[index]
    end
  end

  if exception.override_test?
    return exception.test(modulus, total, test_digits, :standard_modulus)
  end

  total % modulus == 0
end