class BirthdateFromPesel::Parser

Constants

MONTHS_MAP

Public Class Methods

new(pesel) click to toggle source
# File lib/birthdate_from_pesel.rb, line 18
def initialize(pesel)
  @pesel = pesel
end

Public Instance Methods

birthdate() click to toggle source
# 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) click to toggle source
# File lib/birthdate_from_pesel.rb, line 31
def full_year(month, year)
  "#{match_century(month)}#{year}".to_i
end
match_century(month) click to toggle source
# 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) click to toggle source
# 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