Class: Parenting::Chore

Inherits:
Object
  • Object
show all
Defined in:
lib/parenting/chore.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#commandObject

Returns the value of attribute command.



4
5
6
# File 'lib/parenting/chore.rb', line 4

def command
  @command
end

#completedObject

Returns the value of attribute completed.



6
7
8
# File 'lib/parenting/chore.rb', line 6

def completed
  @completed
end

#costObject

Returns the value of attribute cost.



5
6
7
# File 'lib/parenting/chore.rb', line 5

def cost
  @cost
end

#depsObject

Returns the value of attribute deps.



6
7
8
# File 'lib/parenting/chore.rb', line 6

def deps
  @deps
end

#exit_statusObject

Returns the value of attribute exit_status.



3
4
5
# File 'lib/parenting/chore.rb', line 3

def exit_status
  @exit_status
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/parenting/chore.rb', line 6

def name
  @name
end

#on_failureObject

Returns the value of attribute on_failure.



3
4
5
# File 'lib/parenting/chore.rb', line 3

def on_failure
  @on_failure
end

#on_stderrObject

Returns the value of attribute on_stderr.



3
4
5
# File 'lib/parenting/chore.rb', line 3

def on_stderr
  @on_stderr
end

#on_successObject

Returns the value of attribute on_success.



3
4
5
# File 'lib/parenting/chore.rb', line 3

def on_success
  @on_success
end

#resultObject

Returns the value of attribute result.



5
6
7
# File 'lib/parenting/chore.rb', line 5

def result
  @result
end

#stderrObject

Returns the value of attribute stderr.



4
5
6
# File 'lib/parenting/chore.rb', line 4

def stderr
  @stderr
end

#stdinObject

Returns the value of attribute stdin.



4
5
6
# File 'lib/parenting/chore.rb', line 4

def stdin
  @stdin
end

#stdoutObject

Returns the value of attribute stdout.



4
5
6
# File 'lib/parenting/chore.rb', line 4

def stdout
  @stdout
end

#threadObject

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

Returns:

  • (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

Returns:

  • (Boolean)


74
75
76
# File 'lib/parenting/chore.rb', line 74

def failed?
  self.result == :failure
end

#handle_completionObject



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

Returns:

  • (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