Module: Foobara::CommandPatternImplementation::Concerns::Runtime

Includes:
Foobara::Concern
Included in:
Foobara::CommandPatternImplementation
Defined in:
foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/runtime.rb

Defined Under Namespace

Modules: ClassMethods Classes: CannotHaltWithoutAddingErrors, Halt

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Foobara::Concern

foobara_class_methods_module_for, foobara_concern?, included

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception.



20
21
22
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/runtime.rb', line 20

def exception
  @exception
end

#outcomeObject (readonly)

Returns the value of attribute outcome.



20
21
22
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/runtime.rb', line 20

def outcome
  @outcome
end

Instance Method Details

#executeObject



74
75
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/runtime.rb', line 74

def execute
end

#halt!Object

Raises:



89
90
91
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/runtime.rb', line 89

def halt!
  raise Halt
end

#runObject



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
53
54
55
56
57
58
59
60
61
62
63
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/runtime.rb', line 26

def run
  Foobara::Namespace.use self.class do
    invoke_with_callbacks_and_transition(:open_transaction)

    invoke_with_callbacks_and_transition_in_transaction(%i[
                                                          cast_and_validate_inputs
                                                          load_records
                                                          validate_records
                                                          validate
                                                          run_execute
                                                          commit_transaction
                                                        ])

    invoke_with_callbacks_and_transition(:succeed)
  end

  @outcome
rescue Halt
  rollback_transaction

  return outcome if state_machine.currently_errored?

  if error_collection.empty?
    # :nocov:
    raise CannotHaltWithoutAddingErrors, "Cannot halt without adding errors first. " \
                                         "Either add errors or use error! transition instead."
    # :nocov:
  end

  state_machine.fail!

  @outcome = Outcome.errors(error_collection)
rescue => e
  @exception = e
  rollback_transaction
  state_machine.error!
  raise
end

#run!Object



22
23
24
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/runtime.rb', line 22

def run!
  run.result!
end

#run_executeObject



69
70
71
72
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/runtime.rb', line 69

def run_execute
  result = process_result_using_result_type(execute)
  @outcome = Outcome.success(result)
end

#succeedObject



77
78
79
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/runtime.rb', line 77

def succeed
  # noop but for now helpful for carrying out state transition
end

#success?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/runtime.rb', line 65

def success?
  outcome&.success?
end

#validateObject



85
86
87
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/runtime.rb', line 85

def validate
  # can override if desired, default is a no-op
end

#validate_recordsObject



81
82
83
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/runtime.rb', line 81

def validate_records
  # noop
end