Class: Personnummer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ Personnummer

Returns a new instance of Personnummer.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/personnummer.rb', line 8

def initialize(number)
  @valid = false

  # Match the number
  number = number.to_s
  if number.match(/(\d{2}){0,1}(\d{2})(\d{2})(\d{2})([\-\+]{0,1})(\d{3})(\d{0,1})/)

    # Calculate the control digit based on the birth date and serial number
    cd = luhn_algorithm("#{$~[2]}#{$~[3]}#{$~[4]}#{$~[6]}")

    # Set control digit to calculated if it's missing
    @control_digit = $~[7].nil? || $~[7].empty? ? cd : $~[7].to_i

    # Get the different parts of the number
    century  = $~[1].to_i
    year     = $~[2].to_i
    month    = $~[3].to_i
    day      = $~[4].to_i
    @divider  = $~[5]
    @serial   = $~[6].to_i

    # Set default divider if not present
    if @divider.empty?
      @divider = '-'
    end

    # Make the personnummer valid if the checksum is correct
    @valid = true if @control_digit == cd && !$~[7].empty?

    # Get the current date
    today = Date.today

    if century == 0
      # Decide which century corresponds to the number
      if year < (today.year-2000) && @divider == '-'
        century = 2000
      elsif year < (today.year-2000) && @divider == '+'
        century = 1900
      elsif @divider == '+'
        century = 1800
      else
        century = 1900
      end
    else
      century *= 100
    end

    # Get the date the person was born
    @born   = Date.parse("#{century+year}-#{month}-#{day}")

    # Get the region name
    @region = region_name(@serial)

    # Check if the person is female based the serial (even == female)
    @female = (@serial % 2 == 0)
  else
    raise ArgumentError.new, "The supplied personnummer is invalid"
  end
end

Instance Attribute Details

#bornObject (readonly)

Public readonly attributes



6
7
8
# File 'lib/personnummer.rb', line 6

def born
  @born
end

#control_digitObject (readonly)

Public readonly attributes



6
7
8
# File 'lib/personnummer.rb', line 6

def control_digit
  @control_digit
end

#regionObject (readonly)

Public readonly attributes



6
7
8
# File 'lib/personnummer.rb', line 6

def region
  @region
end

Instance Method Details

#ageObject



68
69
70
71
72
73
74
# File 'lib/personnummer.rb', line 68

def age
  if Date.today > @born
    (Date.today - @born).to_i/365
  else
    0
  end
end

#female?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/personnummer.rb', line 88

def female?
  @female
end

#male?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/personnummer.rb', line 84

def male?
  !@female
end

#to_sObject



76
77
78
# File 'lib/personnummer.rb', line 76

def to_s
  "%s%s%03d%d" % [@born.strftime("%y%m%d"), @divider, @serial, @control_digit]
end

#valid?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/personnummer.rb', line 80

def valid?
  @valid
end