Class: Whenever::Output::Cron

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

Constant Summary collapse

DAYS =
%w(sun mon tue wed thu fri sat)
MONTHS =
%w(jan feb mar apr may jun jul aug sep oct nov dec)
KEYWORDS =
[:reboot, :yearly, :annually, :monthly, :weekly, :daily, :midnight, :hourly]
REGEX =
/^(@(#{KEYWORDS.join '|'})|((\*?[\d\/,\-]*)\s){3}(\*?([\d\/,\-]|(#{MONTHS.join '|'}))*\s)(\*?([\d\/,\-]|(#{DAYS.join '|'}))*))$/i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Cron.



13
14
15
16
17
18
19
20
# File 'lib/whenever/cron.rb', line 13

def initialize(time = nil, task = nil, at = nil, options = {})
  chronic_options = options[:chronic_options] || {}

  @at_given = at
  @time = time
  @task = task
  @at   = at.is_a?(String) ? (Chronic.parse(at, chronic_options) || 0) : (at || 0)
end

Instance Attribute Details

#taskObject

Returns the value of attribute task.



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

def task
  @task
end

#timeObject

Returns the value of attribute time.



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

def time
  @time
end

Class Method Details

.enumerate(item, detect_cron = true) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/whenever/cron.rb', line 22

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, options = {}) ⇒ Object



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

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

Instance Method Details

#outputObject



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

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

#time_in_cron_syntaxObject



49
50
51
52
53
54
55
56
57
# File 'lib/whenever/cron.rb', line 49

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