Class: ApplicationOperation

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/templates/application_operation.rb

Overview

Base class for operation objects that provides a standard interface and error handling. Operation objects should inherit from this class and implement their own logic in the call method.

Example usage:

# Defining a successful operation
class MySuccessOperation < ApplicationOperation
def initialize(some_param, log_uuid: nil)
  super(log_uuid: log_uuid)
  @some_param = some_param
end

def call
  # Custom operation logic that succeeds
  success('Operation successful!', additional_info: 'Processed correctly')
end
end

# Defining a failing operation
class MyFailingOperation < ApplicationOperation
def initialize(some_param, log_uuid: nil)
  super(log_uuid: log_uuid)
  @some_param = some_param
end

def call
  # Custom operation logic that fails
  raise StandardError, 'Something went wrong!'
end
end

Success example:

result = MySuccessOperation.call(some_param)
=> [true, 'Operation successful!', additional_info: 'Processed correctly']

Failure example:

result = MyFailingOperation.call(some_param)
=> [false, #<StandardError: Something went wrong!>]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_uuid: nil) ⇒ ApplicationOperation

Initializes the operation object with an optional log_uuid. If no log_uuid is provided, a new UUID is generated automatically.

If not provided, a new UUID is generated.

Parameters:

  • log_uuid (String, nil) (defaults to: nil)

    An optional UUID to associate with the operation.



58
59
60
# File 'lib/generators/templates/application_operation.rb', line 58

def initialize(log_uuid: nil)
  self.log_uuid = log_uuid || SecureRandom.uuid
end

Class Method Details

.call(*args, **kwargs) ⇒ Array

Entry point for the operation object. Initializes the object and calls the call_with_rescue method.

Parameters:

  • args (Array)

    Positional arguments to be passed to the initializer.

  • kwargs (Hash)

    Keyword arguments to be passed to the initializer.

Returns:

  • (Array)

    A standardized result array where the first element is a boolean indicating success, the second element is either nil or the error object, and additional elements contain extra data.



49
50
51
# File 'lib/generators/templates/application_operation.rb', line 49

def self.call(*args, **kwargs)
  new(*args, **kwargs).call_with_rescue
end

Instance Method Details

#callObject

The core logic of the operation object should be implemented in this method by the subclass. This method must be overridden in any subclass that inherits from ApplicationOperation.

Raises:

  • (NotImplementedError)

    If the method is not overridden by a subclass.



81
82
83
# File 'lib/generators/templates/application_operation.rb', line 81

def call
  raise NotImplementedError, "Subclasses must implement the call method"
end

#call_with_rescueArray

Standardized method to invoke the operation object with exception handling. Captures any unexpected errors and returns a standardized failure response.

Returns:

  • (Array)

    A standardized result array where the first element is a boolean indicating success, the second element is either nil or the error object, and additional elements contain extra data.



67
68
69
70
71
72
73
74
75
# File 'lib/generators/templates/application_operation.rb', line 67

def call_with_rescue
  log_start
  result = call
  log_success
  result
rescue StandardError => e
  log_failure(e)
  handle_unexpected_error(e)
end