Class: Parenting::Chore
- Inherits:
-
Object
- Object
- Parenting::Chore
- Defined in:
- lib/parenting/chore.rb
Instance Attribute Summary collapse
-
#command ⇒ Object
Returns the value of attribute command.
-
#completed ⇒ Object
Returns the value of attribute completed.
-
#cost ⇒ Object
Returns the value of attribute cost.
-
#deps ⇒ Object
Returns the value of attribute deps.
-
#exit_status ⇒ Object
Returns the value of attribute exit_status.
-
#name ⇒ Object
Returns the value of attribute name.
-
#on_failure ⇒ Object
Returns the value of attribute on_failure.
-
#on_stderr ⇒ Object
Returns the value of attribute on_stderr.
-
#on_success ⇒ Object
Returns the value of attribute on_success.
-
#result ⇒ Object
Returns the value of attribute result.
-
#stderr ⇒ Object
Returns the value of attribute stderr.
-
#stdin ⇒ Object
Returns the value of attribute stdin.
-
#stdout ⇒ Object
Returns the value of attribute stdout.
-
#thread ⇒ Object
Returns the value of attribute thread.
Instance Method Summary collapse
- #complete? ⇒ Boolean
- #done_with(name) ⇒ Object
- #failed? ⇒ Boolean
- #handle_completion ⇒ Object
-
#initialize(opts) ⇒ Chore
constructor
A new instance of Chore.
- #run! ⇒ Object
- #satisfied?(completed) ⇒ Boolean
Constructor Details
#initialize(opts) ⇒ Chore
Returns a new instance of Chore.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/parenting/chore.rb', line 8 def initialize(opts) [:on_success, :on_failure, :on_stderr].each do |cb| self.send :"#{cb}=", opts.fetch(cb).dup end self.name = opts[:name] || nil self.deps = opts[:deps] || [] self.completed = Queue.new self.command = opts.fetch(:command).dup self.command = [self.command] unless self.command.is_a? Array self.cost = opts[:cost] || nil self.stdin = opts[:stdin] || nil self.stdout = nil self.stderr = Queue.new self.result = :working end |
Instance Attribute Details
#command ⇒ Object
Returns the value of attribute command.
4 5 6 |
# File 'lib/parenting/chore.rb', line 4 def command @command end |
#completed ⇒ Object
Returns the value of attribute completed.
6 7 8 |
# File 'lib/parenting/chore.rb', line 6 def completed @completed end |
#cost ⇒ Object
Returns the value of attribute cost.
5 6 7 |
# File 'lib/parenting/chore.rb', line 5 def cost @cost end |
#deps ⇒ Object
Returns the value of attribute deps.
6 7 8 |
# File 'lib/parenting/chore.rb', line 6 def deps @deps end |
#exit_status ⇒ Object
Returns the value of attribute exit_status.
3 4 5 |
# File 'lib/parenting/chore.rb', line 3 def exit_status @exit_status end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/parenting/chore.rb', line 6 def name @name end |
#on_failure ⇒ Object
Returns the value of attribute on_failure.
3 4 5 |
# File 'lib/parenting/chore.rb', line 3 def on_failure @on_failure end |
#on_stderr ⇒ Object
Returns the value of attribute on_stderr.
3 4 5 |
# File 'lib/parenting/chore.rb', line 3 def on_stderr @on_stderr end |
#on_success ⇒ Object
Returns the value of attribute on_success.
3 4 5 |
# File 'lib/parenting/chore.rb', line 3 def on_success @on_success end |
#result ⇒ Object
Returns the value of attribute result.
5 6 7 |
# File 'lib/parenting/chore.rb', line 5 def result @result end |
#stderr ⇒ Object
Returns the value of attribute stderr.
4 5 6 |
# File 'lib/parenting/chore.rb', line 4 def stderr @stderr end |
#stdin ⇒ Object
Returns the value of attribute stdin.
4 5 6 |
# File 'lib/parenting/chore.rb', line 4 def stdin @stdin end |
#stdout ⇒ Object
Returns the value of attribute stdout.
4 5 6 |
# File 'lib/parenting/chore.rb', line 4 def stdout @stdout end |
#thread ⇒ Object
Returns the value of attribute thread.
5 6 7 |
# File 'lib/parenting/chore.rb', line 5 def thread @thread end |
Instance Method Details
#complete? ⇒ Boolean
56 57 58 |
# File 'lib/parenting/chore.rb', line 56 def complete? self.result == :success || self.result == :failure end |
#done_with(name) ⇒ Object
60 61 62 |
# File 'lib/parenting/chore.rb', line 60 def done_with(name) self.completed << name end |
#failed? ⇒ Boolean
74 75 76 |
# File 'lib/parenting/chore.rb', line 74 def failed? self.result == :failure end |
#handle_completion ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/parenting/chore.rb', line 64 def handle_completion if self.result == :success self.on_success.call(self) elsif self.result == :failure self.on_failure.call(self) else raise "This should not happen" end end |
#run! ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/parenting/chore.rb', line 30 def run! self.thread = Thread.new do cmd = [self.command].flatten Open3.popen3(* cmd) do |i, o, e, t| i.write(self.stdin); i.close e.each_line do |line| self.stderr << line end e.close self.stdout = o.read o.close result = t.value self.exit_status = result.exitstatus if result.success? self.result = :success else self.result = :failure end end end end |
#satisfied?(completed) ⇒ Boolean
26 27 28 |
# File 'lib/parenting/chore.rb', line 26 def satisfied?(completed) self.deps.empty? || self.deps.all?{|d| completed.include?(d)} end |