Class: EasyTimers::Timers

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_timers/timers.rb

Instance Method Summary collapse

Constructor Details

#initializeTimers

Create a new Timers instance to hold our timers.



10
11
12
# File 'lib/easy_timers/timers.rb', line 10

def initialize()
  @group = Group.new()
end

Instance Method Details

#after(time, name = nil, &block) ⇒ Symbol #after(seconds, name = nil, &block) ⇒ Symbol

Overloads:

  • #after(time, name = nil, &block) ⇒ Symbol

    Schedule a block to be called immediately after the specified time.

    Parameters:

    • time (Time)

      A time to fire the callback.

    • name (Symbol) (defaults to: nil)

      An optional name for the timer. Will be generated if not provided.

    Returns:

    • (Symbol)

      The name of the timer.

  • #after(seconds, name = nil, &block) ⇒ Symbol

    Schedule a block to be called after the given number of seconds.

    Parameters:

    • seconds (Float)

      Number of seconds to wait before firing the callback.

    • name (Symbol) (defaults to: nil)

      An optional name for the timer. Will be generated if not provided.

    Returns:

    • (Symbol)

      The name of the timer.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/easy_timers/timers.rb', line 27

def after(time, name = nil, &block)
  if time.is_a?(Time)
    time = time.gmtime.to_f
  elsif time.is_a?(Numeric)
    time = Time.now.gmtime.to_f + time.to_f
  else
    raise ArgumentError, "Invalid arguments to method."
  end

  timer = Timer.new(time, name, 0, false, block)
  name = @group.insert(timer)
  return name
end

#after_then_every(start_date, interval, name = nil, &block) ⇒ Symbol #after_then_every(start_seconds, interval, name = nil, &block) ⇒ Symbol

Overloads:

  • #after_then_every(start_date, interval, name = nil, &block) ⇒ Symbol

    Schedule a recurring timer that will first fire after the given time.

    Parameters:

    • start_date (Time)

      A time to first fire the callback

    • interval (Float, Fixnum)

      Number of seconds to delay after the callback returns.

    • name (Symbol) (defaults to: nil)

      An optional name for the timer. Will be generated if not provided.

    Returns:

    • (Symbol)

      The name of the timer.

  • #after_then_every(start_seconds, interval, name = nil, &block) ⇒ Symbol

    Schedule a recurring timer that will first fire after the given time. The timer will then be rescheduled every interval.

    Parameters:

    • start_seconds (Float, Fixnum)

      Number of seconds to delay before firing the first callback.

    • interval (Float, Fixnum)

      Number of seconds to delay after the callback returns.

    • name (Symbol) (defaults to: nil)

      An optional name for the timer. Will be generated if not provided.

    Returns:

    • (Symbol)

      The name of the timer.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/easy_timers/timers.rb', line 76

def after_then_every(start, interval, name = nil, &block)

  if start.is_a?(Time) && interval.is_a?(Numeric) && block_given?
    time = start.gmtime.to_f
  elsif start.is_a?(Numeric) && interval.is_a?(Numeric) && block_given?
    time = Time.now.gmtime.to_f + start.to_f
  else
    raise ArgumentError, "Invalid arguments."
  end

  timer = Timer.new(time, name, interval.to_f, true, block)
  name = @group.insert(timer)
  return name
end

#cancel(name) ⇒ Object

Cancel a timer.



93
94
95
# File 'lib/easy_timers/timers.rb', line 93

def cancel(name)
  @group.delete(name)
end

#every(seconds, name = nil, &block) ⇒ Symbol

Schedule a block to fire every given amount of seconds

Parameters:

  • seconds (Float, Fixnum)

    Number of seconds to wait before firing the callback, and the interval between subsequent events.

  • name (Symbol) (defaults to: nil)

    An optional name for the timer. Will be generated if not provided.

Returns:

  • (Symbol)

    The name of the timer.



48
49
50
51
52
53
54
55
56
57
# File 'lib/easy_timers/timers.rb', line 48

def every(seconds, name = nil, &block)
  if seconds <= 0 # zero disallowed because you can't run it every 0 seconds.
    raise ArgumentError, "Interval must be greater than 0."
  end
  interval = seconds.to_f
  time = Time.now.gmtime.to_f + interval
  timer = Timer.new(time, name, interval, true, block)
  name = @group.insert(timer)
  return name
end