lib/fugit/cron.rb in fugit-1.2.0 vs lib/fugit/cron.rb in fugit-1.2.1

- old
+ new

@@ -10,13 +10,17 @@ '@monthly' => '0 0 1 * *', '@weekly' => '0 0 * * 0', '@daily' => '0 0 * * *', '@midnight' => '0 0 * * *', '@hourly' => '0 * * * *' } + MAXDAYS = [ + nil, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] - attr_reader :original, :zone - attr_reader :seconds, :minutes, :hours, :monthdays, :months, :weekdays, :timezone + attr_reader( + :original, :zone) + attr_reader( + :seconds, :minutes, :hours, :monthdays, :months, :weekdays, :timezone) class << self def new(original) @@ -222,10 +226,11 @@ loop do fail RuntimeError.new( "too many loops for #{@original.inspect} #next_time, breaking, " + + "cron expression most likely invalid (Feb 30th like?), " + "please fill an issue at https://git.io/fjJC9" ) if (i += 1) > MAX_ITERATION_COUNT (ifrom == t.to_i) && (t.inc(1); next) month_match?(t) || (t.inc_month; next) @@ -258,10 +263,11 @@ loop do fail RuntimeError.new( "too many loops for #{@original.inspect} #previous_time, breaking, " + + "cron expression most likely invalid (Feb 30th like?), " + "please fill an issue at https://git.io/fjJCQ" ) if (i += 1) > MAX_ITERATION_COUNT month_match?(t) || (t.dec_month; next) day_match?(t) || (t.dec_day; next) @@ -394,10 +400,26 @@ to_a.hash end protected + def compact_month_days + + return true if @months == nil || @monthdays == nil + + ms, ds = + @months.inject([ [], [] ]) { |a, m| + @monthdays.each { |d| + next if d > MAXDAYS[m] + a[0] << m; a[1] << d } + a } + @months = ms.uniq + @monthdays = ds.uniq + + @months.any? && @monthdays.any? + end + def rough_days return nil if @weekdays == nil && @monthdays == nil months = (@months || (1..12).to_a) @@ -433,9 +455,11 @@ determine_hours(h[:hou]) determine_monthdays(h[:dom]) determine_months(h[:mon]) determine_weekdays(h[:dow]) determine_timezone(h[:tz]) + + return nil unless compact_month_days self end def expand(min, max, r)