Class: Tap::Support::ExecutableQueue

Inherits:
Monitor
  • Object
show all
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

Constructor Details

#initializeExecutableQueue

Creates a new ExecutableQueue



11
12
13
14
# File 'lib/tap/support/executable_queue.rb', line 11

def initialize
  super
  @rounds = [[]]
end

Instance Method Details

#clearObject

Clears self and returns an array of the enqueued methods and inputs, organized by round.



18
19
20
21
22
23
# File 'lib/tap/support/executable_queue.rb', line 18

def clear
  synchronize do
    current, @rounds = @rounds, [[]]
    current
  end
end

#concat(round) ⇒ Object

Enques an array of [method, inputs] entries as a round. Rounds are dequeued completely before the next round is dequeued.



78
79
80
81
82
83
84
85
86
# File 'lib/tap/support/executable_queue.rb', line 78

def concat(round)
  synchronize do
    round.each do |method, inputs|
      check_method(method)
    end
    
    @rounds << round
  end
end

#deqObject

Dequeues the next method and inputs as an array like [method, inputs]. Returns nil if the queue is empty.



72
73
74
# File 'lib/tap/support/executable_queue.rb', line 72

def deq
  synchronize { queue.shift }
end

#empty?Boolean

True if no methods are enqueued

Returns:

  • (Boolean)


35
36
37
# File 'lib/tap/support/executable_queue.rb', line 35

def empty?
  synchronize { size == 0 }
end

#enq(method, inputs) ⇒ Object

Enqueues the method and inputs. Raises an error if the
method is not an Executable.



54
55
56
57
58
59
# File 'lib/tap/support/executable_queue.rb', line 54

def enq(method, inputs)
  synchronize do
    check_method(method)
    queue.push [method, inputs]
  end
end

#has?(entry) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tap/support/executable_queue.rb', line 39

def has?(entry)
  synchronize do
    entry_id = entry.object_id
    
    @rounds.each do |round|
      round.each do |enqued_entry|
        return true if entry_id == enqued_entry.object_id
      end
    end
    false
  end
end

#sizeObject

Returns the number of enqueued methods



26
27
28
29
30
31
32
# File 'lib/tap/support/executable_queue.rb', line 26

def size
  synchronize do
    size = 0 
    @rounds.each {|round| size += round.length }
    size
  end
end

#to_a(flatten = true) ⇒ Object

Converts self to an array. If flatten is specified, all rounds are concatenated into a single array.



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tap/support/executable_queue.rb', line 90

def to_a(flatten=true)
  synchronize do
    if flatten
      array = []
      @rounds.each {|round| array.concat(round) }
      array
    else
      @rounds.collect {|round| round.dup}
    end
  end
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.



63
64
65
66
67
68
# File 'lib/tap/support/executable_queue.rb', line 63

def unshift(method, inputs)
  synchronize do
    check_method(method)
    queue.unshift [method, inputs]
  end
end