lib/fugit/cron.rb in fugit-1.9.0 vs lib/fugit/cron.rb in fugit-1.10.0
- old
+ new
@@ -316,9 +316,56 @@
end
t.time.translate(from.zone)
end
+ # Used by Fugit::Cron#next and Fugit::Cron#prev
+ #
+ class CronIterator include ::Enumerable
+ attr_reader :cron, :start, :current, :direction
+ def initialize(cron, direction, start)
+ @cron = cron
+ @start = start
+ @current = start.dup
+ @direction = direction
+ end
+ def each
+ loop do
+ yield(@current = @cron.send(@direction, @current))
+ end
+ end
+ end
+
+ # Returns an ::Enumerable instance that yields each "next time" in
+ # succession
+ #
+ def next(from=::EtOrbi::EoTime.now)
+
+ CronIterator.new(self, :next_time, from)
+ end
+
+ # Returns an ::Enumerable instance that yields each "previous time" in
+ # succession
+ #
+ def prev(from=::EtOrbi::EoTime.now)
+
+ CronIterator.new(self, :previous_time, from)
+ end
+
+ # Returns an array of EtOrbi::EoTime instances that correspond to
+ # the occurrences of the cron within the given time range
+ #
+ def within(time_range, time_end=nil)
+
+ sta, ned =
+ time_range.is_a?(::Range) ? [ time_range.begin, time_range.end ] :
+ [ ::EtOrbi.make_time(time_range), ::EtOrbi.make_time(time_end) ]
+
+ CronIterator
+ .new(self, :next_time, sta)
+ .take_while { |eot| eot.to_t < ned }
+ end
+
# Mostly used as a #next_time sanity check.
# Avoid for "business" use, it's slow.
#
# 2017 is a non leap year (though it is preceded by
# a leap second on 2016-12-31)