Permalink
Browse files

introduces daily grouping

  • Loading branch information...
nikola
nikola committed Apr 10, 2012
1 parent 25fd4c5 commit e86c0614d0ff327296a619856d2cd0c5914018e8
Showing with 34 additions and 7 deletions.
  1. +9 −1 README.md
  2. +7 −1 lib/activerecord-periodic/scopes.rb
  3. +8 −2 lib/activerecord-periodic/span.rb
  4. +7 −0 spec/scopes_spec.rb
  5. +3 −3 spec/span_spec.rb
View
@@ -25,14 +25,22 @@ Order.span('this month')
Usage
-------
-You can use these language structures to describe time scopes with `Periods`
+You can use these language structures to describe time scopes with
+`ActiveRecord::Periodic`
'the day 5 days ago'
'the week 5 weeks ago'
'the week 5 months ago'
'from 21/12/2012 until 23/12/2012'
'last week'
+You can also use grouping to get a Hash of daily counts like so:
+
+```ruby
+Order.span('last week group daily')
+# a Hash like: { '07-02-2012' => 234, '08-02-2012' => 245, ... }
+```
+
Complete (and short) list of features
-------
@@ -27,8 +27,14 @@ def self.included(klass)
column = pair.first.periods_has_time_span_scopes_column ||
raise(::ActiveRecord::Periodic::NoColumnGiven)
- memo.where(pair.first.arel_table[column].gteq(span.beginning.to_s(:db))).
+ memo = memo.where(pair.first.arel_table[column].gteq(span.beginning.to_s(:db))).
where(pair.first.arel_table[column].lt(span.end.to_s(:db)))
+
+ if span.grouping?
+ memo.group("date(#{pair.first.arel_table[column].name})")
+ else
+ memo
+ end
else
memo
end
@@ -19,7 +19,7 @@
module ActiveRecord::Periodic
class Span
include Comparable # by length
- attr_accessor :beginning, :end
+ attr_accessor :beginning, :end, :group_by
def initialize(*args)
args = args.flatten
@@ -28,14 +28,20 @@ def initialize(*args)
if args.size == 2 && args.all? { |a| a.kind_of?(Time) }
[ args.first, args.last ]
elsif (text = args.first).kind_of?(String)
- Parser[text].parse
+ group_by = text.split(/\sgroup\s/i)
+ @group_by = group_by[1]
+ Parser[group_by.first].parse
end
end
class << self
alias :[] :new
end
+ def grouping?
+ !! group_by
+ end
+
# if the span is finite or not
def finite?
! infinite?
View
@@ -68,5 +68,12 @@
Order.span('full').size.should == 4
Order.span('today').size.should == 1
end
+
+ it "should group correctly" do
+ hash = Order.span('from 5 days ago until today group daily').size
+ hash.should be_kind_of(Hash)
+ hash.size.should == 2
+ hash.values.sum.should == 3
+ end
end
View
@@ -71,21 +71,21 @@
end
it "#daily yields days" do
- Span['the month 2012 February'].daily do |day|
+ Span['the month February 2012'].daily do |day|
day.should be_kind_of(Span)
day.length.should == 1.day
end.should == 29
end
it "#weekly yields weeks" do
- Span['the month 2012 February'].weekly do |week|
+ Span['the month February 2012'].weekly do |week|
week.should be_kind_of(Span)
week.length.should == 1.week
end.should == 5
end
it "#weekly yields only complete weeks if strict is true" do
- Span['the month 2012 February'].weekly(true) do |week|
+ Span['the month February 2012'].weekly(true) do |week|
week.should be_kind_of(Span)
week.length.should == 1.week
end.should == 3

0 comments on commit e86c061

Please sign in to comment.