Class: EventQueue
- Inherits:
-
Object
- Object
- EventQueue
- Defined in:
- lib/delve/event_queue.rb
Instance Method Summary collapse
- #add(event, time) ⇒ Object
- #clear ⇒ Object
- #get ⇒ Object
-
#initialize ⇒ EventQueue
constructor
A new instance of EventQueue.
- #remove(event) ⇒ Object
- #time ⇒ Object
Constructor Details
#initialize ⇒ EventQueue
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 |
#clear ⇒ Object
45 46 47 |
# File 'lib/delve/event_queue.rb', line 45 def clear @events = Array.new end |
#get ⇒ Object
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 |
#time ⇒ Object
7 8 9 |
# File 'lib/delve/event_queue.rb', line 7 def time @time end |