lib/fugit/cron.rb in fugit-1.1.4 vs lib/fugit/cron.rb in fugit-1.1.5

- old
+ new

@@ -248,10 +248,44 @@ Frequency.new(deltas, t1 - t0) end end + SLOTS = [ + [ :seconds, 1, 60 ], + [ :minutes, 60, 60 ], + [ :hours, 3600, 24 ], + [ :days, 24 * 3600, 365 ] ] + + def rough_frequency + + slots = SLOTS + .collect { |k, v0, v1| + a = (k == :days) ? rough_days : instance_variable_get("@#{k}") + [ k, v0, v1, a ] } + + slots.each do |k, v0, _, a| + next if a == [ 0 ] + break if a != nil + return v0 if a == nil + end + + slots.each do |k, v0, v1, a| + next unless a && a.length > 1 + return (a + [ a.first + v1 ]) + .each_cons(2) + .collect { |a0, a1| a1 - a0 } + .min * v0 + end + + slots.reverse.each do |k, v0, v1, a| + return v0 * v1 if a && a.length == 1 + end + + 1 # second + end + class Frequency attr_reader :span, :delta_min, :delta_max, :occurrences attr_reader :span_years, :yearly_occurrences @@ -281,10 +315,20 @@ def to_a [ @seconds, @minutes, @hours, @monthdays, @months, @weekdays ] end + def to_h + + { seconds: @seconds, + minutes: @minutes, + hours: @hours, + monthdays: @monthdays, + months: @months, + weekdays: @weekdays } + end + def ==(o) o.is_a?(::Fugit::Cron) && o.to_a == to_a end alias eql? == @@ -293,9 +337,34 @@ to_a.hash end protected + + def rough_days + + return nil if @weekdays == nil && @monthdays == nil + + months = (@months || (1..12).to_a) + + monthdays = months + .product(@monthdays || []) + .collect { |m, d| + d = 31 + d if d < 0 + (m - 1) * 30 + d } # rough + + weekdays = (@weekdays || []) + .collect { |d, w| + w ? + d + (w - 1) * 7 : + (0..3).collect { |ww| d + ww * 7 } } + .flatten + weekdays = months + .product(weekdays) + .collect { |m, d| (m - 1) * 30 + d } # rough + + (monthdays + weekdays).sort + end FREQUENCY_CACHE = {} def init(original, h)