Module: Kommando::CommandPlugins::Execute::ClassMethods

Defined in:
lib/kommando/command_plugins/execute.rb

Instance Method Summary collapse

Instance Method Details

#_after_execute(context) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/kommando/command_plugins/execute.rb', line 43

def _after_execute(context)
  case context[:execute_return_value]
  when CommandResult::Failure
    context.merge(halt: context[:execute_return_value])
  else
    context
  end
end

#_before_execute(context) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/kommando/command_plugins/execute.rb', line 29

def _before_execute(context)
  unless context[:parameters].key?(:command_id)
    context[:parameters] = context[:parameters].merge({ command_id: SecureRandom.uuid })
  end

  context
end

#_execute(context) ⇒ Object



37
38
39
40
41
# File 'lib/kommando/command_plugins/execute.rb', line 37

def _execute(context)
  context.merge(execute_return_value: context[:instance].handle(context[:dependencies], context[:parameters]))
rescue StandardError => error
  context.merge(halt: { error: :unhandled_exception, data: error })
end

#_execute_context_to_data(data, context) ⇒ Object



65
66
67
# File 'lib/kommando/command_plugins/execute.rb', line 65

def _execute_context_to_data(data, context)
  data.merge(context.slice(:parameters)).merge(command: name)
end

#_execute_context_to_result(context) ⇒ Object



52
53
54
# File 'lib/kommando/command_plugins/execute.rb', line 52

def _execute_context_to_result(context)
  CommandResult.success(_execute_context_to_data({}, context))
end

#_halt_with_failure(context) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/kommando/command_plugins/execute.rb', line 56

def _halt_with_failure(context)
  case context[:halt]
  when CommandResult::Failure
    context[:halt]
  else
    CommandResult.failure(_execute_context_to_data({}, context).merge({ error: context[:halt][:error], details: context[:halt][:data] }))
  end
end

#execute(dependencies, parameters) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/kommando/command_plugins/execute.rb', line 10

def execute(dependencies, parameters)
  context = {
    dependencies: dependencies,
    parameters: parameters,
    instance: new,
  }

  context = _before_execute(context)
  return _halt_with_failure(context) if context.key?(:halt)

  context = _execute(context)
  return _halt_with_failure(context) if context.key?(:halt)

  context = _after_execute(context)
  return _halt_with_failure(context) if context.key?(:halt)

  _execute_context_to_result(context)
end