Class: SimpleScheduler::At

Inherits:
Time
  • Object
show all
Defined in:
lib/simple_scheduler/at.rb

Overview

A Time class for parsing the :at option on a task into the first time it should run.

Time.now
# => 2016-12-09 08:24:11 -0600
SimpleScheduler::At.new("*:30")
# => 2016-12-09 08:30:00 -0600
SimpleScheduler::At.new("1:00")
# => 2016-12-10 01:00:00 -0600
SimpleScheduler::At.new("Sun 0:00")
# => 2016-12-11 00:00:00 -0600

Defined Under Namespace

Classes: InvalidTime

Constant Summary collapse

AT_PATTERN =
/\A(Sun|Mon|Tue|Wed|Thu|Fri|Sat)?\s?(?:\*{1,2}|((?:\b[0-1]?[0-9]|2[0-3]))):([0-5]\d)\z/
DAYS =
%w(Sun Mon Tue Wed Thu Fri Sat).freeze

Instance Method Summary collapse

Constructor Details

#initialize(at, time_zone = nil) ⇒ At

Accepts a time string to determine when a task should be run for the first time. Valid formats:

"18:00"
"3:30"
"**:00"
"*:30"
"Sun 2:00"
"[Sun|Mon|Tue|Wed|Thu|Fri|Sat] 00:00"

Parameters:

  • at (String)

    The formatted string for a task's run time

  • time_zone (ActiveSupport::TimeZone) (defaults to: nil)

    The time zone to parse the at time in



28
29
30
31
32
33
# File 'lib/simple_scheduler/at.rb', line 28

def initialize(at, time_zone = nil)
  @at = at
  @time_zone = time_zone || Time.zone
  super(parsed_time.year, parsed_time.month, parsed_time.day,
        parsed_time.hour, parsed_time.min, parsed_time.sec, parsed_time.utc_offset)
end

Instance Method Details

#hourInteger

Always returns the specified hour if the hour was given, otherwise it returns the hour calculated based on other specified options.

Returns:

  • (Integer)


38
39
40
# File 'lib/simple_scheduler/at.rb', line 38

def hour
  hour? ? at_hour : super
end

#hour?Boolean

Returns whether or not the hour was specified in the :at string.

Returns:

  • (Boolean)


44
45
46
# File 'lib/simple_scheduler/at.rb', line 44

def hour?
  at_match[2].present?
end