Class: BirthNumber

Inherits:
Object
  • Object
show all
Defined in:
lib/birth_number.rb,
lib/birth_number/version.rb

Overview

Class representing a Norwegian “Birth Number” (“Fødselsnummer”).

A birth number is a national identificaion number used in Norway, is unique for each person, and encodes birth date as well as gender. The last two digits are used as a checksum to verify a valid birth number.

This implementation is based of the description on the Norwegian Wikipedia page.

Constant Summary collapse

VERSION =
'1.0.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(birth_date, personal_number) ⇒ BirthNumber

Returns a new instance of BirthNumber.

Parameters:

  • birth_date (#to_date, #to_s)
  • personal_number (#to_i)


58
59
60
61
62
63
64
65
66
# File 'lib/birth_number.rb', line 58

def initialize(birth_date, personal_number)
  if birth_date.respond_to? :to_date
    self.birth_date = birth_date.to_date
  else
    self.birth_date = Date.parse(birth_date.to_s)
  end

  self.personal_number = format('%05d', personal_number.to_i)
end

Instance Attribute Details

#birth_dateDate

Birth Date

Returns:

  • (Date)


23
24
25
# File 'lib/birth_number.rb', line 23

def birth_date
  @birth_date
end

#personal_numberString

Personal Number

Returns:

  • (String)


27
28
29
# File 'lib/birth_number.rb', line 27

def personal_number
  @personal_number
end

Class Method Details

.parse(birth_number) ⇒ BirthNumber

Parse a 11-digit Birth Number

Parameters:

  • birth_number (#to_s)

Returns:

Raises:

  • (ArgumentError)

    if passed string is not 11 digits or invalid date



34
35
36
37
38
39
40
41
42
43
# File 'lib/birth_number.rb', line 34

def self.parse(birth_number)
  birth_number = birth_number.to_s
  unless birth_number =~ /^\d{11}$/
    fail ArgumentError, 'Birth number must be 11 digits'
  end

  birth_date = parse_birth_date(birth_number)

  new(birth_date, birth_number[6, 5])
end

.valid?(birth_number) ⇒ Boolean

Check if a given birth number is valid

Parameters:

  • birth_number (#to_s)

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
# File 'lib/birth_number.rb', line 47

def self.valid?(birth_number)
  digits = birth_number.to_s.chars.map(&:to_i)

  digits.last(2) == control_digits(digits)

rescue ArgumentError
  return false
end

Instance Method Details

#==(other) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/birth_number.rb', line 89

def ==(other)
  unless other.respond_to?(:birth_date) && other.respond_to?(:personal_number)
    return false
  end

  birth_date == other.birth_date && personal_number == other.personal_number
end

#===(other) ⇒ Object



97
98
99
# File 'lib/birth_number.rb', line 97

def ===(other)
  to_s == other.to_s
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/birth_number.rb', line 101

def eql?(other)
  self == other
end

#female?Boolean

Is this birth number assigned to a woman

Returns:

  • (Boolean)


79
80
81
# File 'lib/birth_number.rb', line 79

def female?
  personal_number[2].to_i.even?
end

#hashObject



105
106
107
# File 'lib/birth_number.rb', line 105

def hash
  to_s.hash
end

#male?Boolean

Is this birth number assigned to a man

Returns:

  • (Boolean)


74
75
76
# File 'lib/birth_number.rb', line 74

def male?
  personal_number[2].to_i.odd?
end

#to_sString

Formats this birthnumber in the proper 11-digit form

Returns:

  • (String)


85
86
87
# File 'lib/birth_number.rb', line 85

def to_s
  birth_date.strftime('%d%m%y') + format('%05d', personal_number.to_i)
end

#valid?Boolean

Check if this birth number is valid

Returns:

  • (Boolean)


69
70
71
# File 'lib/birth_number.rb', line 69

def valid?
  BirthNumber.valid?(self)
end