lib/fugit/cron.rb in fugit-0.9.0 vs lib/fugit/cron.rb in fugit-0.9.1
- old
+ new
@@ -59,23 +59,28 @@
].join(' ')
end
def self.parse(s)
+ return s if s.is_a?(self)
+
original = s
s = SPECIALS[s] || s
#p s; Raabro.pp(Parser.parse(s, debug: 3))
h = Parser.parse(s)
- fail ArgumentError.new(
- "couldn't parse #{original.inspect}"
- ) unless h
+ return nil unless h
self.allocate.send(:init, s, h)
end
+ def self.do_parse(s)
+
+ parse(s) || fail(ArgumentError.new("not a cron string #{s.inspect}"))
+ end
+
class NextTime # TODO at some point, use ZoTime
def initialize(t)
@t = t.is_a?(NextTime) ? t.time : t
end
@@ -173,10 +178,11 @@
true
end
def match?(t)
+ t = Fugit.do_parse_at(t)
t = NextTime.new(t)
month_match?(t) && day_match?(t) && hour_match?(t) && min_match?(t)
end
@@ -214,11 +220,11 @@
end
# Returns [ min delta, max delta, occurence count ]
# Computes for a non leap year (2017).
#
- def frequency(year=2017)
+ def brute_frequency(year=2017)
FREQUENCY_CACHE["#{to_cron_s}|#{year}"] ||=
begin
deltas = []
@@ -230,9 +236,25 @@
t0 = t1 + 60
end
[ deltas.min, deltas.max, deltas.size ]
end
+ end
+
+ def to_a
+
+ [ @minutes, @hours, @monthdays, @months, @weekdays ]
+ end
+
+ def ==(o)
+
+ o.is_a?(::Fugit::Cron) && o.to_a == to_a
+ end
+ alias eql? ==
+
+ def hash
+
+ to_a.hash
end
protected
FREQUENCY_CACHE = {}