Class: UkAccountValidator::Validators::DoubleAlternate

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

Overview

Perform validation for sort codes with MOD10

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/double_alternate.rb', line 5

def modulus
  10
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  test_string = @sort_code + @account_number

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

  total = applying_exceptions(test_digits) do
    # Apply weights
    weighted_test_digits = NUMBER_INDEX.map do |weight, index|
      @modulus_weight.send(weight) * test_digits[index]
    end

    # Split into individual digits by concating then splitting again.
    weighted_test_digits = weighted_test_digits.join.split(//).map(&:to_i)

    # Now sum
    weighted_test_digits.inject(:+)
  end

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

  total % modulus == 0
end