Class: Personnummer
- Inherits:
-
Object
- Object
- Personnummer
- Defined in:
- lib/personnummer.rb
Instance Attribute Summary collapse
-
#born ⇒ Object
readonly
Public readonly attributes.
-
#control_digit ⇒ Object
readonly
Public readonly attributes.
-
#region ⇒ Object
readonly
Public readonly attributes.
Instance Method Summary collapse
- #age ⇒ Object
- #female? ⇒ Boolean
-
#initialize(number) ⇒ Personnummer
constructor
A new instance of Personnummer.
- #male? ⇒ Boolean
- #to_s ⇒ Object
- #valid? ⇒ Boolean
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
#born ⇒ Object (readonly)
Public readonly attributes
6 7 8 |
# File 'lib/personnummer.rb', line 6 def born @born end |
#control_digit ⇒ Object (readonly)
Public readonly attributes
6 7 8 |
# File 'lib/personnummer.rb', line 6 def control_digit @control_digit end |
#region ⇒ Object (readonly)
Public readonly attributes
6 7 8 |
# File 'lib/personnummer.rb', line 6 def region @region end |
Instance Method Details
#age ⇒ Object
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
88 89 90 |
# File 'lib/personnummer.rb', line 88 def female? @female end |
#male? ⇒ Boolean
84 85 86 |
# File 'lib/personnummer.rb', line 84 def male? !@female end |
#to_s ⇒ Object
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
80 81 82 |
# File 'lib/personnummer.rb', line 80 def valid? @valid end |