Class: Winever::CronTime

Inherits:
Object
  • Object
show all
Defined in:
lib/winever/cron_time.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cron_time_string) ⇒ CronTime

Returns a new instance of CronTime.



5
6
7
8
9
10
11
12
13
# File 'lib/winever/cron_time.rb', line 5

def initialize cron_time_string
  @string = cron_time_string

  @parts = Array.new(5, '')
  string_parts = cron_time_string.split(/ +/)
  @parts[0...string_parts.size] = string_parts

  @minute, @hour, @day, @month, @dow = @parts
end

Instance Attribute Details

#dayObject

Returns the value of attribute day.



3
4
5
# File 'lib/winever/cron_time.rb', line 3

def day
  @day
end

#dowObject

Returns the value of attribute dow.



3
4
5
# File 'lib/winever/cron_time.rb', line 3

def dow
  @dow
end

#hourObject

Returns the value of attribute hour.



3
4
5
# File 'lib/winever/cron_time.rb', line 3

def hour
  @hour
end

#minuteObject

Returns the value of attribute minute.



3
4
5
# File 'lib/winever/cron_time.rb', line 3

def minute
  @minute
end

#monthObject

Returns the value of attribute month.



3
4
5
# File 'lib/winever/cron_time.rb', line 3

def month
  @month
end

#partsObject

Returns the value of attribute parts.



3
4
5
# File 'lib/winever/cron_time.rb', line 3

def parts
  @parts
end

#stringObject

Returns the value of attribute string.



3
4
5
# File 'lib/winever/cron_time.rb', line 3

def string
  @string
end

Instance Method Details

#supported?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/winever/cron_time.rb', line 33

def supported?
  unsupported_reason.nil?
end

#triggersObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/winever/cron_time.rb', line 15

def triggers
  # For now, we don't support anything other than specific time.
  # But it is possible to handle almost all cron schedule options in the task scheduler of Windows.
  # It doesn't help that win32-taskscheduler also seems to only support one trigger per task.

  return [] unless supported?
  trigger = {
      :start_year => Date.today.year,
      :start_month => Date.today.month,
      :start_day => Date.today.day,
      :start_hour => hour.to_i,
      :start_minute => minute.to_i,
      :trigger_type => Win32::TaskScheduler::TASK_TIME_TRIGGER_DAILY
  }

  [trigger]
end

#unsupported_reasonObject



37
38
39
40
41
42
# File 'lib/winever/cron_time.rb', line 37

def unsupported_reason
  return "Need 5 parts delimited by spaces" if parts.compact.reject(&:empty?).length != 5
  return "Only '*' is supported for day, month and day or week parts" if [day, month, dow].detect{|v| v != '*'}
  return "Only single number is supported for minute and hour parts" if [minute, hour].detect{|v| (v =~ /^\d+$/).nil? }
  nil
end