Class: Whenever::Output::Cron

Inherits:
Object
  • Object
show all
Defined in:
lib/whenever/cron.rb

Constant Summary collapse

REGEX =
/^.+ .+ .+ .+ .+.?$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time = nil, task = nil, at = nil) ⇒ Cron

Returns a new instance of Cron.



10
11
12
13
14
# File 'lib/whenever/cron.rb', line 10

def initialize(time = nil, task = nil, at = nil)
  @time = time
  @task = task
  @at   = at.is_a?(String) ? (Chronic.parse(at) || 0) : (at || 0)
end

Instance Attribute Details

#taskObject

Returns the value of attribute task.



8
9
10
# File 'lib/whenever/cron.rb', line 8

def task
  @task
end

#timeObject

Returns the value of attribute time.



8
9
10
# File 'lib/whenever/cron.rb', line 8

def time
  @time
end

Class Method Details

.enumerate(item, detect_cron = true) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/whenever/cron.rb', line 16

def self.enumerate(item, detect_cron = true)
  if item and item.is_a?(String)
    items = 
      if detect_cron && item =~ REGEX
        [item]
      else
        item.split(',')
      end
  else
    items = item
    items = [items] unless items and items.respond_to?(:each)
  end
  items
end

.output(times, job) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/whenever/cron.rb', line 31

def self.output(times, job)
  enumerate(times).each do |time|
    enumerate(job.at, false).each do |at|
      yield new(time, job.output, at).output
    end
  end
end

Instance Method Details

#outputObject



39
40
41
# File 'lib/whenever/cron.rb', line 39

def output
  [time_in_cron_syntax, task].compact.join(' ').strip
end

#time_in_cron_syntaxObject



43
44
45
46
47
48
49
50
# File 'lib/whenever/cron.rb', line 43

def time_in_cron_syntax
  case @time
    when REGEX  then @time # raw cron sytax given
    when Symbol then parse_symbol
    when String then parse_as_string
    else parse_time
  end
end