Class: TaskBag::Bag

Inherits:
Object
  • Object
show all
Defined in:
lib/taskbag/bag.rb

Instance Method Summary collapse

Constructor Details

#initialize(task_class) ⇒ Bag



3
4
5
6
# File 'lib/taskbag/bag.rb', line 3

def initialize(task_class)
  @tasks = []
  @task_class = task_class
end

Instance Method Details

#add(business) ⇒ Object



16
17
18
# File 'lib/taskbag/bag.rb', line 16

def add(business)
  @tasks << business
end

#closeObject



20
21
22
23
24
# File 'lib/taskbag/bag.rb', line 20

def close
  loop { break unless @tasks.any? }
  @closed = true
  @threads.each{|t| t.join}
end

#closed?Boolean



26
27
28
# File 'lib/taskbag/bag.rb', line 26

def closed?
  !!@closed
end

#nextObject



30
31
32
# File 'lib/taskbag/bag.rb', line 30

def next
  @tasks.pop
end

#open(nthreads) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/taskbag/bag.rb', line 8

def open(nthreads)
  @closed = false
  bag = self
  @threads = nthreads.times.map do |w|
    Thread.new { @task_class.new(bag).start }
  end
end