Add #queue and #next for Schedule

This commit is contained in:
Dzmitry Plashchynski
2015-03-03 15:38:54 +02:00
parent 518160d5d9
commit cd925ea952
2 changed files with 19 additions and 4 deletions

View File

@@ -5,6 +5,16 @@ module Crono
end
def add(peformer, period)
@schedule << [peformer, period]
end
def next
[queue.first[0], queue.first[1].next]
end
private
def queue
@schedule.sort { |a,b| a[1].next <=> b[1].next }
end
end
end

View File

@@ -5,11 +5,16 @@ class TestJob
end
describe Crono::Schedule do
describe "#add" do
it "should add an entry to schedule" do
describe "#next" do
it "should return next job in schedule" do
@schedule = Crono::Schedule.new
@period = Crono::Period.new(1.day)
@schedule.add(TestJob, @period)
[
Crono::Period.new(3.day, at: "18:55"),
Crono::Period.new(1.day, at: "15:30"),
Crono::Period.new(7.day, at: "06:05")
].each { |period| @schedule.add(TestJob, period) }
expect(@schedule.next).to be_eql([TestJob, 1.day.from_now.change(hour: 15, min: 30)])
end
end
end