Class: UnifiedQueues::Multi::Driver::UnifiedQueuesDriver::Single
- Inherits:
-
UnifiedQueues::Multi::Driver
- Object
- UnifiedQueues::Multi::Driver
- UnifiedQueues::Multi::Driver::UnifiedQueuesDriver::Single
- Defined in:
- lib/unified-queues/multi/driver/unified-queues.rb
Overview
Multi queue driver for unified queues single queue interface.
Instance Attribute Summary
Attributes inherited from UnifiedQueues::Multi::Driver
Instance Method Summary collapse
-
#create(name, cls = @class) ⇒ Queue
Creates new queue under given name.
-
#get(name) ⇒ Object
(also: #[])
Returns named queue from instance.
-
#initialize(cls, *args, &block) ⇒ Single
constructor
Constructor.
-
#insert(name, queue) ⇒ Object
(also: #[]=)
Inserts queue instance to queues.
-
#list ⇒ Array
Lists names of all available queues.
-
#list_subscribed ⇒ Array
Lists all subscribed queues.
-
#list_used ⇒ Array
Lists all used queues.
-
#pop(blocking = false, &block) ⇒ Object|nil
Pops value from the queue.
-
#push(value, key = value, &block) ⇒ Object
Pushes value to the currently used queue.
-
#subscribe(name, &block) ⇒ Object
Subscribes to the queue.
-
#subscribed {|@subscribed.second| ... } ⇒ Queue
Currently subscribed queue.
-
#unsubscribe(name, &block) ⇒ Object
Unsubscribes from the queue.
-
#use(name, &block) ⇒ Object
Sets queue with given name as used.
-
#used {|@used.second| ... } ⇒ Queue
Currently used queue.
Methods inherited from UnifiedQueues::Multi::Driver
Constructor Details
#initialize(cls, *args, &block) ⇒ Single
Constructor.
70 71 72 73 |
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 70 def initialize(cls, *args, &block) @queues = { } @class = [cls, args, block] end |
Instance Method Details
#create(name, cls = @class) ⇒ Queue
Creates new queue under given name.
105 106 107 108 109 110 111 |
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 105 def create(name, cls = @class) if not name.in? @queues self[name] = cls.first::new(*cls.second, &cls.third) else self[name] end end |
#get(name) ⇒ Object Also known as: []
Returns named queue from instance.
131 132 133 |
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 131 def get(name) @queues[name] end |
#insert(name, queue) ⇒ Object Also known as: []=
Inserts queue instance to queues.
120 121 122 |
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 120 def insert(name, queue) @queues[name] = queue end |
#list ⇒ Array
Lists names of all available queues.
201 202 203 |
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 201 def list @queues.keys end |
#list_subscribed ⇒ Array
Lists all subscribed queues.
219 220 221 |
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 219 def list_subscribed [@subscribed.first] end |
#list_used ⇒ Array
Lists all used queues.
210 211 212 |
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 210 def list_used [@used.first] end |
#pop(blocking = false, &block) ⇒ Object|nil
Pops value from the queue. In contrast to default Queue library, blocks or returns nil
if empty.
94 95 96 |
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 94 def pop(blocking = false, &block) self.subscribed.pop(blocking, &block) end |
#push(value, key = value, &block) ⇒ Object
Pushes value to the currently used queue.
82 83 84 |
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 82 def push(value, key = value, &block) self.used.push(value, key, &block) end |
#subscribe(name, &block) ⇒ Object
Subscribes to the queue. So marks it as target for #pop. Note, than only single queue can be subscribed at one time.
157 158 159 160 161 |
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 157 def subscribe(name, &block) self.create(name) @subscribed = [name, self[name]] yield if not block.nil? end |
#subscribed {|@subscribed.second| ... } ⇒ Queue
Currently subscribed queue.
191 192 193 194 |
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 191 def subscribed(&block) yield @subscribed.second if not block.nil? return @subscribed.second end |
#unsubscribe(name, &block) ⇒ Object
Unsubscribes from the queue.
168 169 170 171 172 173 174 |
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 168 def unsubscribe(name, &block) if not @subscribed.nil? and (@subscribed.first == name) @subscribed = nil end yield if not block.nil? end |
#use(name, &block) ⇒ Object
Sets queue with given name as used. So marks it as target for #push.
144 145 146 147 148 |
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 144 def use(name, &block) self.create(name) @used = [name, self[name]] yield if not block.nil? end |
#used {|@used.second| ... } ⇒ Queue
Currently used queue.
181 182 183 184 |
# File 'lib/unified-queues/multi/driver/unified-queues.rb', line 181 def used(&block) yield @used.second if not block.nil? return @used.second end |