Sha256: 142552294ed02e92001e84db036a71afc4809824863ce0be3199d524a6cc3ef7

Contents?: true

Size: 476 Bytes

Versions: 1

Compression:

Stored size: 476 Bytes

Contents

module PolishValidators
  class RegonValidator
    def initialize(regon)
      @regon = regon.to_s
    end

    def valid?
      return unless @regon.match(/\A\d{9,14}\Z/)

      weights8 = [8, 9, 2, 3, 4, 5, 6, 7]
      weights14 = [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8]

      regon = @regon.split(//).collect(&:to_i)
      checksum = (regon.length == 9 ? weights8 : weights14).reduce(0) { |a, e| a + regon.shift * e }

      checksum % 11 == regon.shift
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polish_validators-1.0.0 lib/polish_validators/regon_validator.rb