Class: StartAt

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/startat.rb

Overview

StartAt is class for executing code at a specific point in the future. The object is initialized with a #DateTime object with a point of time in the future and a code block to be executed. Calling #start will calculate the wait time (in seconds), initialize a #Thread that will then sleep for the appropriate amount of time, and then the code block will be executed.

For example:

require 'date'
require 'startat'

bday = DateTime.parse("2009-08-20 08:30:00 AM EDT")
action = lambda { puts "Happy Birthday, Dad!" }
wait_for_it = StartAt.new(bday, &action)
wait_for_it.start
wait_for_it.join

StartAt was originally written to post schedule information for a symposium to Twitter (see the examples directory).

Constant Summary collapse

VERSION =
'0.1.0'
Seconds_per_day =

:nodoc:

24 * 60 * 60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_datetime, &block) ⇒ StartAt

Returns a new future action object that will execute the code block at a point in the future as defined by #start_datetime.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/startat.rb', line 35

def initialize(start_datetime, &block)
  @thread_started = false
  if start_datetime.class == DateTime
    @start_datetime = start_datetime
  else
    raise ArgumentError.new("must have a DateTime object")
  end
  if block
    @action = block
  elsif block_given?
    @action = lambda { yield }
  else 
    raise ArgumentError.new("missing a block")
  end
end

Instance Attribute Details

#start_datetimeObject (readonly)

Returns the value of attribute start_datetime.



31
32
33
# File 'lib/startat.rb', line 31

def start_datetime
  @start_datetime
end

Class Method Details

.get_time_after(future_datetime, time_after) ⇒ Object

Takes a future #DateTime object and returns a #DateTime object for the time after as specified in time_before in seconds.



104
105
106
# File 'lib/startat.rb', line 104

def self.get_time_after(future_datetime, time_after)
  future_datetime + Rational(time_after, Seconds_per_day)
end

.get_time_before(future_datetime, time_before) ⇒ Object

Takes a future #DateTime object and returns a #DateTime object for the time before as specified in time_before in seconds.



98
99
100
# File 'lib/startat.rb', line 98

def self.get_time_before(future_datetime, time_before)
  future_datetime - Rational(time_before, Seconds_per_day)
end

Instance Method Details

#<=>(other) ⇒ Object

Compares the start #DateTime of the current future action with the start time of the #other future action.



53
54
55
# File 'lib/startat.rb', line 53

def <=> (other)
  @start_datetime <=> other.start_datetime
end

#cancelObject Also known as: kill

Cancels a running future action object thread.



77
78
79
80
81
82
# File 'lib/startat.rb', line 77

def cancel
  if ! @thread_started
    @sleeper.exit
    @thread_started = false
  end
end

#joinObject

Joins the current thread to the future action thread. See #Thread.join



92
93
94
# File 'lib/startat.rb', line 92

def join
  @sleeper.join
end

#startObject

Starts the future action thread by determining the time needed to wait based on the start date and then executes the code block once the time has elapsed.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/startat.rb', line 60

def start
  if ! @thread_started
    @sleeper = Thread.new do
      wait = get_wait_time()
      # Check that the wait time is positive, otherwise the future
      # event time has already past. We skip it, since a warning
      # message was produced in wait_time.
      if wait > 0
        sleep wait
        @action.call
      end
    end
    @thread_started = true
  end
end

#statusObject

Returns the status of a future action object thread. See #Thread#status



87
88
89
# File 'lib/startat.rb', line 87

def status
  @sleeper.status
end