Class: Isikukood
- Inherits:
-
Object
- Object
- Isikukood
- Defined in:
- lib/isikukood.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
Instance Method Summary collapse
- #age ⇒ Object
- #birthday ⇒ Object
-
#initialize(code) ⇒ Isikukood
constructor
A new instance of Isikukood.
- #sex ⇒ Object
- #valid? ⇒ Boolean
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
#code ⇒ Object (readonly)
Returns the value of attribute code.
5 6 7 |
# File 'lib/isikukood.rb', line 5 def code @code end |
Instance Method Details
#age ⇒ Object
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 |
#birthday ⇒ Object
15 16 17 18 |
# File 'lib/isikukood.rb', line 15 def birthday return nil unless valid? birth_date.to_s end |
#sex ⇒ Object
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
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 |