Class: QObserver
- Inherits:
-
Object
- Object
- QObserver
- Defined in:
- lib/xmpp4r/observable/observable_thing.rb
Overview
QObserver - simple observer-to-queue class
Instance Method Summary (collapse)
-
- (QObserver) initialize
constructor
A new instance of QObserver.
- - (Object) inspect
-
- (Object) queues
Return the queues we have registered.
-
- (Object) received(q)
Get the contents of the queue in an array (or pass each item to the given block.
q
queue.
-
- (Boolean) received?(q)
Received something in this queue?.
-
- (Object) size(q)
Get the size of a given queue.
-
- (Object) update(thing, *args)
update method for our Observer.
Constructor Details
- (QObserver) initialize
A new instance of QObserver
134 135 136 |
# File 'lib/xmpp4r/observable/observable_thing.rb', line 134 def initialize @queues = Hash.new end |
Instance Method Details
- (Object) inspect
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 |
- (Object) queues
Return the queues we have registered
151 152 153 |
# File 'lib/xmpp4r/observable/observable_thing.rb', line 151 def queues @queues.keys end |
- (Object) received(q)
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 |
- (Boolean) received?(q)
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 |
- (Object) size(q)
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 |
- (Object) update(thing, *args)
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 |