Permalink
Browse files

Begin implementation of by_week

  • Loading branch information...
1 parent 73b011a commit d517c1f0de3e16618b1db4778c5fc88f0eafbb58 @radar committed Feb 18, 2012
Showing with 64 additions and 0 deletions.
  1. +2 −0 lib/by_star.rb
  2. +15 −0 lib/by_star/by_week.rb
  3. +47 −0 spec/by_star/by_week_spec.rb
View
@@ -1,6 +1,7 @@
require 'by_star/by_year'
require 'by_star/by_month'
require 'by_star/by_fortnight'
+require 'by_star/by_week'
module ByStar
@@ -12,6 +13,7 @@ def by_star_field(field=nil)
include ByYear
include ByMonth
include ByFortnight
+ include ByWeek
class ParseError < StandardError
View
@@ -0,0 +1,15 @@
+module ByStar
+ module ByWeek
+
+ # For reasoning why I use *args rather than variables here,
+ # please see the by_year method comments in lib/by_star/by_year.rb
+
+ def by_week(time=Time.zone.now)
+ send("by_star_#{time_klass(time)}", time)
+ end
+
+ def by_week_Time(time)
+ between(time.beginning_of_week, time.end_of_week)
+ end
+ end
+end
@@ -0,0 +1,47 @@
+require 'spec_helper'
+describe "by week" do
+ def posts_count(*args)
+ find_posts(*args).count
+ end
+
+ def find_posts(*args)
+ Post.by_week(*args)
+ end
+
+ it "should be able to find posts in the current week" do
+ posts_count.should eql(8)
+ end
+
+ it "should be able to find posts in the 1st week" do
+ posts_count(0).should eql(1)
+ end
+
+ it "should be able to find posts in the 1st week of last year" do
+ posts_count(0, :year => Time.zone.now.year-1).should eql(1)
+ end
+
+ it "should not find any posts from a week ago" do
+ posts_count(1.week.ago).should eql(0)
+ end
+
+ it "should be able to find posts by a given date" do
+ posts_count(1.week.ago.to_date).should eql(0)
+ end
+
+ it "should raise an error when given an invalid argument" do
+ lambda { find(54) }.should raise_error(ByStar::ParseError, "by_week takes only a Time or Date object, a Fixnum (less than or equal to 53) or a Chronicable string.")
+ end
+
+ it "should be able to use an alternative field" do
+ Event.by_week(nil, :field => "start_time").posts_count.should eql(0)
+ end
+
+ it "should find posts at the start of the year" do
+ posts_count(0).should eql(1)
+ end
+
+ it "should find posts at the end of the year" do
+ posts_count(Time.zone.now.end_of_year).should eql(1)
+ end
+
+end

0 comments on commit d517c1f

Please sign in to comment.