Class: Familia::MultiResult

Inherits:
Object
  • Object
show all
Defined in:
lib/familia/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 = Familia::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.



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

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



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

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



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

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



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

def errors?
  !errors.empty?
end

#sizeInteger

Returns the number of results in the multi-operation.

Returns:

  • (Integer)

    The number of individual command results returned



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

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



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

def successful?
  errors.empty?
end

#to_hHash

Returns a hash representation of the result.

Returns:

  • (Hash)

    Hash with :success and :results keys



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

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.



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

def tuple
  [successful?, results]
end