Class: Rbkit::Timer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interval, &timer_block) ⇒ Timer

Returns a new instance of Timer.



5
6
7
8
9
# File 'lib/rbkit/timer.rb', line 5

def initialize(interval, &timer_block)
  @interval = interval
  @timer_block = timer_block
  @last_fired_at = Time.now
end

Instance Attribute Details

#intervalObject

Returns the value of attribute interval.



3
4
5
# File 'lib/rbkit/timer.rb', line 3

def interval
  @interval
end

#last_fired_atObject

Returns the value of attribute last_fired_at.



3
4
5
# File 'lib/rbkit/timer.rb', line 3

def last_fired_at
  @last_fired_at
end

Instance Method Details

#runObject



11
12
13
14
15
16
# File 'lib/rbkit/timer.rb', line 11

def run
  if Time.now - last_fired_at > interval
    @timer_block.call
    @last_fired_at = Time.now
  end
end