diff --git a/lib/job_list.rb b/lib/job_list.rb index e43b212..98bad4e 100644 --- a/lib/job_list.rb +++ b/lib/job_list.rb @@ -88,6 +88,26 @@ module Whenever output.join end + def combine entries + entries.map! {|entry| entry.split(/ +/,6)} + 0.upto(5) do |f| + (entries.length-1).downto(1) do |i| + next if entries[i][f] == '*' + comparison = entries[i][0...f] + entries[i][f+1..-1] + (i-1).downto(0) do |j| + next if entries[j][f] == '*' + if comparison == entries[j][0...f] + entries[j][f+1..-1] + entries[j][f] += ',' + entries[i][f] + entries.delete_at(i) + break + end + end + end + end + + entries.map {|entry| entry.join(' ')} + end + def cron_jobs return if @jobs.empty? @@ -102,7 +122,7 @@ module Whenever end end - output.join + combine(output).join end end diff --git a/lib/outputs/cron.rb b/lib/outputs/cron.rb index c770d32..54b36ea 100644 --- a/lib/outputs/cron.rb +++ b/lib/outputs/cron.rb @@ -11,16 +11,22 @@ module Whenever @at = at.is_a?(String) ? (Chronic.parse(at) || 0) : (at || 0) end - def self.output(time, job) - if job.at and job.at.is_a?(String) and !job.at.empty? - ats = job.at.split(',') + def self.enumerate(item) + if item and item.is_a?(String) + items = item.split(',') else - ats = [job.at] + items = item + items = [items] unless items and items.respond_to?(:each) end + items + end - ats.each do |at| - out = new(time, job.output, at) - yield "#{out.time_in_cron_syntax} #{out.task}" + def self.output(times, job) + enumerate(times).each do |time| + enumerate(job.at).each do |at| + out = new(time, job.output, at) + yield "#{out.time_in_cron_syntax} #{out.task}" + end end end diff --git a/test/output_at_test.rb b/test/output_at_test.rb index d6f1ee1..44447be 100644 --- a/test/output_at_test.rb +++ b/test/output_at_test.rb @@ -17,7 +17,23 @@ class OutputAtTest < Test::Unit::TestCase end end - context "weekday at a multiple diverse times" do + context "weekday at a multiple diverse times, via an array" do + setup do + @output = Whenever.cron \ + <<-file + every "weekday", :at=>%w(5:02am 3:52pm) do + command "blahblah" + end + file + end + + should "output the runner using that path" do + assert_match '2 5 * * mon-fri blahblah', @output + assert_match '52 15 * * mon-fri blahblah', @output + end + end + + context "weekday at a multiple diverse times, comma separated" do setup do @output = Whenever.cron \ <<-file @@ -33,4 +49,34 @@ class OutputAtTest < Test::Unit::TestCase end end + context "weekday at a multiple aligned times" do + setup do + @output = Whenever.cron \ + <<-file + every "weekday", :at=>'5:02am, 3:02pm' do + command "blahblah" + end + file + end + + should "output the runner using that path" do + assert_match '2 5,15 * * mon-fri blahblah', @output + end + end + + context "various days at a various aligned times" do + setup do + @output = Whenever.cron \ + <<-file + every "mon,wed,fri", :at=>'5:02am, 3:02pm' do + command "blahblah" + end + file + end + + should "output the runner using that path" do + assert_match '2 5,15 * * mon,wed,fri blahblah', @output + end + end + end