Commit 5dc8b0d961fbb527dfab559523a6405651903267
Exists in
master
and in
10 other branches
Merge pull request #104 from codesnik/raise_argument_error
Raise argument error
Showing
4 changed files
with
18 additions
and
12 deletions
Show diff stats
lib/groupdate.rb
... | ... | @@ -4,8 +4,10 @@ require "groupdate/version" |
4 | 4 | require "groupdate/magic" |
5 | 5 | |
6 | 6 | module Groupdate |
7 | - FIELDS = [:second, :minute, :hour, :day, :week, :month, :year, :day_of_week, :hour_of_day, :day_of_month, :month_of_year] | |
8 | - METHODS = FIELDS.map { |v| :"group_by_#{v}" } | |
7 | + PERIODS = [:second, :minute, :hour, :day, :week, :month, :year, :day_of_week, :hour_of_day, :day_of_month, :month_of_year] | |
8 | + # backwards compatibility for anyone who happened to use it | |
9 | + FIELDS = PERIODS | |
10 | + METHODS = PERIODS.map { |v| :"group_by_#{v}" } | |
9 | 11 | |
10 | 12 | mattr_accessor :week_start, :day_start, :time_zone |
11 | 13 | self.week_start = :sun | ... | ... |
lib/groupdate/enumerable.rb
1 | 1 | module Enumerable |
2 | - Groupdate::FIELDS.each do |field| | |
3 | - define_method :"group_by_#{field}" do |options = {}, &block| | |
2 | + Groupdate::PERIODS.each do |period| | |
3 | + define_method :"group_by_#{period}" do |options = {}, &block| | |
4 | 4 | if block |
5 | - Groupdate::Magic.new(field, options).group_by(self, &block) | |
5 | + Groupdate::Magic.new(period, options).group_by(self, &block) | |
6 | 6 | else |
7 | 7 | raise ArgumentError, "no block given" |
8 | 8 | end |
... | ... | @@ -11,7 +11,7 @@ module Enumerable |
11 | 11 | |
12 | 12 | def group_by_period(period, options = {}, &block) |
13 | 13 | # to_sym is unsafe on user input, so convert to strings |
14 | - permitted_periods = ((options[:permit] || Groupdate::FIELDS).map(&:to_sym) & Groupdate::FIELDS).map(&:to_s) | |
14 | + permitted_periods = ((options[:permit] || Groupdate::PERIODS).map(&:to_sym) & Groupdate::PERIODS).map(&:to_s) | |
15 | 15 | if permitted_periods.include?(period.to_s) |
16 | 16 | send("group_by_#{period}", options, &block) |
17 | 17 | else | ... | ... |
lib/groupdate/scopes.rb
1 | 1 | module Groupdate |
2 | 2 | module Scopes |
3 | - Groupdate::FIELDS.each do |field| | |
4 | - define_method :"group_by_#{field}" do |*args| | |
3 | + Groupdate::PERIODS.each do |period| | |
4 | + define_method :"group_by_#{period}" do |field, *args| | |
5 | 5 | args = args.dup |
6 | 6 | options = args[-1].is_a?(Hash) ? args.pop : {} |
7 | - options[:time_zone] ||= args[1] unless args[1].nil? | |
8 | - options[:range] ||= args[2] unless args[2].nil? | |
7 | + options[:time_zone] ||= args[0] unless args[0].nil? | |
8 | + options[:range] ||= args[1] unless args[1].nil? | |
9 | 9 | |
10 | - Groupdate::Magic.new(field, options).relation(args[0], self) | |
10 | + Groupdate::Magic.new(period, options).relation(field, self) | |
11 | 11 | end |
12 | 12 | end |
13 | 13 | |
14 | 14 | def group_by_period(period, field, options = {}) |
15 | 15 | # to_sym is unsafe on user input, so convert to strings |
16 | - permitted_periods = ((options[:permit] || Groupdate::FIELDS).map(&:to_sym) & Groupdate::FIELDS).map(&:to_s) | |
16 | + permitted_periods = ((options[:permit] || Groupdate::PERIODS).map(&:to_sym) & Groupdate::PERIODS).map(&:to_s) | |
17 | 17 | if permitted_periods.include?(period.to_s) |
18 | 18 | send("group_by_#{period}", field, options) |
19 | 19 | else | ... | ... |
test/postgresql_test.rb