Class: Gaskit::OperationResult
- Inherits:
-
Object
- Object
- Gaskit::OperationResult
- Defined in:
- lib/gaskit/operation_result.rb
Overview
Represents the result of an operation, encapsulating success/failure, values, errors, and execution duration.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#context ⇒ Hash
readonly
The context used during the operation.
-
#duration ⇒ Float
readonly
The duration of the operation in seconds.
-
#error ⇒ Exception?
readonly
The error that occurred during the operation, if any.
-
#success ⇒ Boolean
readonly
Whether the operation was successful.
-
#value ⇒ Object?
readonly
The result value of the operation, if any.
Instance Method Summary collapse
-
#early_exit? ⇒ Boolean
Indicates whether the operation exited early using ‘exit(:key)`.
-
#failure? ⇒ Boolean
Indicates whether the operation failed.
-
#initialize(success, value, error, duration:, context: {}) ⇒ OperationResult
constructor
Initializes a new instance of OperationResult.
-
#inspect ⇒ String
Provides a human-readable string representation of the result.
-
#status ⇒ Symbol
Returns the status of the operation result.
-
#success? ⇒ Boolean
Indicates whether the operation was successful.
-
#to_h ⇒ Hash
Converts the operation result to a structured hash.
-
#to_json(options = {}) ⇒ String
Serializes the result to a JSON string.
Constructor Details
#initialize(success, value, error, duration:, context: {}) ⇒ OperationResult
Initializes a new instance of OperationResult.
38 39 40 41 42 43 44 |
# File 'lib/gaskit/operation_result.rb', line 38 def initialize(success, value, error, duration:, context: {}) @success = success @value = value @error = error @duration = format_duration(duration) @context = context end |
Instance Attribute Details
#context ⇒ Hash (readonly)
Returns The context used during the operation.
29 30 31 |
# File 'lib/gaskit/operation_result.rb', line 29 def context @context end |
#duration ⇒ Float (readonly)
Returns The duration of the operation in seconds.
26 27 28 |
# File 'lib/gaskit/operation_result.rb', line 26 def duration @duration end |
#error ⇒ Exception? (readonly)
Returns The error that occurred during the operation, if any.
23 24 25 |
# File 'lib/gaskit/operation_result.rb', line 23 def error @error end |
#success ⇒ Boolean (readonly)
Returns Whether the operation was successful.
17 18 19 |
# File 'lib/gaskit/operation_result.rb', line 17 def success @success end |
#value ⇒ Object? (readonly)
Returns The result value of the operation, if any.
20 21 22 |
# File 'lib/gaskit/operation_result.rb', line 20 def value @value end |
Instance Method Details
#early_exit? ⇒ Boolean
Indicates whether the operation exited early using ‘exit(:key)`.
70 71 72 |
# File 'lib/gaskit/operation_result.rb', line 70 def early_exit? !@success && error.is_a?(Gaskit::OperationExit) end |
#failure? ⇒ Boolean
Indicates whether the operation failed.
63 64 65 |
# File 'lib/gaskit/operation_result.rb', line 63 def failure? !@success end |
#inspect ⇒ String
Provides a human-readable string representation of the result.
49 50 51 |
# File 'lib/gaskit/operation_result.rb', line 49 def inspect "#<#{self.class.name} status=#{status} value=#{value.inspect} duration=#{duration}>" end |
#status ⇒ Symbol
Returns the status of the operation result.
77 78 79 80 81 |
# File 'lib/gaskit/operation_result.rb', line 77 def status return :early_exit if early_exit? success? ? :success : :failure end |
#success? ⇒ Boolean
Indicates whether the operation was successful.
56 57 58 |
# File 'lib/gaskit/operation_result.rb', line 56 def success? @success end |
#to_h ⇒ Hash
Converts the operation result to a structured hash.
-
Includes ‘:value` on success
-
Includes ‘:exit` if early_exit?
-
Includes ‘:error` if a failure occurred
-
Always includes ‘:meta` with duration and context
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/gaskit/operation_result.rb', line 91 def to_h hash = { success: success?, status: status, value: value }.compact hash = failure_to_hash(hash) if failure? hash[:meta] = { duration: duration, context: context }.compact hash.freeze end |
#to_json(options = {}) ⇒ String
Serializes the result to a JSON string.
112 113 114 |
# File 'lib/gaskit/operation_result.rb', line 112 def to_json( = {}) to_h.to_json() end |