Class: QObserver
- Inherits:
-
Object
- Object
- QObserver
- Defined in:
- lib/observable_thing.rb
Overview
QObserver - simple observer-to-queue class
Instance Method Summary collapse
-
#initialize ⇒ QObserver
constructor
A new instance of QObserver.
- #inspect ⇒ Object
-
#queues ⇒ Object
Return the queues we have registered.
-
#received(q) ⇒ Object
Get the contents of the queue in an array (or pass each item to the given block.
-
#received?(q) ⇒ Boolean
Received something in this queue?.
-
#size(q) ⇒ Object
Get the size of a given queue.
-
#update(thing, *args) ⇒ Object
update method for our Observer.
Constructor Details
#initialize ⇒ QObserver
Returns a new instance of QObserver.
130 131 132 |
# File 'lib/observable_thing.rb', line 130 def initialize @queues = Hash.new end |
Instance Method Details
#inspect ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/observable_thing.rb', line 134 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 |
#queues ⇒ Object
Return the queues we have registered
147 148 149 |
# File 'lib/observable_thing.rb', line 147 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
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/observable_thing.rb', line 162 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
154 155 156 |
# File 'lib/observable_thing.rb', line 154 def received?(q) @queues.include?(q) and ! @queues[q].empty? end |
#size(q) ⇒ Object
Get the size of a given queue
- q
-
queue
176 177 178 |
# File 'lib/observable_thing.rb', line 176 def size(q) @queues[q].size rescue 0 end |
#update(thing, *args) ⇒ Object
update method for our Observer
- thing
-
what to be updated
183 184 185 186 |
# File 'lib/observable_thing.rb', line 183 def update(thing, *args) @queues[thing] = Queue.new if ! @queues.include?(thing) @queues[thing].enq args end |