Class: Personnummer
- Inherits:
-
Object
- Object
- Personnummer
- Defined in:
- lib/personnummer.rb
Defined Under Namespace
Classes: Born
Instance Attribute Summary collapse
-
#control_digit ⇒ Object
readonly
Public readonly attributes.
-
#region ⇒ Object
readonly
Public readonly attributes.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #age ⇒ Object
- #born ⇒ Object
- #co_ordination_number? ⇒ Boolean
- #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 |
# 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? @born = Born.new(century, year, month, day, @divider) @valid = @born.valid? && @valid # 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
#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
#==(other) ⇒ Object
58 59 60 61 62 |
# File 'lib/personnummer.rb', line 58 def ==(other) other = self.class.new(other) if other.is_a?(String) || other.is_a?(Integer) to_s == other.to_s end |
#age ⇒ Object
50 51 52 |
# File 'lib/personnummer.rb', line 50 def age @born.age end |
#born ⇒ Object
54 55 56 |
# File 'lib/personnummer.rb', line 54 def born @born.to_date end |
#co_ordination_number? ⇒ Boolean
87 88 89 |
# File 'lib/personnummer.rb', line 87 def co_ordination_number? @born.co_ordination_number? end |
#female? ⇒ Boolean
83 84 85 |
# File 'lib/personnummer.rb', line 83 def female? @female end |
#male? ⇒ Boolean
79 80 81 |
# File 'lib/personnummer.rb', line 79 def male? !@female end |
#to_s ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/personnummer.rb', line 64 def to_s born_string = born.strftime("%y%m%d") if co_ordination_number? day = born_string[4..6].to_i + 60 born_string = [born_string[0..3], "%02d" % day].join end "%s%s%03d%d" % [born_string, @divider, @serial, @control_digit] end |
#valid? ⇒ Boolean
75 76 77 |
# File 'lib/personnummer.rb', line 75 def valid? @valid end |