Class: Valideez::Pesel

Inherits:
Base
  • Object
show all
Includes:
Common
Defined in:
lib/valideez/pesel.rb

Instance Attribute Summary

Attributes inherited from Base

#val, #validable

Instance Method Summary collapse

Methods included from Common

#validate_format, #validate_length

Methods inherited from Base

#valid?

Constructor Details

#initialize(val, options = {}) ⇒ Pesel

Returns a new instance of Pesel.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/valideez/pesel.rb', line 7

def initialize(val, options = {})
  super val

  assign({ 
    :length => 11,
    :format => /\A\d{11}\Z/, 
    :mask => [1, 3, 7, 9, 1, 3, 7, 9, 1, 3],
    :modulo => 10,
    :age => 0,
    :sum_control => true,
  }.merge(options))

  @arr = []
end

Instance Method Details

#validate_ageObject



30
31
32
33
34
35
36
37
# File 'lib/valideez/pesel.rb', line 30

def validate_age
  now = Time.now
  y = (now.strftime("%Y").to_i - age).to_s
  m = now.strftime("%m")
  d = now.strftime("%d")

  age == 0 || convert_to_date(val[0..5]) <= Date.parse([y, m, d].join("-"))
end

#validate_sum_controlObject



22
23
24
25
26
27
28
# File 'lib/valideez/pesel.rb', line 22

def validate_sum_control
  mod = checksum % modulo
  mod = modulo - mod
  mod = 0 if mod == modulo 

  mod === @arr.shift
end