Browse files

Complete implementation of by_week

  • Loading branch information...
1 parent 2f822a6 commit 4a889bda7324d6376543468083a4937213357d68 @radar committed Feb 18, 2012
Showing with 22 additions and 11 deletions.
  1. +19 −4 lib/by_star/by_week.rb
  2. +3 −7 spec/by_star/by_week_spec.rb
View
23 lib/by_star/by_week.rb
@@ -4,12 +4,27 @@ 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)
+ def by_week(*args)
+ options = args.extract_options!.symbolize_keys!
+ time = args.first
+ time ||= Time.zone.local(options[:year], 1, 1) if options[:year]
+ time ||= Time.zone.now
+ send("by_week_#{time_klass(time)}", time, options)
end
- def by_week_Time(time)
- between(time.beginning_of_week, time.end_of_week)
+ private
+
+ def by_week_Time_or_Date(time, options={})
+ between(time.beginning_of_week, time.end_of_week, options)
+ end
+ alias_method :by_week_Time, :by_week_Time_or_Date
+ alias_method :by_week_Date, :by_week_Time_or_Date
+
+ def by_week_Fixnum(week, options={})
+ time = Time.zone.local(options[:year], 1, 1) if options[:year]
+ time ||= Time.zone.now
+ start_time = time.beginning_of_year + week.to_i.weeks
+ between(start_time, start_time + 1.week, options)
end
end
end
View
10 spec/by_star/by_week_spec.rb
@@ -13,7 +13,7 @@ def find_posts(*args)
end
it "should be able to find posts in the 1st week" do
- posts_count(0).should eql(1)
+ posts_count(0).should eql(8)
end
it "should be able to find posts in the 1st week of last year" do
@@ -28,16 +28,12 @@ def find_posts(*args)
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)
+ Event.by_week(:field => "start_time").size.should eql(2)
end
it "should find posts at the start of the year" do
- posts_count(0).should eql(1)
+ posts_count(0).should eql(8)
end
it "should find posts at the end of the year" do

0 comments on commit 4a889bd

Please sign in to comment.