Class: Packet::PeriodicEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/packet/packet_periodic_event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interval, &block) ⇒ PeriodicEvent

Returns a new instance of PeriodicEvent.



4
5
6
7
8
9
10
# File 'lib/packet/packet_periodic_event.rb', line 4

def initialize(interval, &block)
  @cancel_flag = false
  @timer_signature = Guid.hexdigest
  @block = block
  @scheduled_time = Time.now + interval
  @interval = interval
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



3
4
5
# File 'lib/packet/packet_periodic_event.rb', line 3

def block
  @block
end

#cancel_flagObject

Returns the value of attribute cancel_flag.



3
4
5
# File 'lib/packet/packet_periodic_event.rb', line 3

def cancel_flag
  @cancel_flag
end

#intervalObject

Returns the value of attribute interval.



3
4
5
# File 'lib/packet/packet_periodic_event.rb', line 3

def interval
  @interval
end

#timer_signatureObject

Returns the value of attribute timer_signature.



3
4
5
# File 'lib/packet/packet_periodic_event.rb', line 3

def timer_signature
  @timer_signature
end

Instance Method Details

#cancelObject



17
18
19
# File 'lib/packet/packet_periodic_event.rb', line 17

def cancel
  @cancel_flag = true
end

#runObject



21
22
23
24
# File 'lib/packet/packet_periodic_event.rb', line 21

def run
  @scheduled_time += @interval
  @block.call
end

#run_now?Boolean

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/packet/packet_periodic_event.rb', line 12

def run_now?
  return true if @scheduled_time <= Time.now
  return false
end