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.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/m26_age.rb', line 26

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



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

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

#initialize_leap_yearsObject



49
50
51
52
53
54
# File 'lib/m26_age.rb', line 49

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)


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

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

#leap_yearsObject



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

def leap_years
  @@leap_years
end

#max_pulseObject



82
83
84
85
86
87
88
# File 'lib/m26_age.rb', line 82

def max_pulse
  if float_years <= 20
    200.0
  else
    220.0 - float_years
  end
end

#next_birthdayObject



60
61
62
63
64
65
66
67
# File 'lib/m26_age.rb', line 60

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



69
70
71
72
73
74
75
76
# File 'lib/m26_age.rb', line 69

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_sObject



22
23
24
# File 'lib/m26_age.rb', line 22

def to_s
  "Age - dob: #{dob} as_of: #{as_of_date } days: #{days} whole: #{whole_years} float: #{float_years} mp: #{max_pulse}"
end