Class: Toolx::Core::Operation::Flow::Executor

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Rescuable
Defined in:
lib/toolx/core/operation/flow.rb

Constant Summary collapse

ExecutorError =
Class.new(::Toolx::Core::Errors::NestedStandardError)
OperationAlreadyInitialized =
Class.new(ExecutorError)
OperationNotInitialized =
Class.new(ExecutorError)
ActionException =
Class.new(::Toolx::Core::Errors::NestedStandardError)
OnSuccessException =
Class.new(ActionException)
OnFailException =
Class.new(ActionException) do
  attr_reader :operation_exception
  def initialize(msg = nil, nested = $ERROR_INFO, operation_exception = nil)
    super(msg, nested)
    @operation_exception = operation_exception
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation_class) ⇒ Executor

Returns a new instance of Executor.



34
35
36
37
# File 'lib/toolx/core/operation/flow.rb', line 34

def initialize(operation_class)
  @operation_class = operation_class
  self
end

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception.



39
40
41
# File 'lib/toolx/core/operation/flow.rb', line 39

def exception
  @exception
end

#fail_errorsObject (readonly)

Returns the value of attribute fail_errors.



39
40
41
# File 'lib/toolx/core/operation/flow.rb', line 39

def fail_errors
  @fail_errors
end

#operationObject (readonly)

Returns the value of attribute operation.



39
40
41
# File 'lib/toolx/core/operation/flow.rb', line 39

def operation
  @operation
end

#operation_classObject (readonly)

Returns the value of attribute operation_class.



39
40
41
# File 'lib/toolx/core/operation/flow.rb', line 39

def operation_class
  @operation_class
end

#responseObject (readonly)

Returns the value of attribute response.



39
40
41
# File 'lib/toolx/core/operation/flow.rb', line 39

def response
  @response
end

Instance Method Details

#actionsObject

Accessor for action handlers : () -> Hash[Symbol, Proc | Symbol]



164
165
166
# File 'lib/toolx/core/operation/flow.rb', line 164

def actions
  @actions ||= {}
end

#bind_with(bind_object) ⇒ Object

Assigns object to call symbol handlers on : (Object) -> Executor



142
143
144
145
# File 'lib/toolx/core/operation/flow.rb', line 142

def bind_with(bind_object)
  @bind_object = bind_object
  self
end

#clear!Object

Clears execution state (response, error, exception) : () -> Executor



93
94
95
96
97
98
99
# File 'lib/toolx/core/operation/flow.rb', line 93

def clear!
  @response = nil
  @exception = nil
  @fail_errors = nil
  @errors = nil
  self
end

#clear_operation!Object

Clears only the operation instance : () -> Executor



86
87
88
89
# File 'lib/toolx/core/operation/flow.rb', line 86

def clear_operation!
  @operation = nil
  self
end

#errorsObject

Returns errors from failure or exception : () -> untyped



80
81
82
# File 'lib/toolx/core/operation/flow.rb', line 80

def errors
  @errors ||= fail_errors || exception_error
end

#fail!(fail_obj = true) ⇒ Object

Forces failure with custom error payload : (untyped) -> Executor



156
157
158
159
160
# File 'lib/toolx/core/operation/flow.rb', line 156

def fail!(fail_obj = true)
  @errors = nil
  @fail_errors = fail_obj
  self
end

#fail?Boolean Also known as: failure?

Returns:

  • (Boolean)


73
# File 'lib/toolx/core/operation/flow.rb', line 73

def fail? = !success?

#new(**params) ⇒ Object

Initializes the operation instance with the given params Raises OperationAlreadyInitialized if called twice without reset : (**untyped) -> Executor



44
45
46
47
48
49
# File 'lib/toolx/core/operation/flow.rb', line 44

def new(**params)
  raise OperationAlreadyInitialized if operation

  @operation = operation_class.new(**params)
  self
end

#new!(**params) ⇒ Object

Forces reinitialization of the operation instance : (**untyped) -> Executor



53
54
55
56
# File 'lib/toolx/core/operation/flow.rb', line 53

def new!(**params)
  @operation = nil
  new(**params)
end

#on(actions_with_responses = {}) ⇒ Object

Binds success/fail handlers with method names : (success: Symbol?, fail: Symbol?) -> Executor Examples:

executor.on(success: :dashboard, fail: :show_error)
executor.on(success: -> () { render :done })
executor.on(success: -> (response) { render :done, locals: { model: response.subject } })
executor.on(success: -> (_response, operation) { render :done, locals: { model: operation.response.subject } })


112
113
114
# File 'lib/toolx/core/operation/flow.rb', line 112

def on(actions_with_responses = {})
  actions_assign(actions_with_responses, :success, :fail)
end

#on_fail(binded_method = nil, &block) ⇒ Object

Binds failure handler with method name or block : (Symbol?) { (Executor) -> void } -> Executor Examples: executor.on_fail(:show_error) executor.on_fail { render :error } executor.on_fail { |response| render :error, locals: { error: response.errors } } executor.on_fail { |_response, operation| render :error, locals: { error: operation.errors } }



135
136
137
138
# File 'lib/toolx/core/operation/flow.rb', line 135

def on_fail(binded_method = nil, &block)
  actions[:fail] = binded_method || block
  self
end

#on_success(binded_method = nil, &block) ⇒ Object

Binds success handler with method name or block : (Symbol?) { (Executor) -> void } -> Executor Examples: executor.on_success(:dashboard) executor.on_success { render :done } executor.on_success { |response| render :done, locals: { model: response.subject } } executor.on_success { |_response, operation| render :done, locals: { model: operation.response.subject } }



123
124
125
126
# File 'lib/toolx/core/operation/flow.rb', line 123

def on_success(binded_method = nil, &block)
  actions[:success] = binded_method || block
  self
end

#perform(**params) ⇒ Object

Runs the operation (initialize if params provided) : (**untyped) -> Executor



60
61
62
63
# File 'lib/toolx/core/operation/flow.rb', line 60

def perform(**params)
  new(**params) if params.present?
  execute
end

#perform!(**params) ⇒ Object

Forces execution (reset and run) : (**untyped) -> Executor



67
68
69
70
71
# File 'lib/toolx/core/operation/flow.rb', line 67

def perform!(**params)
  clear_operation!.new(**params) if params.present?
  clear!
  execute
end

#reset!Object

Clears everything (operation and state) : () -> Executor



103
# File 'lib/toolx/core/operation/flow.rb', line 103

def reset! = clear_operation!.clear!

#success?Boolean Also known as: ok?

Returns:

  • (Boolean)


74
# File 'lib/toolx/core/operation/flow.rb', line 74

def success? = errors.respond_to?(:empty?) ? errors.empty? : !errors

#unbind!Object

Removes bound handler object : () -> Executor



149
150
151
152
# File 'lib/toolx/core/operation/flow.rb', line 149

def unbind!
  @bind_object = nil
  self
end