Class: RestCore::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/rest-core/timer.rb

Constant Summary collapse

TimerGen =
if Timers.respond_to?(:new)
  Timers.new
else
  Timers::Group.new
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout, error, &block) ⇒ Timer

Returns a new instance of Timer.



12
13
14
15
16
17
# File 'lib/rest-core/timer.rb', line 12

def initialize timeout, error, &block
  self.timeout = timeout
  self.error   = error
  self.block   = block
  start
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



11
12
13
# File 'lib/rest-core/timer.rb', line 11

def error
  @error
end

#timeoutObject

Returns the value of attribute timeout.



11
12
13
# File 'lib/rest-core/timer.rb', line 11

def timeout
  @timeout
end

Instance Method Details

#cancelObject



23
24
25
# File 'lib/rest-core/timer.rb', line 23

def cancel
  timer.cancel
end

#on_timeout(&block) ⇒ Object



19
20
21
# File 'lib/rest-core/timer.rb', line 19

def on_timeout &block
  self.block = block
end

#startObject



27
28
29
30
# File 'lib/rest-core/timer.rb', line 27

def start
  return if timeout.nil? || timeout.zero?
  self.timer = TimerGen.after(timeout){ block.call }
end