Class: BirthdateFromPesel::Parser

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

Instance Method Summary collapse

Constructor Details

#initialize(pesel) ⇒ Parser

Returns a new instance of Parser.



18
19
20
# File 'lib/birthdate_from_pesel.rb', line 18

def initialize(pesel)
  @pesel = pesel
end

Instance Method Details

#birthdateObject



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

def birthdate
  match = @pesel.match(/\A([0-9]{2})([0-9]{2})([0-9]{2})/)

  if match
    year, month, day = match.captures
    Date.new(full_year(month, year), month(month), day.to_i)
  end
end

#full_year(month, year) ⇒ Object



31
32
33
# File 'lib/birthdate_from_pesel.rb', line 31

def full_year(month, year)
  "#{match_century(month)}#{year}".to_i
end

#match_century(month) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/birthdate_from_pesel.rb', line 35

def match_century(month)
  case month.chars.first
  when '8', '9'
    18
  when '2', '3'
    20
  when '4', '5'
    21
  when '6', '7'
    22
  else
    19
  end
end

#month(month) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/birthdate_from_pesel.rb', line 50

def month(month)
  month.chars.tap do |result|
    MONTHS_MAP.each do |k, v|
      result[0] = result[0].tr(k.to_s, v.to_s)
    end
  end.join.to_i
end