From 6d90cb323352d7c614464a729897b9b94b3ba2d8 Mon Sep 17 00:00:00 2001 From: Dzmitry Plashchynski Date: Sat, 7 Mar 2015 04:13:59 +0200 Subject: [PATCH] Period#next should return today time if it is first run and not too late --- lib/crono/cli.rb | 2 +- lib/crono/period.rb | 14 ++++++++++++-- spec/period_spec.rb | 15 ++++++++++++--- spec/scheduler_spec.rb | 6 +----- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/crono/cli.rb b/lib/crono/cli.rb index 231e4ac..f1f90cc 100644 --- a/lib/crono/cli.rb +++ b/lib/crono/cli.rb @@ -56,7 +56,7 @@ module Crono logger.info "Jobs:" Crono.scheduler.jobs.each do |job| - logger.info job.description + logger.info %{"#{job.performer}" with rule "#{job.period.description}" next time will perform at #{job.next}} end end diff --git a/lib/crono/period.rb b/lib/crono/period.rb index 4832ab2..7a3349d 100644 --- a/lib/crono/period.rb +++ b/lib/crono/period.rb @@ -6,8 +6,13 @@ module Crono end def next(since: nil) - since ||= Time.now - @period.since(since).change({hour: @at_hour, min: @at_min}.compact) + if since.nil? + @next = Time.now.change(time_atts) + return @next if @next.future? + since = Time.now + end + + @period.since(since).change(time_atts) end def description @@ -27,5 +32,10 @@ module Crono raise "Unknown 'at' format" end end + + private + def time_atts + {hour: @at_hour, min: @at_min}.compact + end end end diff --git a/spec/period_spec.rb b/spec/period_spec.rb index 29b5b92..699ab45 100644 --- a/spec/period_spec.rb +++ b/spec/period_spec.rb @@ -22,12 +22,14 @@ describe Crono::Period do end it "should set time to 'at' time as a string" do - @period = Crono::Period.new(2.day, at: "15:20") - expect(@period.next).to be_eql(2.day.from_now.change(hour: 15, min: 20)) + time = 10.minutes.ago + @period = Crono::Period.new(2.day, at: [time.hour, time.min].join(':')) + expect(@period.next).to be_eql(2.day.from_now.change(hour: time.hour, min: time.min)) end it "should set time to 'at' time as a hash" do - at = {hour: 18, min: 45} + time = 10.minutes.ago + at = {hour: time.hour, min: time.min} @period = Crono::Period.new(2.day, at: at) expect(@period.next).to be_eql(2.day.from_now.change(at)) end @@ -42,6 +44,13 @@ describe Crono::Period do @period = Crono::Period.new(2.day) expect(@period.next(since: 1.day.ago)).to be_eql(1.day.from_now) end + + it "should return today time if it is first run and not too late" do + time = 10.minutes.from_now + at = {hour: time.hour, min: time.min} + @period = Crono::Period.new(2.day, at: at) + expect(@period.next).to be_eql(Time.now.change(at)) + end end end end diff --git a/spec/scheduler_spec.rb b/spec/scheduler_spec.rb index 0a5d276..63c15c3 100644 --- a/spec/scheduler_spec.rb +++ b/spec/scheduler_spec.rb @@ -25,11 +25,7 @@ describe Crono::Scheduler do describe "#next" do it "should return next job in schedule" do - expect(@scheduler.next).to be @jobs[1] - end - - it "should return next based on last" do - expect(@scheduler.next) + expect(@scheduler.next).to be @jobs[2] end end end