lib/fugit/nat.rb in fugit-1.2.2 vs lib/fugit/nat.rb in fugit-1.2.3
- old
+ new
@@ -6,56 +6,62 @@
#
module Nat
class << self
- def parse(s)
+ def parse(s, opts={})
return s if s.is_a?(Fugit::Cron) || s.is_a?(Fugit::Duration)
return nil unless s.is_a?(String)
#p s; Raabro.pp(Parser.parse(s, debug: 3), colours: true)
a = Parser.parse(s)
- if a && a.include?([ :flag, 'every' ])
- parse_cron(a)
- else
- nil
- end
+ return nil unless a
+
+ return parse_cron(a, opts) \
+ if a.include?([ :flag, 'every' ])
+ return parse_cron(a, opts) \
+ if a.include?([ :flag, 'from' ]) && a.find { |e| e[0] == :day_range }
+
+ nil
end
- def do_parse(s)
+ def do_parse(s, opts={})
- parse(s) ||
+ parse(s, opts) ||
fail(ArgumentError.new("could not parse a nat #{s.inspect}"))
end
- def parse_cron(a)
+ def parse_cron(a, opts={})
h = { min: nil, hou: [], dom: nil, mon: nil, dow: nil }
hkeys = h.keys
a.each do |key, val|
if key == :biz_day
(h[:dow] ||= []) << '1-5'
elsif key == :simple_hour || key == :numeral_hour
h[:hou] << val
elsif key == :digital_hour
- h[:hou] = [ val[0] ]
- h[:min] = [ val[1] ]
+ (h[:hou] ||= []) << val[0].to_i
+ (h[:min] ||= []) << val[1].to_i
elsif key == :name_day
(h[:dow] ||= []) << val
+ elsif key == :day_range
+ (h[:dow] ||= []) << val.collect { |v| v.to_s[0, 3] }.join('-')
elsif key == :flag && val == 'pm' && h[:hou]
h[:hou][-1] = h[:hou][-1] + 12
elsif key == :tz
h[:tz] = val
elsif key == :duration
process_duration(h, *val[0].to_h.first)
end
end
h[:min] ||= [ 0 ]
+ h[:min].uniq!
h[:hou].sort! if h[:hou]
h[:dow].sort! if h[:dow]
a = hkeys
.collect { |k|
@@ -63,13 +69,16 @@
(v && v.any?) ? v.collect(&:to_s).join(',') : '*' }
a.insert(0, h[:sec]) if h[:sec]
a << h[:tz].first if h[:tz]
s = a.join(' ')
+#p s
Fugit::Cron.parse(s)
end
+ protected
+
def process_duration(h, interval, value)
send("process_duration_#{interval}", h, value)
end
@@ -135,10 +144,16 @@
def plain_day(i); rex(:plain_day, i, /day/i); end
def biz_day(i); rex(:biz_day, i, /(biz|business|week) *day/i); end
def name_day(i); rex(:name_day, i, /#{WEEKDAYS.reverse.join('|')}/i); end
+ def range_sep(i); rex(nil, i, / *- *| +(to|through) +/); end
+
+ def day_range(i)
+ seq(:day_range, i, :name_day, :range_sep, :name_day)
+ end
+
def _tz_name(i)
rex(nil, i, /[A-Z][a-zA-Z0-9+\-]+(\/[A-Z][a-zA-Z0-9+\-_]+){0,2}/)
end
def _tz_delta(i)
rex(nil, i, /[-+]([01][0-9]|2[0-4]):?(00|15|30|45)/)
@@ -153,14 +168,15 @@
\s?
(mon(ths?)?|d(ays?)?|h(ours?)?|m(in(ute)?s?)?|s(ec(ond)?s?)?)
/ix)
end
- def flag(i); rex(:flag, i, /(every|at|after|am|pm|on|in)/i); end
+ def flag(i); rex(:flag, i, /(every|from|at|after|am|pm|on|in)/i); end
def datum(i)
alt(nil, i,
+ :day_range,
:plain_day, :biz_day, :name_day,
:_tz,
:flag,
:duration,
:name_hour, :numeral_hour, :digital_hour, :simple_hour)
@@ -197,9 +213,11 @@
[ k, [ v[0, 2], v[2, 2] ] ]
when :name_hour
[ :digital_hour, NHOURS[v] ]
when :name_day
[ k, WEEKDAYS.index(v[0, 3]) ]
+ when :day_range
+ [ k, tt.subgather(nil).collect { |st| st.string.downcase } ]
else
[ k, v ]
end }
end
end