Class: QObserver

Inherits:
Object
  • Object
show all
Defined in:
lib/xmpp4r/observable/observable_thing.rb

Overview

QObserver - simple observer-to-queue class

Instance Method Summary collapse

Constructor Details

#initializeQObserver



134
135
136
# File 'lib/xmpp4r/observable/observable_thing.rb', line 134

def initialize
  @queues = Hash.new
end

Instance Method Details

#inspectObject



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/xmpp4r/observable/observable_thing.rb', line 138

def inspect
  h = {}
  @queues.each do |q, queue|
    h[q] = queue.size
  end
  if h.length > 0
    sprintf("#<%s:0x%x size=%s>", self.class.name, __id__, h.inspect)
  else
    sprintf("#<%s:0x%x>", self.class.name, __id__)
  end
end

#queuesObject

Return the queues we have registered



151
152
153
# File 'lib/xmpp4r/observable/observable_thing.rb', line 151

def queues
  @queues.keys
end

#received(q) ⇒ Object

Get the contents of the queue in an array (or pass each item to the given block.

q

queue



166
167
168
169
170
171
172
173
174
175
# File 'lib/xmpp4r/observable/observable_thing.rb', line 166

def received(q)
  return nil if ! @queues.include?(q)
  if block_given?
    yield @queues[q].deq while ! @queues[q].empty?
  else
    a = []
    a << @queues[q].deq while ! @queues[q].empty?
    return a
  end
end

#received?(q) ⇒ Boolean

Received something in this queue?

q

queue



158
159
160
# File 'lib/xmpp4r/observable/observable_thing.rb', line 158

def received?(q)
  @queues.include?(q) and ! @queues[q].empty?
end

#size(q) ⇒ Object

Get the size of a given queue

q

queue



180
181
182
# File 'lib/xmpp4r/observable/observable_thing.rb', line 180

def size(q)
  @queues[q].size rescue 0
end

#update(thing, *args) ⇒ Object

update method for our Observer

thing

what to be updated



187
188
189
190
# File 'lib/xmpp4r/observable/observable_thing.rb', line 187

def update(thing, *args)
  @queues[thing] = Queue.new if ! @queues.include?(thing)
  @queues[thing].enq args
end