Class: EasyTimers::Timer

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

Overview

Wraps a timer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time, name, interval, recurring, callback) ⇒ Timer

Create a new instance

Parameters:

  • time (Float)

    Seconds since epoch.

  • name (Symbol)

    A name for this timer; generated from the current clock time if nil.

  • interval (Float)

    Seconds.

  • recurring (Boolean)
  • callback (Callable)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/easy_timers/timer.rb', line 15

def initialize(time, name, interval, recurring, callback)
  @time = time
  @name = name
  @interval = interval
  @recurring = recurring
  @callback = callback
  @cancelled = false

  if @name == nil
    @name = Time.now.gmtime.to_f.to_s.to_sym
  end
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



7
8
9
# File 'lib/easy_timers/timer.rb', line 7

def callback
  @callback
end

#intervalObject (readonly)

Returns the value of attribute interval.



7
8
9
# File 'lib/easy_timers/timer.rb', line 7

def interval
  @interval
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/easy_timers/timer.rb', line 7

def name
  @name
end

#recurringObject (readonly)

Returns the value of attribute recurring.



7
8
9
# File 'lib/easy_timers/timer.rb', line 7

def recurring
  @recurring
end

#timeObject (readonly)

Returns the value of attribute time.



7
8
9
# File 'lib/easy_timers/timer.rb', line 7

def time
  @time
end

Instance Method Details

#cancelObject

Cancel the timer by overwriting the callback



30
31
32
33
# File 'lib/easy_timers/timer.rb', line 30

def cancel()
  @callback = nil
  @cancelled = true
end

#cancelled?Boolean

Check if this timer has been cancelled.

Returns:

  • (Boolean)


37
38
39
# File 'lib/easy_timers/timer.rb', line 37

def cancelled?
  return @cancelled
end