Class: TimeQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/argus/time_queue.rb

Defined Under Namespace

Classes: Item

Instance Method Summary collapse

Constructor Details

#initializeTimeQueue

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

Returns:

  • (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_sObject


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