Skip to content

Commit 9742d42

Browse files
committed
Changing spans param to options hash
Swapping the new spans parameter for an options hash to make the method calls more specific in their meaning.
1 parent e14ad76 commit 9742d42

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,17 @@ schedule.last(2) # [now + 1.day, now + 2.days]
7777
schedule.last # now + 2.days
7878

7979
# or the next occurrence
80-
schedule.next_occurrence(from_time) # defaults to Time.now
81-
schedule.next_occurrences(3, from_time) # defaults to Time.now
82-
schedule.next_occurrences(3, from_time, true) # include prior occurrences with duration overlapping from_time
83-
schedule.remaining_occurrences # for terminating schedules
80+
schedule.next_occurrence(from_time) # defaults to Time.now
81+
schedule.next_occurrences(3, from_time) # defaults to Time.now
82+
schedule.remaining_occurrences # for terminating schedules
8483

8584
# or the previous occurrence
8685
schedule.previous_occurrence(from_time)
8786
schedule.previous_occurrences(3, from_time)
8887

88+
# or include prior occurrences with a duration overlapping from_time
89+
schedule.next_occurrences(3, from_time, :spans => true)
90+
schedule.occurrences_between(from_time, to_time, :spans => true)
8991

9092
# or give the schedule a duration and ask if occurring_at?
9193
schedule = IceCube::Schedule.new(now, :duration => 3600)

lib/ice_cube/schedule.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,15 @@ def each_occurrence(&block)
166166
end
167167

168168
# The next n occurrences after now
169-
def next_occurrences(num, from = nil, spans = false)
169+
def next_occurrences(num, from = nil, options = {})
170170
from = TimeUtil.match_zone(from, start_time) || TimeUtil.now(start_time)
171-
enumerate_occurrences(from + 1, nil, spans).take(num)
171+
enumerate_occurrences(from + 1, nil, options).take(num)
172172
end
173173

174174
# The next occurrence after now (overridable)
175-
def next_occurrence(from = nil, spans = false)
175+
def next_occurrence(from = nil, options = {})
176176
from = TimeUtil.match_zone(from, start_time) || TimeUtil.now(start_time)
177-
enumerate_occurrences(from + 1, nil, spans).next
177+
enumerate_occurrences(from + 1, nil, options).next
178178
rescue StopIteration
179179
nil
180180
end
@@ -195,26 +195,26 @@ def previous_occurrences(num, from)
195195
end
196196

197197
# The remaining occurrences (same requirements as all_occurrences)
198-
def remaining_occurrences(from = nil, spans = false)
198+
def remaining_occurrences(from = nil, options = {})
199199
require_terminating_rules
200200
from ||= TimeUtil.now(@start_time)
201-
enumerate_occurrences(from, nil, spans).to_a
201+
enumerate_occurrences(from, nil, options).to_a
202202
end
203203

204204
# Returns an enumerator for all remaining occurrences
205-
def remaining_occurrences_enumerator(from = nil, spans = false)
205+
def remaining_occurrences_enumerator(from = nil, options = {})
206206
from ||= TimeUtil.now(@start_time)
207-
enumerate_occurrences(from, nil, spans)
207+
enumerate_occurrences(from, nil, options)
208208
end
209209

210210
# Occurrences between two times
211-
def occurrences_between(begin_time, closing_time, spans = false)
212-
enumerate_occurrences(begin_time, closing_time, spans).to_a
211+
def occurrences_between(begin_time, closing_time, options = {})
212+
enumerate_occurrences(begin_time, closing_time, options).to_a
213213
end
214214

215215
# Return a boolean indicating if an occurrence falls between two times
216-
def occurs_between?(begin_time, closing_time, spans = false)
217-
enumerate_occurrences(begin_time, closing_time, spans).next
216+
def occurs_between?(begin_time, closing_time, options = {})
217+
enumerate_occurrences(begin_time, closing_time, options).next
218218
true
219219
rescue StopIteration
220220
false
@@ -226,7 +226,7 @@ def occurs_between?(begin_time, closing_time, spans = false)
226226
# occurrences at the end of the range since none of their duration
227227
# intersects the range.
228228
def occurring_between?(opening_time, closing_time)
229-
occurs_between?(opening_time, closing_time, true)
229+
occurs_between?(opening_time, closing_time, :spans => true)
230230
end
231231

232232
# Return a boolean indicating if an occurrence falls on a certain date
@@ -402,12 +402,12 @@ def reset
402402
# Find all of the occurrences for the schedule between opening_time
403403
# and closing_time
404404
# Iteration is unrolled in pairs to skip duplicate times in end of DST
405-
def enumerate_occurrences(opening_time, closing_time = nil, spans = false, &block)
405+
def enumerate_occurrences(opening_time, closing_time = nil, options = {}, &block)
406406
opening_time = TimeUtil.match_zone(opening_time, start_time)
407407
closing_time = TimeUtil.match_zone(closing_time, start_time)
408408
opening_time += start_time.subsec - opening_time.subsec rescue 0
409409
opening_time = start_time if opening_time < start_time
410-
spans = false if duration == 0
410+
spans = options[:spans] == true && duration != 0
411411
Enumerator.new do |yielder|
412412
reset
413413
t1 = full_required? ? start_time : realign((spans ? opening_time - duration : opening_time))

spec/examples/schedule_spec.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -458,52 +458,52 @@
458458
t0 = Time.utc(2015, 10, 1, 15, 31)
459459
schedule = IceCube::Schedule.new(t0, :duration => 2 * IceCube::ONE_HOUR)
460460
schedule.add_recurrence_rule IceCube::Rule.daily
461-
next_occ = schedule.next_occurrence(t0 + IceCube::ONE_HOUR, true)
461+
next_occ = schedule.next_occurrence(t0 + IceCube::ONE_HOUR, :spans => true)
462462
next_occ.should == t0
463463
end
464464

465465
it 'should include occurrence in past with duration beyond the start time' do
466466
t0 = Time.utc(2015, 10, 1, 15, 31)
467467
schedule = IceCube::Schedule.new(t0, :duration => 2 * IceCube::ONE_HOUR)
468468
schedule.add_recurrence_rule IceCube::Rule.daily.count(2)
469-
occs = schedule.next_occurrences(10, t0 + IceCube::ONE_HOUR, true)
469+
occs = schedule.next_occurrences(10, t0 + IceCube::ONE_HOUR, :spans => true)
470470
occs.should == [t0, t0 + IceCube::ONE_DAY]
471471
end
472472

473473
it 'should allow duration span on remaining_occurrences' do
474474
t0 = Time.utc(2015, 10, 1, 00, 00)
475475
schedule = IceCube::Schedule.new(t0, :duration => IceCube::ONE_DAY)
476476
schedule.add_recurrence_rule IceCube::Rule.daily.count(3)
477-
occs = schedule.remaining_occurrences(t0 + IceCube::ONE_DAY + IceCube::ONE_HOUR, true)
477+
occs = schedule.remaining_occurrences(t0 + IceCube::ONE_DAY + IceCube::ONE_HOUR, :spans => true)
478478
occs.should == [t0 + IceCube::ONE_DAY, t0 + 2 * IceCube::ONE_DAY]
479479
end
480480

481481
it 'should include occurrences with duration spanning the requested start time' do
482482
t0 = Time.utc(2015, 10, 1, 15, 31)
483483
schedule = IceCube::Schedule.new(t0, :duration => 30 * IceCube::ONE_DAY)
484-
long_event = schedule.remaining_occurrences_enumerator(t0 + IceCube::ONE_DAY, true).take(1)
484+
long_event = schedule.remaining_occurrences_enumerator(t0 + IceCube::ONE_DAY, :spans => true).take(1)
485485
long_event.should == [t0]
486486
end
487487

488488
it 'should find occurrences between including previous one with duration spanning start' do
489489
t0 = Time.utc(2015, 10, 1, 10, 00)
490490
schedule = IceCube::Schedule.new(t0, :duration => IceCube::ONE_HOUR)
491491
schedule.add_recurrence_rule IceCube::Rule.hourly.count(10)
492-
occs = schedule.occurrences_between(t0 + IceCube::ONE_HOUR + 1, t0 + 3 * IceCube::ONE_HOUR + 1, true)
492+
occs = schedule.occurrences_between(t0 + IceCube::ONE_HOUR + 1, t0 + 3 * IceCube::ONE_HOUR + 1, :spans => true)
493493
occs.length.should == 3
494494
end
495495

496496
it 'should include long occurrences starting before and ending after' do
497497
t0 = Time.utc(2015, 10, 1, 00, 00)
498498
schedule = IceCube::Schedule.new(t0, :duration => IceCube::ONE_DAY)
499-
occs = schedule.occurrences_between(t0 + IceCube::ONE_HOUR, t0 + IceCube::ONE_DAY - IceCube::ONE_HOUR, true)
499+
occs = schedule.occurrences_between(t0 + IceCube::ONE_HOUR, t0 + IceCube::ONE_DAY - IceCube::ONE_HOUR, :spans => true)
500500
occs.should == [t0]
501501
end
502502

503503
it 'should not find occurrence with duration ending on start time' do
504504
t0 = Time.utc(2015, 10, 1, 12, 00)
505505
schedule = IceCube::Schedule.new(t0, :duration => IceCube::ONE_HOUR)
506-
schedule.occurs_between?(t0 + IceCube::ONE_HOUR, t0 + 2 * IceCube::ONE_HOUR, true).should be_false
506+
schedule.occurs_between?(t0 + IceCube::ONE_HOUR, t0 + 2 * IceCube::ONE_HOUR, :spans => true).should be_false
507507
end
508508

509509
it 'should quickly fetch a future time from a recurring schedule' do
@@ -513,7 +513,7 @@
513513
schedule.add_recurrence_rule IceCube::Rule.hourly
514514
occ = nil
515515
timing = Benchmark.realtime do
516-
occ = schedule.remaining_occurrences_enumerator(t1, true).take(1)
516+
occ = schedule.remaining_occurrences_enumerator(t1, :spans => true).take(1)
517517
end
518518
timing.should < 0.1
519519
occ.should == [t1]
@@ -523,7 +523,7 @@
523523
t0 = Time.utc(2015, 10, 1, 10, 00)
524524
schedule = IceCube::Schedule.new(t0, :duration => IceCube::ONE_HOUR / 2)
525525
schedule.add_recurrence_rule IceCube::Rule.minutely(30).count(6)
526-
third_occ = schedule.next_occurrence(t0 + IceCube::ONE_HOUR, true)
526+
third_occ = schedule.next_occurrence(t0 + IceCube::ONE_HOUR, :spans => true)
527527
third_occ.should == t0 + IceCube::ONE_HOUR
528528
end
529529

0 commit comments

Comments
 (0)