Class: Whenever::Output::Cron

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

Constant Summary collapse

KEYWORDS =
[:reboot, :yearly, :annually, :monthly, :weekly, :daily, :midnight, :hourly]
REGEX =
/^(@(#{KEYWORDS.join '|'})|((\*?[\d\/,\-]*)\s*){5})$/

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.



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

def initialize(time = nil, task = nil, at = nil)
  @at_given = at
  @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.



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

def task
  @task
end

#timeObject

Returns the value of attribute time.



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

def time
  @time
end

Class Method Details

.enumerate(item, detect_cron = true) ⇒ Object



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

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



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

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



41
42
43
# File 'lib/whenever/cron.rb', line 41

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

#time_in_cron_syntaxObject



45
46
47
48
49
50
51
52
53
# File 'lib/whenever/cron.rb', line 45

def time_in_cron_syntax
  @time = @time.to_i if @time.is_a?(Numeric) # Compatibility with `1.day` format using ruby 2.3 and activesupport
  case @time
    when REGEX  then @time # raw cron syntax given
    when Symbol then parse_symbol
    when String then parse_as_string
    else parse_time
  end
end