c7-personnummer 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog +4 -0
- data/demo.rb +1 -1
- data/lib/personnummer.rb +7 -3
- metadata +1 -1
data/Changelog
CHANGED
data/demo.rb
CHANGED
data/lib/personnummer.rb
CHANGED
@@ -5,13 +5,14 @@ class Personnummer
|
|
5
5
|
attr_reader :age, :born, :region, :control_digit
|
6
6
|
|
7
7
|
def initialize(number)
|
8
|
+
|
8
9
|
@valid = false
|
9
10
|
|
10
11
|
# Store the initial number
|
11
|
-
@number = number
|
12
|
+
@number = number.to_s
|
12
13
|
|
13
14
|
# Match the number
|
14
|
-
if number.match(/(\d{2})(\d{2})(\d{2})([\-\+])(\d{3})(\d{0,1})/)
|
15
|
+
if @number.match(/(\d{2})(\d{2})(\d{2})([\-\+]{0,1})(\d{3})(\d{0,1})/)
|
15
16
|
|
16
17
|
# Calculate the control digit based on the birth date and serial number
|
17
18
|
@control_digit = luhn_algorithm("#{$~[1]}#{$~[2]}#{$~[3]}#{$~[5]}")
|
@@ -23,6 +24,9 @@ class Personnummer
|
|
23
24
|
divider = $~[4]
|
24
25
|
serial = $~[5].to_i
|
25
26
|
|
27
|
+
# Set default divider if not present
|
28
|
+
divider ||= '-'
|
29
|
+
|
26
30
|
# Make the personnummer valid if the checksum is correct
|
27
31
|
@valid = true if @control_digit == $~[6].to_i && !$~[6].empty?
|
28
32
|
|
@@ -103,7 +107,7 @@ private
|
|
103
107
|
|
104
108
|
def region_name(code)
|
105
109
|
|
106
|
-
# Don't return a region name if the person was born
|
110
|
+
# Don't return a region name if the person was born after 1990
|
107
111
|
# (When the previous region code was changed to a serial number)
|
108
112
|
if @born.year > 1990
|
109
113
|
return ''
|