Class: M26::Age

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

Constant Summary collapse

@@leap_years =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_dateObject

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

#daysObject

Returns the value of attribute days.



12
13
14
# File 'lib/m26_age.rb', line 12

def days
  @days
end

#dobObject

Returns the value of attribute dob.



12
13
14
# File 'lib/m26_age.rb', line 12

def dob
  @dob
end

#float_yearsObject

Returns the value of attribute float_years.



12
13
14
# File 'lib/m26_age.rb', line 12

def float_years
  @float_years
end

#whole_yearsObject

Returns the value of attribute whole_years.



12
13
14
# File 'lib/m26_age.rb', line 12

def whole_years
  @whole_years
end

#yymmObject

Returns the value of attribute yymm.



12
13
14
# File 'lib/m26_age.rb', line 12

def yymm
  @yymm
end

Class Method Details

.leap_yearsObject



14
15
16
# File 'lib/m26_age.rb', line 14

def self.leap_years
  @@leap_years
end

Instance Method Details

#dob_mm_ddObject



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

def dob_mm_dd
  "#{dob.month}-#{dob.mday}"
end

#initialize_leap_yearsObject



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

Returns:

  • (Boolean)


57
58
59
# File 'lib/m26_age.rb', line 57

def leap_year?(y)
  @@leap_years.include?(y)
end

#leap_yearsObject

TODO - move these two methods down



19
20
21
# File 'lib/m26_age.rb', line 19

def leap_years
  @@leap_years
end

#max_pulseObject



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_birthdayObject



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_birthdayObject



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_fObject



83
84
85
# File 'lib/m26_age.rb', line 83

def to_f
  float_years # TODO - test
end

#to_sObject



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