Commit 91e563ff5077814bbe816eff4ddd6ea6f0b20322
1 parent
46e7a697
Exists in
master
and in
17 other branches
Protect methods
Showing
1 changed file
with
65 additions
and
63 deletions
Show diff stats
lib/groupdate/magic.rb
@@ -15,39 +15,10 @@ module Groupdate | @@ -15,39 +15,10 @@ module Groupdate | ||
15 | end | 15 | end |
16 | end | 16 | end |
17 | 17 | ||
18 | - def time_zone | ||
19 | - @time_zone ||= begin | ||
20 | - time_zone = options[:time_zone] || Groupdate.time_zone || Time.zone || "Etc/UTC" | ||
21 | - time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone[time_zone] | ||
22 | - end | ||
23 | - end | ||
24 | - | ||
25 | - def week_start | ||
26 | - @week_start ||= [:mon, :tue, :wed, :thu, :fri, :sat, :sun].index((options[:week_start] || options[:start] || Groupdate.week_start).to_sym) | ||
27 | - end | ||
28 | - | ||
29 | - def day_start | ||
30 | - @day_start ||= (options[:day_start] || Groupdate.day_start).to_i | ||
31 | - end | ||
32 | - | ||
33 | def group_by(enum, &block) | 18 | def group_by(enum, &block) |
34 | series(enum.group_by{|v| round_time(block.call(v)) }, []) | 19 | series(enum.group_by{|v| round_time(block.call(v)) }, []) |
35 | end | 20 | end |
36 | 21 | ||
37 | - def time_range | ||
38 | - @time_range ||= begin | ||
39 | - time_range = options[:range] | ||
40 | - if !time_range and options[:last] | ||
41 | - step = 1.send(field) if 1.respond_to?(field) | ||
42 | - if step | ||
43 | - now = Time.now | ||
44 | - time_range = round_time(now - (options[:last].to_i - 1).send(field))..now | ||
45 | - end | ||
46 | - end | ||
47 | - time_range | ||
48 | - end | ||
49 | - end | ||
50 | - | ||
51 | def relation(column, relation) | 22 | def relation(column, relation) |
52 | column = relation.connection.quote_table_name(column) | 23 | column = relation.connection.quote_table_name(column) |
53 | time_zone = self.time_zone.tzinfo.name | 24 | time_zone = self.time_zone.tzinfo.name |
@@ -117,6 +88,71 @@ module Groupdate | @@ -117,6 +88,71 @@ module Groupdate | ||
117 | end | 88 | end |
118 | end | 89 | end |
119 | 90 | ||
91 | + def perform(relation, method, *args, &block) | ||
92 | + # undo reverse since we do not want this to appear in the query | ||
93 | + reverse = relation.reverse_order_value | ||
94 | + if reverse | ||
95 | + relation = relation.reverse_order | ||
96 | + end | ||
97 | + order = relation.order_values.first | ||
98 | + if order.is_a?(String) | ||
99 | + parts = order.split(" ") | ||
100 | + reverse_order = (parts.size == 2 && parts[0].to_sym == field && parts[1].to_s.downcase == "desc") | ||
101 | + reverse = !reverse if reverse_order | ||
102 | + end | ||
103 | + | ||
104 | + multiple_groups = relation.group_values.size > 1 | ||
105 | + | ||
106 | + cast_method = | ||
107 | + case field | ||
108 | + when :day_of_week, :hour_of_day | ||
109 | + lambda{|k| k.to_i } | ||
110 | + else | ||
111 | + utc = ActiveSupport::TimeZone["UTC"] | ||
112 | + lambda{|k| (k.is_a?(String) ? utc.parse(k) : k.to_time).in_time_zone(time_zone) } | ||
113 | + end | ||
114 | + | ||
115 | + count = | ||
116 | + begin | ||
117 | + Hash[ relation.send(method, *args, &block).map{|k, v| [multiple_groups ? k[0...@group_index] + [cast_method.call(k[@group_index])] + k[(@group_index + 1)..-1] : cast_method.call(k), v] } ] | ||
118 | + rescue NoMethodError | ||
119 | + raise "Be sure to install time zone support - https://github.com/ankane/groupdate#for-mysql" | ||
120 | + end | ||
121 | + | ||
122 | + series(count, 0, multiple_groups, reverse) | ||
123 | + end | ||
124 | + | ||
125 | + protected | ||
126 | + | ||
127 | + def time_zone | ||
128 | + @time_zone ||= begin | ||
129 | + time_zone = options[:time_zone] || Groupdate.time_zone || Time.zone || "Etc/UTC" | ||
130 | + time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone[time_zone] | ||
131 | + end | ||
132 | + end | ||
133 | + | ||
134 | + def week_start | ||
135 | + @week_start ||= [:mon, :tue, :wed, :thu, :fri, :sat, :sun].index((options[:week_start] || options[:start] || Groupdate.week_start).to_sym) | ||
136 | + end | ||
137 | + | ||
138 | + def day_start | ||
139 | + @day_start ||= (options[:day_start] || Groupdate.day_start).to_i | ||
140 | + end | ||
141 | + | ||
142 | + def time_range | ||
143 | + @time_range ||= begin | ||
144 | + time_range = options[:range] | ||
145 | + if !time_range and options[:last] | ||
146 | + step = 1.send(field) if 1.respond_to?(field) | ||
147 | + if step | ||
148 | + now = Time.now | ||
149 | + time_range = round_time(now - (options[:last].to_i - 1).send(field))..now | ||
150 | + end | ||
151 | + end | ||
152 | + time_range | ||
153 | + end | ||
154 | + end | ||
155 | + | ||
120 | def series(count, default_value, multiple_groups = false, reverse = false) | 156 | def series(count, default_value, multiple_groups = false, reverse = false) |
121 | series = | 157 | series = |
122 | case field | 158 | case field |
@@ -193,40 +229,6 @@ module Groupdate | @@ -193,40 +229,6 @@ module Groupdate | ||
193 | end] | 229 | end] |
194 | end | 230 | end |
195 | 231 | ||
196 | - def perform(relation, method, *args, &block) | ||
197 | - # undo reverse since we do not want this to appear in the query | ||
198 | - reverse = relation.reverse_order_value | ||
199 | - if reverse | ||
200 | - relation = relation.reverse_order | ||
201 | - end | ||
202 | - order = relation.order_values.first | ||
203 | - if order.is_a?(String) | ||
204 | - parts = order.split(" ") | ||
205 | - reverse_order = (parts.size == 2 && parts[0].to_sym == field && parts[1].to_s.downcase == "desc") | ||
206 | - reverse = !reverse if reverse_order | ||
207 | - end | ||
208 | - | ||
209 | - multiple_groups = relation.group_values.size > 1 | ||
210 | - | ||
211 | - cast_method = | ||
212 | - case field | ||
213 | - when :day_of_week, :hour_of_day | ||
214 | - lambda{|k| k.to_i } | ||
215 | - else | ||
216 | - utc = ActiveSupport::TimeZone["UTC"] | ||
217 | - lambda{|k| (k.is_a?(String) ? utc.parse(k) : k.to_time).in_time_zone(time_zone) } | ||
218 | - end | ||
219 | - | ||
220 | - count = | ||
221 | - begin | ||
222 | - Hash[ relation.send(method, *args, &block).map{|k, v| [multiple_groups ? k[0...@group_index] + [cast_method.call(k[@group_index])] + k[(@group_index + 1)..-1] : cast_method.call(k), v] } ] | ||
223 | - rescue NoMethodError | ||
224 | - raise "Be sure to install time zone support - https://github.com/ankane/groupdate#for-mysql" | ||
225 | - end | ||
226 | - | ||
227 | - series(count, 0, multiple_groups, reverse) | ||
228 | - end | ||
229 | - | ||
230 | def round_time(time) | 232 | def round_time(time) |
231 | time = time.to_time.in_time_zone(time_zone) - day_start.hours | 233 | time = time.to_time.in_time_zone(time_zone) - day_start.hours |
232 | 234 |