Sha256: e864d2bebc2021588cec03a4dde1060f520570b430e88447172d5d9ec0a6b31c

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

require "attr_extras/explicit"
require "banktools-gb/errors"

module BankTools
  module GB
    class AccountNumberWithSortCode
      extend AttrExtras.mixin

      ACCOUNT_NUMBER_MIN_LENGTH = 6
      ACCOUNT_NUMBER_MAX_LENGTH = 10
      SORT_CODE_LENGTH = 6

      pattr_initialize [ :account_number, :sort_code ]

      def valid?
        UkAccountValidator::Validator.new(compact_account_number, compact_sort_code).valid?
      end

      def errors
        errors = []
        errors << :account_number_too_short if account_number_too_short?
        errors << :account_number_too_long if account_number_too_long?
        errors << :sort_code_with_wrong_length if sort_code_with_wrong_length?
        errors << :sort_code_invalid_characters if any_non_digits?(compact_sort_code)
        errors << :account_number_invalid_characters if any_non_digits?(compact_account_number)
        errors
      end

      private

      def sort_code_with_wrong_length?
        compact_sort_code.length != SORT_CODE_LENGTH
      end

      def account_number_too_short?
        compact_account_number.length < ACCOUNT_NUMBER_MIN_LENGTH
      end

      def account_number_too_long?
        compact_account_number.length > ACCOUNT_NUMBER_MAX_LENGTH
      end

      def any_non_digits?(number)
        number.match(/\D/)
      end

      def compact_sort_code
        compact(sort_code)
      end

      def compact_account_number
        compact(account_number)
      end

      def compact(number)
        number.gsub(/[\s-]/, "")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
banktools-gb-0.3.0 lib/banktools-gb/account_number_with_sort_code.rb
banktools-gb-0.1.0 lib/banktools-gb/account_number_with_sort_code.rb