Class: Parenting::Boss
- Inherits:
-
Object
- Object
- Parenting::Boss
- Defined in:
- lib/parenting/boss.rb
Instance Attribute Summary collapse
-
#chores ⇒ Object
Returns the value of attribute chores.
-
#completed ⇒ Object
Returns the value of attribute completed.
-
#in_progress ⇒ Object
Returns the value of attribute in_progress.
-
#max_children ⇒ Object
Returns the value of attribute max_children.
Instance Method Summary collapse
- #add_chore(opts) ⇒ Object
- #assign_next_chore ⇒ Object
- #assignable_chores ⇒ Object
- #check_children ⇒ Object
- #done? ⇒ Boolean
- #free_children? ⇒ Boolean
- #handle_complaints ⇒ Object
-
#initialize(number_of_children) ⇒ Boss
constructor
A new instance of Boss.
- #run! ⇒ Object
Constructor Details
#initialize(number_of_children) ⇒ Boss
Returns a new instance of Boss.
7 8 9 10 11 12 |
# File 'lib/parenting/boss.rb', line 7 def initialize(number_of_children) self.max_children = number_of_children self.chores = [] self.in_progress = [] self.completed = Set.new end |
Instance Attribute Details
#chores ⇒ Object
Returns the value of attribute chores.
5 6 7 |
# File 'lib/parenting/boss.rb', line 5 def chores @chores end |
#completed ⇒ Object
Returns the value of attribute completed.
5 6 7 |
# File 'lib/parenting/boss.rb', line 5 def completed @completed end |
#in_progress ⇒ Object
Returns the value of attribute in_progress.
5 6 7 |
# File 'lib/parenting/boss.rb', line 5 def in_progress @in_progress end |
#max_children ⇒ Object
Returns the value of attribute max_children.
5 6 7 |
# File 'lib/parenting/boss.rb', line 5 def max_children @max_children end |
Instance Method Details
#add_chore(opts) ⇒ Object
14 15 16 |
# File 'lib/parenting/boss.rb', line 14 def add_chore(opts) self.chores << Parenting::Chore.new(opts) end |
#assign_next_chore ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/parenting/boss.rb', line 18 def assign_next_chore return unless self.assignable_chores.any? return if self.in_progress.length >= self.max_children next_location = self.chores.find_index{|c| c.satisfied? self.completed } next_chore = self.chores.delete_at next_location next_chore.run! self.in_progress << next_chore end |
#assignable_chores ⇒ Object
65 66 67 68 69 |
# File 'lib/parenting/boss.rb', line 65 def assignable_chores self.chores.select do |c| c.satisfied? self.completed end end |
#check_children ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/parenting/boss.rb', line 45 def check_children remaining = [] self.in_progress.each do |chore| if chore.complete? chore.handle_completion self.completed << chore.name if chore.name and not chore.failed? else remaining << chore end end self.in_progress = remaining self.in_progress.each do |chore| until chore.completed.empty? self.completed << chore.shift end end end |
#done? ⇒ Boolean
33 34 35 |
# File 'lib/parenting/boss.rb', line 33 def done? self.assignable_chores.empty? && self.in_progress.empty? end |
#free_children? ⇒ Boolean
29 30 31 |
# File 'lib/parenting/boss.rb', line 29 def free_children? self.in_progress.length < self.max_children end |
#handle_complaints ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/parenting/boss.rb', line 37 def handle_complaints self.in_progress.each do |chore| until chore.stderr.empty? chore.on_stderr.call(chore.stderr.shift) end end end |
#run! ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/parenting/boss.rb', line 71 def run! # get the chores into longest job first - this is to minimize net runtime self.chores = self.chores.sort_by{|c| - c.cost.to_f} # queue up the first set of chores while self.assignable_chores.any? and self.free_children? self.assign_next_chore end # watch the children, and assign new chores if any get free until self.done? sleep 0.05 self.handle_complaints self.check_children while self.free_children? and self.assignable_chores.any? self.assign_next_chore end end end |