Class: MultiResult

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

Overview

Represents the result of a Valkey/Redis transaction operation.

This class encapsulates the outcome of a Database transaction, providing access to both the success status and the individual command results returned by the transaction.

Examples:

Creating a MultiResult instance

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

Checking transaction success

if result.successful?
  puts "Transaction completed successfully"
else
  puts "Transaction failed"
end

Accessing individual command results

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(success, results) ⇒ MultiResult

Creates a new MultiResult instance.

Parameters:

  • success (Boolean)

    Whether all commands succeeded

  • results (Array<String>)

    The raw results from Database commands



43
44
45
46
# File 'lib/multi_result.rb', line 43

def initialize(success, results)
  @success = success
  @results = results
end

Instance Attribute Details

#resultsArray<String> (readonly)

Returns The raw return values from the Database commands.

Returns:

  • (Array<String>)

    The raw return values from the Database commands



31
32
33
# File 'lib/multi_result.rb', line 31

def results
  @results
end

#successBoolean (readonly)

Returns true if all commands in the transaction succeeded, false otherwise.

Returns:

  • (Boolean)

    true if all commands in the transaction succeeded, false otherwise



31
32
33
# File 'lib/multi_result.rb', line 31

def success
  @success
end

Instance Method Details

#sizeInteger

Returns the number of results in the multi-operation.

Returns:

  • (Integer)

    The number of individual command results returned by the transaction.



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

def size
  results.size
end

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

Convenient method to check if the commit was successful.

Returns:

  • (Boolean)

    true if all commands succeeded, false otherwise



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

def successful?
  @success
end

#to_hObject



69
70
71
# File 'lib/multi_result.rb', line 69

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

#tupleArray Also known as: to_a

Returns a tuple representing the result of the transaction.

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.



57
58
59
# File 'lib/multi_result.rb', line 57

def tuple
  [successful?, results]
end