Class: Isikukood

Inherits:
Object
  • Object
show all
Defined in:
lib/isikukood.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ Isikukood

Returns a new instance of Isikukood.



7
8
9
# File 'lib/isikukood.rb', line 7

def initialize(code)
  @code = code.to_s
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/isikukood.rb', line 5

def code
  @code
end

Instance Method Details

#ageObject



20
21
22
23
24
25
# File 'lib/isikukood.rb', line 20

def age
  return nil unless valid?
  now = Time.now.utc
  correction = now.month > birth_date.month || (now.month == birth_date.month && now.day >= birth_date.day) ? 0 : 1
  now.year - birth_date.year - correction
end

#birthdayObject



15
16
17
18
# File 'lib/isikukood.rb', line 15

def birthday
  return nil unless valid?
  birth_date.to_s
end

#sexObject



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

def sex
  return nil unless valid?
  code[0].to_i.odd? ? 'M' : 'F'
end

#valid?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/isikukood.rb', line 11

def valid?
  code.size == 11 && code[10].chr.to_i == control_code && birth_date.is_a?(Date)
end