Class: TimeQueue
- Inherits:
-
Object
- Object
- TimeQueue
- Defined in:
- lib/argus/time_queue.rb
Defined Under Namespace
Classes: Item
Instance Method Summary collapse
- #add(time, value) ⇒ Object
- #all_ready(time) ⇒ Object
- #any_ready?(time) ⇒ Boolean
- #each_ready(time) ⇒ Object
-
#initialize ⇒ TimeQueue
constructor
A new instance of TimeQueue.
- #remove(key) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ TimeQueue
Returns a new instance of TimeQueue.
20 21 22 |
# File 'lib/argus/time_queue.rb', line 20 def initialize @items = [] end |
Instance Method Details
#add(time, value) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/argus/time_queue.rb', line 24 def add(time, value) item = Item.new(time, value) @items << item @items.sort! item.key end |
#all_ready(time) ⇒ Object
39 40 41 42 43 |
# File 'lib/argus/time_queue.rb', line 39 def all_ready(time) result = [] each_ready(time) do |value| result << value end result end |
#any_ready?(time) ⇒ Boolean
35 36 37 |
# File 'lib/argus/time_queue.rb', line 35 def any_ready?(time) ! @items.empty? && @items.first.time <= time end |
#each_ready(time) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/argus/time_queue.rb', line 45 def each_ready(time) while any_ready?(time) item = @items.shift yield item.value end end |
#remove(key) ⇒ Object
31 32 33 |
# File 'lib/argus/time_queue.rb', line 31 def remove(key) @items.delete_if { |item| item.key == key } end |
#to_s ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/argus/time_queue.rb', line 52 def to_s base_time = nil strings = @items[0,3].map { |item| string = item.to_s(base_time) base_time ||= item.time string } strings << "..." if @items.size > 3 "[#{strings.join(', ')}]" end |