Class: Commande::Chain

Inherits:
Object
  • Object
show all
Includes:
Commande
Defined in:
lib/commande/chain.rb

Defined Under Namespace

Classes: InitialCommandResult

Constant Summary

Constants included from Commande

VERSION

Instance Method Summary collapse

Methods included from Commande

included

Constructor Details

#initialize(*commands) ⇒ Chain

Returns a new instance of Chain.



9
10
11
# File 'lib/commande/chain.rb', line 9

def initialize(*commands)
  self.commands = commands
end

Instance Method Details

#call(**args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/commande/chain.rb', line 23

def call(**args)
  self.chain_result = commands.inject(InitialCommandResult.new(args)) do |last_result, current_command|
    current_result = current_command.call(**last_result.payload.dup)

    transfer_logs current_result
    transfer_errors current_result

    fail! unless transfer_success? current_result
    current_result
  end
end

#chain(*commands) ⇒ Object



13
14
15
# File 'lib/commande/chain.rb', line 13

def chain(*commands)
  new(*self.commands, *commands)
end

#valid(**_args) ⇒ Object



17
18
19
20
21
# File 'lib/commande/chain.rb', line 17

def valid(**_args)
  error! 'needs at least one command' if commands.empty?

  true
end