Sha256: 3ce546e8f72903b3c4365d4501760c4fd19499bb10b5af1816dedf9795f0bd07

Contents?: true

Size: 506 Bytes

Versions: 2

Compression:

Stored size: 506 Bytes

Contents

module CreditCardDetector
  class Rule
    def initialize(length:, prefixes:)
      @length = length
      @regexp = compile_regexp(prefixes)
    end

    def matches?(number)
      matches_length?(number.to_s) && matches_prefix?(number.to_s)
    end

    def matches_length?(number)
      @length.include? number.size
    end

    def matches_prefix?(number)
      !!(number =~ @regexp)
    end

    private

    def compile_regexp(prefixes)
      Regexp.new("^(#{prefixes.join("|")})")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
credit_card_detector-0.4 lib/credit_card_detector/rule.rb
credit_card_detector-0.3 lib/credit_card_detector/rule.rb