Sha256: e7cbbe41042c9f509e454a0bdcdf52e0a8b33e18c786c04aa99419feb4af378c

Contents?: true

Size: 908 Bytes

Versions: 6

Compression:

Stored size: 908 Bytes

Contents

require_relative 'base'
require_relative '../commons/mod11'

module BrDocuments
  module IE
    class Pattern1 < Base
      include Commons::Mod11

      def initialize(number)
        super
        @mask = /^(\d{8}\-\d{1})$|^(\d{9})$/
        @weight = [9, 8, 7, 6, 5, 4, 3, 2]
      end

      protected

      def valid_format?
        regex = @mask
        regex.match(@number).present?
      end

      def valid_check_digit?
        calculated_check_digit = generate_check_digit(only_numbers, @weight).to_s
        check_digit_valid = check_digit == calculated_check_digit
        remove_mask_of_number if check_digit_valid

        check_digit_valid
      end

      private

      def check_digit
        only_numbers[-1]
      end

      def remove_mask_of_number
        @number.replace only_numbers
      end

      def only_numbers
        @number.gsub(/[\.\/-]/, '')
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
br_documents-0.2.5 lib/br_documents/ie/pattern1.rb
br_documents-0.2.4 lib/br_documents/ie/pattern1.rb
br_documents-0.2.3 lib/br_documents/ie/pattern1.rb
br_documents-0.2.2 lib/br_documents/ie/pattern1.rb
br_documents-0.2.1 lib/br_documents/ie/pattern1.rb
br_documents-0.2.0 lib/br_documents/ie/pattern1.rb