Class: Whenever::Output::Cron

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

Defined Under Namespace

Classes: OutputRedirection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Cron.



8
9
10
11
12
13
# File 'lib/whenever/outputs/cron.rb', line 8

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

Instance Attribute Details

#taskObject

Returns the value of attribute task.



6
7
8
# File 'lib/whenever/outputs/cron.rb', line 6

def task
  @task
end

#timeObject

Returns the value of attribute time.



6
7
8
# File 'lib/whenever/outputs/cron.rb', line 6

def time
  @time
end

Class Method Details

.enumerate(item) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/whenever/outputs/cron.rb', line 15

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

.output(times, job) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/whenever/outputs/cron.rb', line 25

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

Instance Method Details

#outputObject



33
34
35
# File 'lib/whenever/outputs/cron.rb', line 33

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

#output_redirectionObject



45
46
47
# File 'lib/whenever/outputs/cron.rb', line 45

def output_redirection
  OutputRedirection.new(@output_redirection).to_s unless @output_redirection == :not_set
end

#time_in_cron_syntaxObject



37
38
39
40
41
42
43
# File 'lib/whenever/outputs/cron.rb', line 37

def time_in_cron_syntax
  case @time
    when Symbol then parse_symbol
    when String then parse_as_string
    else parse_time
  end
end