diff --git a/lib/crono/schedule.rb b/lib/crono/schedule.rb index 79e841b..1fd7c09 100644 --- a/lib/crono/schedule.rb +++ b/lib/crono/schedule.rb @@ -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 diff --git a/spec/schedule_spec.rb b/spec/schedule_spec.rb index 6e6bee7..cef32e5 100644 --- a/spec/schedule_spec.rb +++ b/spec/schedule_spec.rb @@ -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