Permalink
Browse files

Finish implementation of by_day

  • Loading branch information...
1 parent 23d7010 commit 5d5059f8917871f22a68bc1d9cab83557d31e608 @radar committed Feb 19, 2012
Showing with 48 additions and 0 deletions.
  1. +4 −0 lib/by_star.rb
  2. +21 −0 lib/by_star/by_day.rb
  3. +23 −0 spec/by_star/by_day_spec.rb
View
4 lib/by_star.rb
@@ -4,6 +4,8 @@
require 'by_star/by_month'
require 'by_star/by_fortnight'
require 'by_star/by_week'
+require 'by_star/by_weekend'
+require 'by_star/by_day'
module ByStar
@@ -16,6 +18,8 @@ def by_star_field(field=nil)
include ByMonth
include ByFortnight
include ByWeek
+ include ByWeekend
+ include ByDay
class ParseError < StandardError
View
21 lib/by_star/by_day.rb
@@ -0,0 +1,21 @@
+module ByStar
+ module ByDay
+ def by_day(*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_day_#{time_klass(time)}", time, options)
+ end
+
+ private
+
+ def by_day_Time_or_Date(time, options)
+ between(time.beginning_of_day, time.end_of_day, options)
+ end
+ alias_method :by_day_Time, :by_day_Time_or_Date
+ alias_method :by_day_Date, :by_day_Time_or_Date
+
+
+ end
+end
View
23 spec/by_star/by_day_spec.rb
@@ -0,0 +1,23 @@
+require 'spec_helper'
+
+describe "by day" do
+ def posts_count(*args)
+ Post.by_day(*args).count
+ end
+
+ it "should be able to find a post for today" do
+ posts_count.should eql(5)
+ end
+
+ it "should be able to find a post by a given date" do
+ posts_count(Date.today).should eql(5)
+ end
+
+ it "should be able to find a post by a given date in last year" do
+ posts_count(:year => Time.zone.now.year - 1).should eql(1)
+ end
+
+ it "should be able to use an alternative field" do
+ Event.by_day(Time.now.yesterday, :field => "start_time").size.should eql(1)
+ end
+end

0 comments on commit 5d5059f

Please sign in to comment.