Sha256: d535e65310148c49be724577d8300d9a09a6f38a306363e664dd620d92ec9dcd

Contents?: true

Size: 395 Bytes

Versions: 1

Compression:

Stored size: 395 Bytes

Contents

module PolishValidators
  class PeselValidator
    def initialize(pesel)
      @pesel = pesel.to_s
    end

    def valid?
      return unless @pesel =~ /\A\d{11}\Z/

      weights = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3]
      pesel = @pesel.split(//).collect(&:to_i)
      checksum = weights.reduce(0) { |a, e| a + pesel.shift * e }

      (10 - (checksum % 10)) % 10 == pesel.shift
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polish_validators-1.0.1 lib/polish_validators/pesel_validator.rb