Skip to content

Commit

Permalink
Avoid StopIteration leak on explicit #next call in #next_occurrence
Browse files Browse the repository at this point in the history
  • Loading branch information
seejohnrun committed Feb 6, 2014
1 parent 48ec35e commit f20184b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/ice_cube/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ def next_occurrences(num, from = nil)
# The next occurrence after now (overridable)
def next_occurrence(from = nil)
from ||= TimeUtil.now(@start_time)
enumerate_occurrences(from + 1, nil).next()
begin
enumerate_occurrences(from + 1, nil).next()
rescue StopIteration
nil
end
end

# The previous occurrence from a given time
Expand Down Expand Up @@ -210,13 +214,12 @@ def occurrences_between(begin_time, closing_time)

# Return a boolean indicating if an occurrence falls between two times
def occurs_between?(begin_time, closing_time)
begin
begin
enumerate_occurrences(begin_time, closing_time).next()
true
rescue StopIteration
false
end

end

# Return a boolean indicating if an occurrence is occurring between two
Expand Down
9 changes: 9 additions & 0 deletions spec/examples/schedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
schedule.start_time.should == Time.local(dt.year, dt.month, dt.day, dt.hour, dt.min, dt.sec)
end

describe :next_occurrence do

it 'should not raise an exception when calling next occurrence with no remaining occurrences' do
schedule = IceCube::Schedule.new Time.now
lambda { schedule.next_occurrence }.should_not raise_error
end

end

describe :duration do

it 'should be based on end_time' do
Expand Down

0 comments on commit f20184b

Please sign in to comment.