Class: Xronor::DSL::ScheduleConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/xronor/dsl/schedule_converter.rb

Constant Summary collapse

WEEKDAYS =
%i(sunday monday tuesday wednesday thursday friday saturday)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(frequency, options) ⇒ ScheduleConverter

Returns a new instance of ScheduleConverter.



39
40
41
42
# File 'lib/xronor/dsl/schedule_converter.rb', line 39

def initialize(frequency, options)
  @frequency = frequency
  @options = options
end

Class Method Details

.convert(frequency, options) ⇒ Object



34
35
36
# File 'lib/xronor/dsl/schedule_converter.rb', line 34

def convert(frequency, options)
  self.new(frequency, options).convert
end

Instance Method Details

#convertObject

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/xronor/dsl/schedule_converter.rb', line 44

def convert
  cron_at, dow_diff = case @options[:at]
                      when String
                        parse_and_convert_time
                      when Numeric
                        [@options[:at], 0]
                      else
                        [0, 0]
                      end

  if WEEKDAYS.include?(@frequency) # :sunday, :monday, ..., :saturday
    return cron_weekly(cron_at, dow_diff)
  end

  shortcut = case @frequency
             when Numeric
               @frequency
             when :minute
               Xronor::DSL.seconds(1, :minute)
             when :hour
               Xronor::DSL.seconds(1, :hour)
             when :day
               Xronor::DSL.seconds(1, :day)
             end

  raise ArgumentError, "Invalid frequency #{@frequency}" if !shortcut.is_a?(Numeric)

  cron(shortcut, cron_at)
end