Permalink
Browse files

Added some convenient aliases #add_recurrence_rule => #rrule, etc

  • Loading branch information...
1 parent 0c8cf02 commit 5757af745fe995cc1baee69795a17dc1135b9e14 John Crepezzi committed Aug 6, 2010
Showing with 73 additions and 11 deletions.
  1. +13 −6 lib/ice_cube/schedule.rb
  2. +49 −0 spec/examples/ice_cube_spec.rb
  3. +11 −5 spec/examples/sample_spec.rb
View
@@ -3,8 +3,10 @@ module IceCube
class Schedule
attr_reader :rdates, :exdates, :start_date, :duration, :end_time
+
+ alias :end_date :end_time
alias :start_time :start_date
-
+
def initialize(start_date, options = {})
@rrule_occurrence_heads = []
@exrule_occurrence_heads = []
@@ -104,9 +106,8 @@ def occurs_at?(date)
# Determine whether a given date appears in the times returned by the schedule
# Required activeSupport
- def occurs_on?(date)
+ def occurs_on?(date)
time = date.to_time
- return false if @end_time && time > @end_time
occurrences_between(time.beginning_of_day, time.end_of_day).any?
end
@@ -129,7 +130,7 @@ def first(n = nil)
dates = find_occurrences { |head| head.first(n || 1) }
n.nil? ? dates.first : dates.slice(0, n)
end
-
+
# Add a rule of any type as an recurrence in this schedule
def add_recurrence_rule(rule)
raise ArgumentError.new('Argument must be a valid rule') unless rule.class < Rule
@@ -164,17 +165,23 @@ def occurrences_between(begin_time, end_time)
# adjust to the propert end date
end_time = @end_time if @end_time && @end_time < end_time
# collect the occurrences
- exclude_dates, include_dates = Set.new(@exdates), SortedSet.new(@rdates)
+ include_dates = SortedSet.new(@rdates)
@rrule_occurrence_heads.each do |rrule_occurrence_head|
include_dates.merge(rrule_occurrence_head.between(begin_time, end_time))
end
@exrule_occurrence_heads.each do |exrule_occurrence_head|
exclude_dates.merge(exrule_occurrence_head.between(begin_time, end_time))
end
# reject all of the ones outside of the range
- include_dates.reject! { |date| exclude_dates.include?(date) || date < begin_time || date > end_time }
+ include_dates.reject! { |date| (@exdates && @exdates.include?(date)) || date < begin_time || date > end_time }
include_dates.to_a
end
+
+ alias :rdate :add_recurrence_date
+ alias rrule add_recurrence_rule
+ alias exdate add_exception_date
+ alias exrule add_exception_rule
+
private
@@ -635,5 +635,54 @@
schedule.occurs_on?(date + 1).should be(false)
schedule.occurs_on?(date - 1).should be(false)
end
+
+ it 'should work with occurs_on? with multiple rdates' do
+ schedule = Schedule.new(Time.local(2010, 7, 10, 16))
+ schedule.add_recurrence_date(Time.local(2010, 7, 11, 16))
+ schedule.add_recurrence_date(Time.local(2010, 7, 12, 16))
+ schedule.add_recurrence_date(Time.local(2010, 7, 13, 16))
+ # test
+ schedule.occurs_on?(Date.new(2010, 7, 11)).should be(true)
+ schedule.occurs_on?(Date.new(2010, 7, 12)).should be(true)
+ schedule.occurs_on?(Date.new(2010, 7, 13)).should be(true)
+ end
+
+ it 'should have some convenient aliases' do
+ start_time = Time.now
+ schedule = Schedule.new(start_time)
+
+ schedule.start_date.should == schedule.start_time
+ schedule.end_date.should == schedule.end_time
+ end
+
+ it 'should have some convenient alias for rrules' do
+ schedule = Schedule.new(Time.now)
+ daily = Rule.daily; monthly = Rule.monthly
+ schedule.add_recurrence_rule daily
+ schedule.rrule monthly
+ schedule.rrules.should == [daily, monthly]
+ end
+
+ it 'should have some convenient alias for exrules' do
+ schedule = Schedule.new(Time.now)
+ daily = Rule.daily; monthly = Rule.monthly
+ schedule.add_exception_rule daily
+ schedule.exrule monthly
+ schedule.exrules.should == [daily, monthly]
+ end
+
+ it 'should have some convenient alias for rdates' do
+ schedule = Schedule.new(Time.now)
+ schedule.add_recurrence_date Time.local(2010, 8, 13)
+ schedule.rdate Time.local(2010, 8, 14)
+ schedule.rdates.should == [Time.local(2010, 8, 13), Time.local(2010, 8, 14)]
+ end
+
+ it 'should have some convenient alias for exdates' do
+ schedule = Schedule.new(Time.now)
+ schedule.add_exception_date Time.local(2010, 8, 13)
+ schedule.exdate Time.local(2010, 8, 14)
+ schedule.exdates.should == [Time.local(2010, 8, 13), Time.local(2010, 8, 14)]
+ end
end
@@ -1,12 +1,18 @@
require File.dirname(__FILE__) + '/spec_helper'
+require 'active_support'
+
describe Schedule, 'occurs_on?' do
- it 'local - should make dates on interval over dst - github issue 4' do
- start_date = Time.local(2010, 3, 12, 19, 0, 0)
- schedule = Schedule.new(start_date)
- schedule.add_recurrence_rule Rule.daily(3)
- schedule.first(3).should == [Time.local(2010, 3, 12, 19, 0, 0), Time.local(2010, 3, 15, 19, 0, 0), Time.local(2010, 3, 18, 19, 0, 0)]
+ it 'should work with occurs_on? with multiple rdates' do
+ schedule = Schedule.new(Time.local(2010, 7, 10, 16))
+ schedule.add_recurrence_date(Time.local(2010, 7, 11, 16))
+ schedule.add_recurrence_date(Time.local(2010, 7, 12, 16))
+ schedule.add_recurrence_date(Time.local(2010, 7, 13, 16))
+ # test
+ schedule.occurs_on?(Date.new(2010, 7, 11)).should be(true)
+ schedule.occurs_on?(Date.new(2010, 7, 12)).should be(true)
+ schedule.occurs_on?(Date.new(2010, 7, 13)).should be(true)
end
end

0 comments on commit 5757af7

Please sign in to comment.