Class: Climate::Command::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/climate/command.rb

Overview

Chain is a helper class for allowing parent commands to participate in command execution

Instance Method Summary collapse

Constructor Details

#initialize(commands) ⇒ Chain

Returns a new instance of Chain.



31
32
33
# File 'lib/climate/command.rb', line 31

def initialize(commands)
  @commands = commands
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/climate/command.rb', line 35

def empty?
  @commands.empty?
end

#runObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/climate/command.rb', line 39

def run
  raise "Can't call run on an empty command chain" if empty?

  next_command = @commands.shift

  begin
    if next_command.method(:run).arity == 1
      next_command.run(self)
    else
      result = next_command.run
      # we ignore the result from a run method with arity == 0 unless
      # it is the last command in the chain.  Really this is only a
      # convenience for testing, so we can assert the chain was followed
      # properly
      if empty?
        result
      else
        self.run
      end
    end
  rescue Climate::CommandError => e
    e.command_class = next_command.class if e.command_class.nil?
    raise
  end
end