Class: PolishValidators::PeselValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/polish_validators/pesel_validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(pesel) ⇒ PeselValidator

Returns a new instance of PeselValidator.



3
4
5
# File 'lib/polish_validators/pesel_validator.rb', line 3

def initialize(pesel)
  @pesel = pesel.to_s
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
# File 'lib/polish_validators/pesel_validator.rb', line 7

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