lib/fugit/nat.rb in fugit-1.8.1 vs lib/fugit/nat.rb in fugit-1.9.0
- old
+ new
@@ -640,13 +640,13 @@
if multi == :fail && hms.count > 1
fail(ArgumentError.new(
"multiple crons in #{opts[:_s].inspect} - #{@slots.inspect}"))
elsif multi == true
- hms.collect { |hm| parse_cron(hm) }
+ hms.collect { |hm| parse_cron(hm, opts) }
else
- parse_cron(hms.first)
+ parse_cron(hms.first, opts)
end
end
protected
@@ -676,11 +676,11 @@
(r[hs.sort] ||= []) << m
r }
.to_a
end
- def parse_cron(hm)
+ def parse_cron(hm, opts)
a = [
slot(:second, '0'),
hm[1],
hm[0],
@@ -689,14 +689,42 @@
slot(:weekday, '*') ]
tz = @slots[:tz]
a << tz.data0 if tz
a.shift if a.first == [ '0' ]
+ letters_last = lambda { |x| x.is_a?(Numeric) ? x : 999_999 }
+
s = a
- .collect { |e| e.uniq.sort.collect(&:to_s).join(',') }
+ .collect { |e|
+ e.uniq.sort_by(&letters_last).collect(&:to_s).join(',') }
.join(' ')
- Fugit::Cron.parse(s)
+ c = Fugit::Cron.parse(s)
+
+ if opts[:strict]
+ restrict(a, c)
+ else
+ c
+ end
+ end
+
+ # Return nil if the cron is "not strict"
+ #
+ # For example, "0 0/17 * * *" (gh-86) is a perfectly valid
+ # cron string, but makes not much sense when derived via `.parse_nat`
+ # from "every 17 hours".
+ #
+ # It happens here because it's nat being strict, not cron.
+ #
+ def restrict(a, cron)
+
+ if m = ((a[1] && a[1][0]) || '').match(/^(\d+|\*)\/(\d+)$/)
+#p m
+ sla = m[1].to_i
+ return nil unless [ 1, 2, 3, 4, 5, 6, 8, 12 ].include?(sla)
+ end
+
+ cron
end
def slot(key, default)
s = @slots[key]
s ? s.data0 : [ default ]