-
Notifications
You must be signed in to change notification settings - Fork 73
/
by_year.rb
62 lines (54 loc) · 1.63 KB
/
by_year.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module ByStar
module ByYear
# NOTE: We would define this as this:
#
# def by_year(time=Time.zone.now, options={})
#
# But, there's a potential situation where people want to just pass options.
# Like this:
#
# Post.by_year(:field => "published_at")
#
# And so, we support any number of arguments and just parse them as necessary.
# By doing it this way, we can support *both* this:
#
# Post.by_year(2012, :field => "published_at")
#
# And this:
#
# Post.by_year(:field => "published_at")
#
# This is because the time variable is going to be defaulting to the current time.
def by_year(*args)
options = args.extract_options!.symbolize_keys!
time = args.first || Time.zone.now
send("by_year_#{time_klass(time)}", time, options)
end
private
def by_year_Time(time, options={})
between(time.beginning_of_year, time.end_of_year, options)
end
def by_year_Date(date, options={})
by_year_Time(date.to_time, options)
end
def by_year_String_or_Fixnum(year, options={})
by_year_Time("#{work_out_year(year)}-01-01".to_time, options)
end
alias_method :by_year_String, :by_year_String_or_Fixnum
alias_method :by_year_Fixnum, :by_year_String_or_Fixnum
def work_out_year(value)
case value.to_i
when 0..39
2000 + value
when 40..99
1900 + value
when nil
Time.zone.now.year
else
# We may be passed something that's not a straight out integer
# These things include: BigDecimals, Floats and Strings.
value.to_i
end
end
end
end