Module: Fugit::Nat

Defined in:
lib/fugit/nat.rb

Overview

A natural language set of parsers for fugit. Focuses on cron expressions. The rest is better left to Chronic and friends.

Defined Under Namespace

Modules: Parser

Class Method Summary collapse

Class Method Details

.do_parse(s) ⇒ Object



28
29
30
31
# File 'lib/fugit/nat.rb', line 28

def self.do_parse(s)

  parse(s) || fail(ArgumentError.new("could not parse a nat #{s.inspect}"))
end

.parse(s) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fugit/nat.rb', line 9

def self.parse(s)

  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))
  a = Parser.parse(s)

#p a
  return nil unless a

  if a.include?([ :flag, 'every' ])
    parse_cron(a)
  else
    nil
  end
end

.parse_cron(a) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fugit/nat.rb', line 33

def self.parse_cron(a)

  h = { min: nil, hou: [], dom: [ nil ], mon: [ nil ], dow: [ nil ] }

  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, 1] ]
      h[:min] = [ val[1, 1] ]
    elsif key == :name_day
      (h[:dow] ||= []) << [ val ]
    elsif key == :flag && val == 'pm' && h[:hou]
      h[:hou][-1] =  [ h[:hou][-1].first + 12 ]
    end
  end
  h[:min] ||= [ 0 ]
  h[:dow].sort_by! { |a, z| a || 0 }

  Fugit::Cron.allocate.send(:init, nil, h)
end