Skip to content

Commit 335793e

Browse files
authored
Merge pull request #172 from coderdojo-japan/extend-args-format
Support '%Y' format
2 parents dfb67bd + ff52830 commit 335793e

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

lib/tasks/statistics.rake

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,38 @@ require_relative '../statistics.rb'
22

33
namespace :statistics do
44
desc '月次のイベント履歴を集計します'
5-
task :aggregation, [:from_yyyymm, :to_yyyymm] => :environment do |tasks, args|
5+
task :aggregation, [:from, :to] => :environment do |tasks, args|
66
date_from_str = -> (str) {
7-
d = %w(%Y%m %Y/%m %Y-%m).map { |fmt|
7+
formats = %w(%Y%m %Y/%m %Y-%m)
8+
d = formats.map { |fmt|
89
begin
910
Time.zone.strptime(str, fmt)
1011
rescue ArgumentError
12+
Time.zone.local(str) if str.length == 4
1113
end
1214
}.compact.first
13-
raise ArgumentError, "Invalid format: `#{str}`" if d.nil?
15+
raise ArgumentError, "Invalid format: `#{str}`, allow format is #{formats.push('%Y').join(' or ')}" if d.nil?
1416
d
1517
}
1618

17-
from = (args[:from_yyyymm] ? date_from_str.call(args[:from_yyyymm]) : Time.current.prev_month).beginning_of_month
18-
to = (args[:to_yyyymm] ? date_from_str.call(args[:to_yyyymm]) : Time.current.prev_month).end_of_month
19+
from = if args[:from]
20+
if args[:from].length == 4
21+
date_from_str.call(args[:from]).beginning_of_year
22+
else
23+
date_from_str.call(args[:from]).beginning_of_month
24+
end
25+
else
26+
Time.current.prev_month.beginning_of_month
27+
end
28+
to = if args[:to]
29+
if args[:to].length == 4
30+
date_from_str.call(args[:to]).end_of_year
31+
else
32+
date_from_str.call(args[:to]).end_of_month
33+
end
34+
else
35+
Time.current.prev_month.end_of_month
36+
end
1937

2038
EventHistory.where(evented_at: from..to).delete_all
2139

0 commit comments

Comments
 (0)