Class: M26::Age
- Inherits:
-
Object
- Object
- M26::Age
- Defined in:
- lib/m26_age.rb
Constant Summary collapse
- @@leap_years =
[]
Instance Attribute Summary collapse
-
#as_of_date ⇒ Object
Returns the value of attribute as_of_date.
-
#days ⇒ Object
Returns the value of attribute days.
-
#dob ⇒ Object
Returns the value of attribute dob.
-
#float_years ⇒ Object
Returns the value of attribute float_years.
-
#whole_years ⇒ Object
Returns the value of attribute whole_years.
-
#yymm ⇒ Object
Returns the value of attribute yymm.
Class Method Summary collapse
Instance Method Summary collapse
- #dob_mm_dd ⇒ Object
-
#initialize(birth_yyyy_mm_dd, as_of_yyyy_mm_dd) ⇒ Age
constructor
A new instance of Age.
- #initialize_leap_years ⇒ Object
- #leap_year?(y) ⇒ Boolean
-
#leap_years ⇒ Object
TODO - move these two methods down.
- #max_pulse ⇒ Object
- #next_birthday ⇒ Object
- #prev_birthday ⇒ Object
- #to_f ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(birth_yyyy_mm_dd, as_of_yyyy_mm_dd) ⇒ Age
Returns a new instance of Age.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/m26_age.rb', line 27 def initialize(birth_yyyy_mm_dd, as_of_yyyy_mm_dd) initialize_leap_years if @@leap_years.size == 0 @dob = Date.parse(birth_yyyy_mm_dd) @as_of_date = Date.parse(as_of_yyyy_mm_dd) @days = as_of_date - dob @yymm = (dob.mon * 100) + (dob.day) if relation_to_birthday < 0 @whole_years = as_of_date.year - dob.year - 1 else @whole_years = as_of_date.year - dob.year end pbd, nbd = prev_birthday, next_birthday ndays = (leap_year?(nbd.year) ? 366 : 365) days_diff = as_of_date - pbd if relation_to_birthday == 0 @float_years = @whole_years.to_f else @float_years = @whole_years.to_f + (days_diff.to_f / ndays.to_f) end end |
Instance Attribute Details
#as_of_date ⇒ Object
Returns the value of attribute as_of_date.
12 13 14 |
# File 'lib/m26_age.rb', line 12 def as_of_date @as_of_date end |
#days ⇒ Object
Returns the value of attribute days.
12 13 14 |
# File 'lib/m26_age.rb', line 12 def days @days end |
#dob ⇒ Object
Returns the value of attribute dob.
12 13 14 |
# File 'lib/m26_age.rb', line 12 def dob @dob end |
#float_years ⇒ Object
Returns the value of attribute float_years.
12 13 14 |
# File 'lib/m26_age.rb', line 12 def float_years @float_years end |
#whole_years ⇒ Object
Returns the value of attribute whole_years.
12 13 14 |
# File 'lib/m26_age.rb', line 12 def whole_years @whole_years end |
#yymm ⇒ Object
Returns the value of attribute yymm.
12 13 14 |
# File 'lib/m26_age.rb', line 12 def yymm @yymm end |
Class Method Details
.leap_years ⇒ Object
14 15 16 |
# File 'lib/m26_age.rb', line 14 def self.leap_years @@leap_years end |
Instance Method Details
#dob_mm_dd ⇒ Object
79 80 81 |
# File 'lib/m26_age.rb', line 79 def dob_mm_dd "#{dob.month}-#{dob.mday}" end |
#initialize_leap_years ⇒ Object
50 51 52 53 54 55 |
# File 'lib/m26_age.rb', line 50 def initialize_leap_years (1900..2100).to_a.each { | y | d = Date.parse("#{y}-01-01") @@leap_years << y if d.leap? } end |
#leap_year?(y) ⇒ Boolean
57 58 59 |
# File 'lib/m26_age.rb', line 57 def leap_year?(y) @@leap_years.include?(y) end |
#leap_years ⇒ Object
TODO - move these two methods down
19 20 21 |
# File 'lib/m26_age.rb', line 19 def leap_years @@leap_years end |
#max_pulse ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/m26_age.rb', line 87 def max_pulse if float_years <= 20 200.0 else 220.0 - float_years end end |
#next_birthday ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/m26_age.rb', line 61 def next_birthday bb = relation_to_birthday if bb >= 0 Date.parse("#{as_of_date.year + 1}-#{dob_mm_dd}") else Date.parse("#{as_of_date.year}-#{dob_mm_dd}") end end |
#prev_birthday ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/m26_age.rb', line 70 def prev_birthday bb = relation_to_birthday if bb > 0 Date.parse("#{as_of_date.year}-#{dob_mm_dd}") else Date.parse("#{as_of_date.year - 1}-#{dob_mm_dd}") end end |
#to_f ⇒ Object
83 84 85 |
# File 'lib/m26_age.rb', line 83 def to_f float_years # TODO - test end |
#to_s ⇒ Object
23 24 25 |
# File 'lib/m26_age.rb', line 23 def to_s "Age - dob: #{dob} as_of: #{as_of_date } days: #{days} whole: #{whole_years} float: #{float_years} mp: #{max_pulse}" end |