Class: ApplicationOperation
- Inherits:
-
Object
- Object
- ApplicationOperation
- 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
-
.call(*args, **kwargs) ⇒ Array
Entry point for the operation object.
Instance Method Summary collapse
-
#call ⇒ Object
The core logic of the operation object should be implemented in this method by the subclass.
-
#call_with_rescue ⇒ Array
Standardized method to invoke the operation object with exception handling.
-
#initialize(log_uuid: nil) ⇒ ApplicationOperation
constructor
Initializes the operation object with an optional log_uuid.
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.
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.
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
#call ⇒ Object
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.
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_rescue ⇒ Array
Standardized method to invoke the operation object with exception handling. Captures any unexpected errors and returns a standardized failure response.
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 |