All Files (100.0%)
1 files in total.
12 relevant lines.
12 lines covered and
0 lines missed
File | % covered | Lines | Relevant Lines | Lines covered | Lines missed |
---|---|---|---|---|---|
./lib/sixarm_ruby_date_age.rb | 100.0 % | 93 | 12 | 12 | 0 |
File | % covered | Lines | Relevant Lines | Lines covered | Lines missed |
---|---|---|---|---|---|
./lib/sixarm_ruby_date_age.rb | 100.0 % | 93 | 12 | 12 | 0 |
./lib/sixarm_ruby_date_age.rb100.0 % covered |
||
# | Hits | |
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
2 |
||
3 |
=begin rdoc |
|
4 |
||
5 |
= SixArm.com » Ruby » Date #age_in_years and #age_in_days methods |
|
6 |
||
7 |
Author:: Joel Parker Henderson, joelparkerhenderson@gmail.com |
|
8 |
Copyright:: Copyright (c) 2006-2011 Joel Parker Henderson |
|
9 |
License:: See LICENSE.txt file |
|
10 |
||
11 |
Date extensions to calculate an age in days and years. |
|
12 |
||
13 |
@example |
|
14 |
||
15 |
date=Date.new(1980,10,31) |
|
16 |
date.age_in_days => 11124 |
|
17 |
date.age_in_years => 31 |
|
18 |
||
19 |
@example of custom dates |
|
20 |
||
21 |
date=Date.new(1980,10,31) |
|
22 |
new_years_eve=(2011,12,31) |
|
23 |
date.age_in_days_on(new_years_eve) => 11383 |
|
24 |
date.age_in_years_on(new_years_eve) => 31 |
|
25 |
||
26 |
||
27 |
=end |
|
28 |
||
29 |
||
30 |
class Date |
1 |
31 |
||
32 |
||
33 |
# @return [Integer] the age in days for a given date. |
|
34 |
# |
|
35 |
# @example |
|
36 |
# |
|
37 |
# date=Date.new(1980,10,31) |
|
38 |
# date.age_in_days => 11124 |
|
39 |
# |
|
40 |
# @example of custom dates |
|
41 |
# |
|
42 |
# date=Date.new(1980,10,31) |
|
43 |
# |
|
44 |
# valentines = Date.new(2011,02,14) |
|
45 |
# date.age_in_days(valentines) => 11063 |
|
46 |
# |
|
47 |
# halloween = Date.new(2011,10,31) |
|
48 |
# date.age_in_days(halloween) => 11322 |
|
49 |
# |
|
50 |
# new_years_eve = Date.new(2011,12,31) |
|
51 |
# date.age_in_days(new_years_eve) => 11383 |
|
52 |
||
53 |
def age_in_days(compare_date=Date.today) |
1 |
54 |
(compare_date.is_a? Date) or raise ArgumentError, "compare_date must be a date" |
6 |
55 |
(compare_date-self).to_i |
5 |
56 |
end |
|
57 |
||
58 |
alias :age_in_days_on :age_in_days |
1 |
59 |
||
60 |
||
61 |
# @return [Integer] the age in years for a given date. |
|
62 |
# |
|
63 |
# @example |
|
64 |
# |
|
65 |
# date=Date.new(1980,10,31) |
|
66 |
# date.age_in_years => 30 |
|
67 |
# |
|
68 |
# @example of custom dates |
|
69 |
# |
|
70 |
# date=Date.new(1980,10,31) |
|
71 |
# |
|
72 |
# valentines = Date.new(2011,02,14) |
|
73 |
# date.age_in_years_on(valentines) => 30 # before the birthday |
|
74 |
# |
|
75 |
# halloween = Date.new(2011,10,31) |
|
76 |
# date.age_in_years_on(halloween) => 31 # on the birthday is one year older |
|
77 |
# |
|
78 |
# new_years_eve = Date.new(2011,12,31) |
|
79 |
# date.age_years(new_years_eve) => 31 # after the birthday is one year older |
|
80 |
||
81 |
def age_in_years(compare_date=Date.today) |
1 |
82 |
(compare_date.is_a? Date) or raise ArgumentError, "compare_date must be a date" |
6 |
83 |
age=compare_date.year-year |
5 |
84 |
compare_month = compare_date.month |
5 |
85 |
age-=1 if compare_month < month or (compare_month==month and compare_date.day < day) |
5 |
86 |
age |
5 |
87 |
end |
|
88 |
||
89 |
alias :age_in_years_on :age_in_years |
1 |
90 |
||
91 |
end |
|
92 |
||
93 |