Class: Tap::Support::ExecutableQueue
- Includes:
- MonitorMixin
- Defined in:
- lib/tap/support/executable_queue.rb
Overview
ExecutableQueue allows thread-safe enqueing and dequeing of Executable methods and inputs for execution.
Instance Method Summary collapse
-
#clear ⇒ Object
Clears all methods and inputs.
- #concat(array) ⇒ Object
-
#deq ⇒ Object
Dequeues the next method and inputs as an array like [method, inputs].
-
#empty? ⇒ Boolean
True if no methods are enqueued.
-
#enq(method, inputs) ⇒ Object
Enqueues the method and inputs.
-
#initialize ⇒ ExecutableQueue
constructor
Creates a new ExecutableQueue.
-
#size ⇒ Object
Returns the number of enqueued methods.
-
#to_a ⇒ Object
Converts self to an array.
-
#unshift(method, inputs) ⇒ Object
Enqueues the method and inputs, but to the top of the queue.
Constructor Details
#initialize ⇒ ExecutableQueue
Creates a new ExecutableQueue
10 11 12 13 14 |
# File 'lib/tap/support/executable_queue.rb', line 10 def initialize # required for MonitorMixin super() @queue = [] end |
Instance Method Details
#clear ⇒ Object
Clears all methods and inputs. Returns the existing queue as an array.
17 18 19 20 21 22 23 |
# File 'lib/tap/support/executable_queue.rb', line 17 def clear synchronize do current = self.queue self.queue = [] current end end |
#concat(array) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/tap/support/executable_queue.rb', line 59 def concat(array) synchronize do array.each do |method, inputs| enq(method, inputs) end end end |
#deq ⇒ Object
Dequeues the next method and inputs as an array like [method, inputs]. Returns nil if the queue is empty.
55 56 57 |
# File 'lib/tap/support/executable_queue.rb', line 55 def deq synchronize { queue.shift } end |
#empty? ⇒ Boolean
True if no methods are enqueued
31 32 33 |
# File 'lib/tap/support/executable_queue.rb', line 31 def empty? queue.empty? end |
#enq(method, inputs) ⇒ Object
Enqueues the method and inputs. Raises an error if the
method is not an Executable.
37 38 39 40 41 42 |
# File 'lib/tap/support/executable_queue.rb', line 37 def enq(method, inputs) synchronize do check_method(method) queue.push [method, inputs] end end |
#size ⇒ Object
Returns the number of enqueued methods
26 27 28 |
# File 'lib/tap/support/executable_queue.rb', line 26 def size queue.length end |
#to_a ⇒ Object
Converts self to an array.
68 69 70 |
# File 'lib/tap/support/executable_queue.rb', line 68 def to_a queue.dup end |
#unshift(method, inputs) ⇒ Object
Enqueues the method and inputs, but to the top of the queue. Raises an error if the method is not an Executable.
46 47 48 49 50 51 |
# File 'lib/tap/support/executable_queue.rb', line 46 def unshift(method, inputs) synchronize do check_method(method) queue.unshift [method, inputs] end end |