module Fugit::Nat
def parse_crons(s, a, opts)
def parse_crons(s, a, opts) return nil unless a h = a .reverse .inject({}) { |r, e| send("parse_#{e[0]}_elt", e, opts, r); r } # # the reverse ensure that in "every day at five", the # "at five" is placed before the "every day" so that # parse_x_elt calls have the right sequence if f = h[:_fail] #fail ArgumentError.new(f) return nil end hms = h[:hms] hours = (hms || []) .uniq .inject({}) { |r, hm| (r[hm[1]] ||= []) << hm[0]; r } .inject({}) { |r, (m, hs)| (r[hs.sort] ||= []) << m; r } .to_a .sort_by { |hs, ms| -hs.size } if hours.empty? hours << (h[:dom] ? [ [ '0' ], [ '0' ] ] : [ [ '*' ], [ '*' ] ]) end crons = hours .collect { |hm| assemble_cron(h.merge(hms: hm)) } fail ArgumentError.new( "multiple crons in #{s.inspect} " + "(#{crons.collect(&:original).join(' | ')})" ) if opts[:multi] == :fail && crons.size > 1 if opts[:multi] == true || (opts[:multi] && opts[:multi] != :fail) crons else crons.first end end