Class: Colonel::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/colonel/parser.rb

Defined Under Namespace

Classes: Command, Schedule, TimeParser

Constant Summary collapse

SCHEDULE_SIZE =
5
PERIOD =
/@hourly|@daily|@weekly|@monthly|@yearly/

Instance Method Summary collapse

Constructor Details

#initialize(text, opts = {}) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
11
# File 'lib/colonel/parser.rb', line 7

def initialize(text, opts={})
  @all_flag = opts[:all_flag].nil? ? true : opts[:all_flag]
  filtered_text = replace_periods(text)
  @tokens = scan_tokens(filtered_text)
end

Instance Method Details

#command_tokensObject



42
43
44
# File 'lib/colonel/parser.rb', line 42

def command_tokens
  @tokens.last(@tokens.size - SCHEDULE_SIZE)
end

#executeObject



46
47
48
# File 'lib/colonel/parser.rb', line 46

def execute
  Job.new Schedule.new(schedule_tokens: schedule_tokens, all_flag: @all_flag), Command.new(command_tokens)
end

#replace_periods(text) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/colonel/parser.rb', line 17

def replace_periods(text)
  period = text.scan(PERIOD).first
  if period
    time_string = case period
                    when "@hourly"
                      "0 * * * *"
                    when "@daily"
                      "0 0 * * *"
                    when "@weekly"
                      "0 0 * * 0"
                    when "@monthly"
                      "0 0 1 * *"
                    when "@yearly"
                      "0 0 1 1 *"
                  end
    text.gsub(/^(#{PERIOD})/, time_string)
  else
    text
  end
end

#scan_tokens(text) ⇒ Object



13
14
15
# File 'lib/colonel/parser.rb', line 13

def scan_tokens(text)
  text.scan(/[\S]+/)
end

#schedule_tokensObject



38
39
40
# File 'lib/colonel/parser.rb', line 38

def schedule_tokens
  @tokens.first(SCHEDULE_SIZE)
end