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 Classes: Slot, SlotGroup

Constant Summary collapse

MAX_INPUT_LENGTH =
256

Class Method Summary collapse

Class Method Details

.do_parse(s, opts = {}) ⇒ Object



42
43
44
45
46
# File 'lib/fugit/nat.rb', line 42

def do_parse(s, opts={})

  parse(s, opts.merge(do_parse: true)) ||
  fail(ArgumentError.new("could not parse a nat #{s.inspect}"))
end

.parse(s, opts = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fugit/nat.rb', line 14

def parse(s, opts={})

  return s if s.is_a?(Fugit::Cron) || s.is_a?(Fugit::Duration)

  return nil unless s.is_a?(String)

  s = s.strip

  if s.length > MAX_INPUT_LENGTH

    fail ArgumentError.new(
      "input too long for a nat string, " +
      "#{s.length} > #{MAX_INPUT_LENGTH}"
    ) if opts[:do_parse]

    return nil
  end

#p s; Raabro.pp(Parser.parse(s, debug: 3), colours: true)
#(p s; Raabro.pp(Parser.parse(s, debug: 1), colours: true)) rescue nil

  if slots = Parser.parse(s)
    slots.to_crons(opts.merge(_s: s))
  else
    nil
  end
end