Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed next_occurrence to work on never-ending schedules

  • Loading branch information...
commit c5eee2a5c7d969cddb1499e75f11a60d6870716e 1 parent f8251a3
John Crepezzi authored
View
3  CHANGELOG
@@ -1,3 +1,4 @@
0.6.0
- * Changed how time serialization is done to preserve TimeWithZone when appropriate.
+ * [#8] Changed how time serialization is done to preserve TimeWithZone when appropriate.
Backward compatibility is intact, but bumping the minor version for the YAML format change.
+ * [#11] Fixed next occurrence to work on never-ending schedules
View
11 lib/ice_cube/rule_occurrence.rb
@@ -25,6 +25,17 @@ def upto(end_date)
find_occurrences { |roc| roc > end_date }
end
+ # Break after the first occurrence after now
+ def next_occurrence(from)
+ found_one = false
+ find_occurrences do |roc|
+ find = roc > from
+ success = found_one
+ found_one = find
+ success
+ end
+ end
+
def first(n)
count = 0
find_occurrences { |roc| count += 1; count > n }
View
3  lib/ice_cube/schedule.rb
@@ -132,7 +132,8 @@ def remaining_occurrences(from = Time.now)
# Find next scheduled occurrence
def next_occurrence(from = Time.now)
- remaining_occurrences(from).first
+ nexts = find_occurrences { |head| head.next_occurrence(from) }
+ nexts.empty? ? nil : (nexts.last if nexts.last > from)
end
# Retrieve the first (n) occurrences of the schedule. May return less than
View
6 spec/examples/recur_spec.rb
@@ -32,4 +32,10 @@
schedule.next_occurrence(schedule.end_time + 1.year).should == nil
end
+ it 'should be able to use next_occurrence on a never-ending schedule' do
+ schedule = Schedule.new(Time.now)
+ schedule.add_recurrence_rule Rule.hourly
+ schedule.next_occurrence(schedule.start_time).should == schedule.start_time + 1.hour
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.