Period should only return future time

This commit is contained in:
Dzmitry Plashchynski
2015-04-12 16:48:57 +03:00
parent 55e3956618
commit f6b393ad6b
2 changed files with 17 additions and 10 deletions
+4 -2
View File
@@ -1,5 +1,5 @@
module Crono module Crono
# Period describe frequency of performing a task # Period describe frequency of jobs
class Period class Period
DAYS = [:monday, :tuesday, :wednesday, :thursday, :friday, :saturday, DAYS = [:monday, :tuesday, :wednesday, :thursday, :friday, :saturday,
:sunday] :sunday]
@@ -14,7 +14,9 @@ module Crono
return initial_next unless since return initial_next unless since
@next = @period.since(since) @next = @period.since(since)
@next = @next.beginning_of_week.advance(days: @on) if @on @next = @next.beginning_of_week.advance(days: @on) if @on
@next.change(time_atts) @next = @next.change(time_atts)
return @next if @next.future?
Time.now
end end
def description def description
+13 -8
View File
@@ -53,28 +53,33 @@ describe Crono::Period do
end end
context 'in daily basis' do context 'in daily basis' do
it "should return Time.now if the next time in past" do
@period = Crono::Period.new(1.day, at: '06:00')
expect(@period.next(since: 2.days.ago)).to be_eql(Time.now)
end
it 'should return the time 2 days from now' do it 'should return the time 2 days from now' do
@period = Crono::Period.new(2.day) @period = Crono::Period.new(2.day)
expect(@period.next).to be_eql(2.day.from_now) expect(@period.next).to be_eql(2.days.from_now)
end end
it "should set time to 'at' time as a string" do it "should set time to 'at' time as a string" do
time = 10.minutes.ago time = 10.minutes.ago
at = [time.hour, time.min].join(':') at = [time.hour, time.min].join(':')
@period = Crono::Period.new(2.day, at: at) @period = Crono::Period.new(2.days, at: at)
expect(@period.next).to be_eql(2.day.from_now.change(hour: time.hour, min: time.min)) expect(@period.next).to be_eql(2.days.from_now.change(hour: time.hour, min: time.min))
end end
it "should set time to 'at' time as a hash" do it "should set time to 'at' time as a hash" do
time = 10.minutes.ago time = 10.minutes.ago
at = { hour: time.hour, min: time.min } at = { hour: time.hour, min: time.min }
@period = Crono::Period.new(2.day, at: at) @period = Crono::Period.new(2.days, at: at)
expect(@period.next).to be_eql(2.day.from_now.change(at)) expect(@period.next).to be_eql(2.days.from_now.change(at))
end end
it "should raise error when 'at' is wrong" do it "should raise error when 'at' is wrong" do
expect { expect {
Crono::Period.new(2.day, at: 1) Crono::Period.new(2.days, at: 1)
}.to raise_error("Unknown 'at' format") }.to raise_error("Unknown 'at' format")
end end
@@ -85,14 +90,14 @@ describe Crono::Period do
end end
it 'should return time in relation to last time' do it 'should return time in relation to last time' do
@period = Crono::Period.new(2.day) @period = Crono::Period.new(2.days)
expect(@period.next(since: 1.day.ago)).to be_eql(1.day.from_now) expect(@period.next(since: 1.day.ago)).to be_eql(1.day.from_now)
end end
it 'should return today time if it is first run and not too late' do it 'should return today time if it is first run and not too late' do
time = 10.minutes.from_now time = 10.minutes.from_now
at = { hour: time.hour, min: time.min } at = { hour: time.hour, min: time.min }
@period = Crono::Period.new(2.day, at: at) @period = Crono::Period.new(2.days, at: at)
expect(@period.next.utc.to_s).to be_eql(Time.now.change(at).utc.to_s) expect(@period.next.utc.to_s).to be_eql(Time.now.change(at).utc.to_s)
end end
end end