Class: Gaskit::OperationResult

Inherits:
Object
  • Object
show all
Defined in:
lib/gaskit/operation_result.rb

Overview

Represents the result of an operation, encapsulating success/failure, values, errors, and execution duration.

Examples:

Using OperationResult to handle success and failure

result = Gaskit::BaseResult.new(true, "data", nil, 1.23)
if result.success?
  puts "Operation succeeded with value: #{result.value}"
else
  puts "Operation failed with reason: #{result.to_h[:error]}"
end

Direct Known Subclasses

FlowResult, QueryResult, ServiceResult

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(success, value, error, duration:, context: {}) ⇒ OperationResult

Initializes a new instance of OperationResult.

Parameters:

  • success (Boolean)

    Whether the operation was successful.

  • value (Object, nil)

    The value obtained as a result of the operation.

  • error (StandardError, nil)

    The error encountered during the operation.

  • duration (Float, String)

    The time taken to complete the operation in seconds.

  • context (Hash) (defaults to: {})

    Optional context metadata for this operation.



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

#contextHash (readonly)

Returns The context used during the operation.

Returns:

  • (Hash)

    The context used during the operation.



29
30
31
# File 'lib/gaskit/operation_result.rb', line 29

def context
  @context
end

#durationFloat (readonly)

Returns The duration of the operation in seconds.

Returns:

  • (Float)

    The duration of the operation in seconds.



26
27
28
# File 'lib/gaskit/operation_result.rb', line 26

def duration
  @duration
end

#errorException? (readonly)

Returns The error that occurred during the operation, if any.

Returns:

  • (Exception, nil)

    The error that occurred during the operation, if any.



23
24
25
# File 'lib/gaskit/operation_result.rb', line 23

def error
  @error
end

#successBoolean (readonly)

Returns Whether the operation was successful.

Returns:

  • (Boolean)

    Whether the operation was successful.



17
18
19
# File 'lib/gaskit/operation_result.rb', line 17

def success
  @success
end

#valueObject? (readonly)

Returns The result value of the operation, if any.

Returns:

  • (Object, nil)

    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)`.

Returns:

  • (Boolean)

    ‘true` if the operation exited early, `false` otherwise.



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.

Returns:

  • (Boolean)

    ‘true` if the operation failed, `false` otherwise.



63
64
65
# File 'lib/gaskit/operation_result.rb', line 63

def failure?
  !@success
end

#inspectString

Provides a human-readable string representation of the result.

Returns:

  • (String)

    The formatted inspection string.



49
50
51
# File 'lib/gaskit/operation_result.rb', line 49

def inspect
  "#<#{self.class.name} status=#{status} value=#{value.inspect} duration=#{duration}>"
end

#statusSymbol

Returns the status of the operation result.

Returns:

  • (Symbol)

    :success, :failure, or :early_exit



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.

Returns:

  • (Boolean)

    ‘true` if the operation was successful, `false` otherwise.



56
57
58
# File 'lib/gaskit/operation_result.rb', line 56

def success?
  @success
end

#to_hHash

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

Returns:

  • (Hash)

    A nested representation of the result.



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.

Parameters:

  • options (Hash) (defaults to: {})

    Optional hash passed to ‘JSON.generate`

Returns:

  • (String)

    JSON representation of the operation result



112
113
114
# File 'lib/gaskit/operation_result.rb', line 112

def to_json(options = {})
  to_h.to_json(options)
end