Class: EventQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/delve/event_queue.rb

Instance Method Summary collapse

Constructor Details

#initializeEventQueue

Returns a new instance of EventQueue.



2
3
4
5
# File 'lib/delve/event_queue.rb', line 2

def initialize
  @time = 0
  @events = Array.new
end

Instance Method Details

#add(event, time) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/delve/event_queue.rb', line 11

def add(event, time)
  raise 'Unable to add a nil event' unless event
  raise 'Unable to schedule event with no time' unless time
  
  i = @events.length
  (0..@events.length - 1).each do |e|
    if @events[e][:time] > time
      i = e
      break
    end
  end

  @events.insert(i, { :event => event, :time => time })
end

#clearObject



45
46
47
# File 'lib/delve/event_queue.rb', line 45

def clear
  @events = Array.new
end

#getObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/delve/event_queue.rb', line 26

def get
  return nil unless @events.length > 0
  
  e = @events.shift

  if e[:time] > 0
    @time += e[:time]
    @events.each { |x| x[:time] -= e[:time] }
  end
  e[:event]
end

#remove(event) ⇒ Object



38
39
40
41
42
43
# File 'lib/delve/event_queue.rb', line 38

def remove(event)
  index = @events.index { |e| e[:event] == event }
  return false unless index
  @events.delete_at index
  true
end

#timeObject



7
8
9
# File 'lib/delve/event_queue.rb', line 7

def time
  @time
end