Class: MultiResult

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

Overview

Represents the result of a Valkey/Redis transaction or pipeline operation.

This class encapsulates the outcome of a Database multi-command operation, providing access to both the command results and derived success status based on the presence of errors in the results.

Success is determined by checking for Exception objects in the results array. When Redis commands fail within a transaction or pipeline, they return exception objects rather than raising them, allowing other commands to continue executing.

Examples:

Creating a MultiResult instance

result = MultiResult.new(["OK", "OK", 1])

Checking transaction success

if result.successful?
  puts "All commands completed without errors"
else
  puts "#{result.errors.size} command(s) failed"
end

Accessing individual command results

result.results.each_with_index do |value, index|
  puts "Command #{index + 1} returned: #{value}"
end

Inspecting errors

if result.errors?
  result.errors.each do |error|
    puts "Error: #{error.message}"
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(results) ⇒ MultiResult

Creates a new MultiResult instance.

Parameters:

  • results (Array)

    The raw results from Database commands. Exception objects in the array indicate command failures.



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

def initialize(results)
  @results = results
end

Instance Attribute Details

#resultsArray (readonly)

Returns The raw return values from the Database commands.

Returns:

  • (Array)

    The raw return values from the Database commands



41
42
43
# File 'lib/multi_result.rb', line 41

def results
  @results
end

Instance Method Details

#errorsArray<Exception>

Returns all Exception objects from the results array.

This method is memoized for performance when called multiple times on the same MultiResult instance.

Returns:

  • (Array<Exception>)

    Array of exceptions that occurred during execution



59
60
61
# File 'lib/multi_result.rb', line 59

def errors
  @errors ||= results.select { |ret| ret.is_a?(Exception) }
end

#errors?Boolean

Checks if any errors occurred during execution.

Returns:

  • (Boolean)

    true if at least one command failed, false otherwise



66
67
68
# File 'lib/multi_result.rb', line 66

def errors?
  !errors.empty?
end

#sizeInteger

Returns the number of results in the multi-operation.

Returns:

  • (Integer)

    The number of individual command results returned



99
100
101
# File 'lib/multi_result.rb', line 99

def size
  results.size
end

#successful?Boolean Also known as: success?, areyouhappynow?

Checks if all commands completed successfully (no exceptions).

This is the primary method for determining if a multi-command operation completed without errors.

Returns:

  • (Boolean)

    true if no exceptions in results, false otherwise



76
77
78
# File 'lib/multi_result.rb', line 76

def successful?
  errors.empty?
end

#to_hHash

Returns a hash representation of the result.

Returns:

  • (Hash)

    Hash with :success and :results keys



106
107
108
# File 'lib/multi_result.rb', line 106

def to_h
  { success: successful?, results: results }
end

#tupleArray Also known as: to_a

Returns a tuple representing the result of the operation.

Examples:

[true, ["OK", true, 1]]

Returns:

  • (Array)

    A tuple containing the success status and the raw results. The success status is a boolean indicating if all commands succeeded. The raw results is an array of return values from the Database commands.



91
92
93
# File 'lib/multi_result.rb', line 91

def tuple
  [successful?, results]
end