Class: Kennitala
- Inherits:
-
Object
- Object
- Kennitala
- Defined in:
- lib/kennitala.rb
Overview
The main Kennitala class
Constant Summary collapse
- VERSION =
The current version of the gem
'0.1.2'
Instance Method Summary collapse
-
#age ⇒ Fixnum
Get the age of entity in years.
-
#company? ⇒ Boolean
Check if the entity is a company.
-
#day ⇒ Fixnum
Get the day of the month of birth or registration.
-
#entity_type ⇒ String
Get the type of entity - If it is a person or an organization.
-
#initialize(kt_string = false, is_company = false) ⇒ Kennitala
constructor
Initialize a Kennitala object.
-
#month ⇒ Fixnum
Get a numeric representation of the month of birth or registration.
-
#person? ⇒ Boolean
Check if the entity is a person.
-
#pp(spacer = ' ') ⇒ String
Pretty print a kennitala.
-
#to_date ⇒ Date
Cast the kennitala to a Date object.
-
#to_s ⇒ String
Cast the kennitala to a String object.
-
#year ⇒ Fixnum
Get the year of birth or registration.
Constructor Details
#initialize(kt_string = false, is_company = false) ⇒ Kennitala
The string provided may include spaces, hyphens and alphabetical characters, which will then be erased from the resulting string.
Initialize a Kennitala object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/kennitala.rb', line 32 def initialize(kt_string = false, is_company = false) kt_string = fake_kt_string(is_company) if kt_string == false unless kt_string.instance_of?(String) raise ArgumentError, 'Kennitala needs to be provided as a String or ' \ 'Boolean (false)' end sanitised_kt = sanitize(kt_string) raise ArgumentError, 'Kennitala is invalid' if sanitised_kt.nil? @value = sanitised_kt end |
Instance Method Details
#age ⇒ Fixnum
Get the age of entity in years. Useful when dealing with age restrictions.
124 125 126 127 128 129 130 131 132 |
# File 'lib/kennitala.rb', line 124 def age year_diff = Date.today.year - to_date.year month_diff = Date.today.month - to_date.month day_diff = Date.today.month - to_date.month return year_diff -= 1 if month_diff.negative? || (month_diff.zero? && day_diff.negative?) year_diff end |
#company? ⇒ Boolean
Check if the entity is a company
62 63 64 65 66 67 |
# File 'lib/kennitala.rb', line 62 def company? date_integer = @value[0, 2].to_i return true if (date_integer > 40) && (date_integer < 72) false end |
#day ⇒ Fixnum
Get the day of the month of birth or registration
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/kennitala.rb', line 92 def day day_from_value = @value[0, 2].to_s[0, 2].to_i day_from_value -= 40 if day_from_value > 40 # There are examples of a date overlapping the number of days in the # given month. Those are considered to be from the last day of that month # instad of the specified out-of-bounds day of the month. # This is common for the year 1969 as new registrations were being done en # masse. last_day_of_month = Date.new(year, month, -1).day return last_day_of_month if day_from_value > last_day_of_month day_from_value end |
#entity_type ⇒ String
Get the type of entity - If it is a person or an organization
53 54 55 56 57 |
# File 'lib/kennitala.rb', line 53 def entity_type date_integer = @value[0, 2].to_i return 'person' if date_integer < 32 return 'company' if (date_integer > 40) && (date_integer < 72) end |
#month ⇒ Fixnum
Get a numeric representation of the month of birth or registration
111 112 113 |
# File 'lib/kennitala.rb', line 111 def month @value[2, 2].to_i end |
#person? ⇒ Boolean
Check if the entity is a person
72 73 74 75 76 77 |
# File 'lib/kennitala.rb', line 72 def person? date_integer = @value[0, 2].to_i return true if date_integer < 32 false end |
#pp(spacer = ' ') ⇒ String
Pretty print a kennitala
Adds a space between the 6th and the 7th digits for readability.
172 173 174 175 176 |
# File 'lib/kennitala.rb', line 172 def pp(spacer = ' ') raise ArgumentError 'Spacer must be a string' unless spacer.instance_of?(String) @value[0, 6] + spacer + @value[6, 9] end |
#to_date ⇒ Date
Cast the kennitala to a Date object
143 144 145 |
# File 'lib/kennitala.rb', line 143 def to_date Date.new(year, month, day) end |
#to_s ⇒ String
Cast the kennitala to a String object
150 151 152 |
# File 'lib/kennitala.rb', line 150 def to_s @value.to_s end |
#year ⇒ Fixnum
Get the year of birth or registration
82 83 84 85 86 87 |
# File 'lib/kennitala.rb', line 82 def year century = (10 + @value[9].to_i) * 100 year = @value[4, 2].to_i return century + year if (1800..1900).cover?(century) return 2000 + year if century == 1000 end |