Class: Automate::Chain

Inherits:
Object
  • Object
show all
Includes:
Messenger
Defined in:
lib/automate/chain.rb

Overview

Captures the entire command chain to be executed. Thus, the main objective of this class is to capture and execute the ‘ChainLinks`. All the capturing happens in the `go` method, and the executing in the `run` method.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Messenger

#fail, #format, #msg, #notice, #success

Class Method Details

.which(task, &block) ⇒ Object

Factory method that creates a new command chain.



12
13
14
15
16
# File 'lib/automate/chain.rb', line 12

def self.which(task, &block)
  c = new(task)
  c.instance_exec(&block)
  c
end

Instance Method Details

#go(desc, &block) ⇒ Object

Add a new link to the command chain



19
20
21
# File 'lib/automate/chain.rb', line 19

def go(desc, &block)
  add_command(desc, block)
end

#run(args = {}) ⇒ Object

Run all the command chain links. Will abort the command chain if any chain link should fail.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/automate/chain.rb', line 26

def run(args = {})
  notice "About to run command chain which '#{@task}'"

  @cmd_list.each_with_index do |cmd, index|
    desc, proc = cmd
    success "#{timestamp} // Running link ##{index+1} - #{desc}"

    error = false
    begin
      ret, out = ChainLink.invoke(proc, args)
    rescue ChainFailedError, ChainLinkFailedError => e
      error = true
    end
    raise ChainFailedError.new(index + 1, desc) if ret == false || error == true


    # Pass the arguments from the last iteration, overwriting everything that
    # was passed from the current one.
    args.merge! out
  end

  args

rescue ChainFailedError => e
  fail("Chain link ##{e.chain} (#{e.description}) failed. Aborting.")
  false
end