Class: SuperPoller::QueueIterator

Inherits:
Object
  • Object
show all
Defined in:
lib/super_poller/queue_iterator.rb

Instance Method Summary collapse

Constructor Details

#initialize(queue) ⇒ QueueIterator

Returns a new instance of QueueIterator.



2
3
4
# File 'lib/super_poller/queue_iterator.rb', line 2

def initialize(queue)
  @queue = queue
end

Instance Method Details

#destroy_the_memoObject



25
26
27
28
29
# File 'lib/super_poller/queue_iterator.rb', line 25

def destroy_the_memo
  while @memo and msg = @queue.fetch and msg != @memo
    @queue.push(msg)
  end
end

#each(&block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/super_poller/queue_iterator.rb', line 6

def each(&block)
  @memo = {:memo => Time.now.to_f}
  @queue.push @memo
  while @memo
    begin
      break unless msg = @queue.fetch
      if msg == @memo
        msg = @memo = nil
      else
        msg = nil if :delete == block.call(msg)
      end
    ensure
      @queue.push msg if msg
    end
  end
ensure
  destroy_the_memo
end