Class: Binpacker::WorkerQueue

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/binpacker/worker_queue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker_id, tests = []) ⇒ WorkerQueue

Returns a new instance of WorkerQueue.



9
10
11
12
13
# File 'lib/binpacker/worker_queue.rb', line 9

def initialize(worker_id, tests = [])
  @worker_id = worker_id
  @tests = tests.dup
  @index = 0
end

Instance Attribute Details

#worker_idObject (readonly)

Returns the value of attribute worker_id.



7
8
9
# File 'lib/binpacker/worker_queue.rb', line 7

def worker_id
  @worker_id
end

Instance Method Details

#each(&block) ⇒ Object



47
48
49
# File 'lib/binpacker/worker_queue.rb', line 47

def each(&block)
  @tests[@index..].each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/binpacker/worker_queue.rb', line 27

def empty?
  @index >= @tests.size
end

#peekObject



22
23
24
25
# File 'lib/binpacker/worker_queue.rb', line 22

def peek
  return nil if empty?
  @tests[@index]
end

#popObject



15
16
17
18
19
20
# File 'lib/binpacker/worker_queue.rb', line 15

def pop
  return nil if empty?
  test = @tests[@index]
  @index += 1
  test
end

#push(test) ⇒ Object



43
44
45
# File 'lib/binpacker/worker_queue.rb', line 43

def push(test)
  @tests << test
end

#remainingObject



35
36
37
# File 'lib/binpacker/worker_queue.rb', line 35

def remaining
  @tests[@index..] || []
end

#sizeObject



31
32
33
# File 'lib/binpacker/worker_queue.rb', line 31

def size
  @tests.size - @index
end

#total_weight(timings) ⇒ Object



39
40
41
# File 'lib/binpacker/worker_queue.rb', line 39

def total_weight(timings)
  remaining.sum { |t| timings.fetch(t.key, Timing::DEFAULT_WEIGHT) }
end