Class: Verquest::Result

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

Overview

A result object for operation outcomes

Result represents the outcome of an operation in the Verquest gem, particularly parameter mapping and validation operations. It follows the Result pattern, providing a consistent interface for both successful and failed operations, avoiding exceptions for control flow.

Examples:

Handling a successful result

result = Verquest::Result.success(transformed_params)
if result.success?
  process_params(result.value)
end

Handling a failed result

result = Verquest::Result.failure(["Invalid email format"])
if result.failure?
  display_errors(result.errors)
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(success:, value: nil, errors: nil) ⇒ Result

Initialize a new Result instance

Parameters:

  • success (Boolean)

    Whether the operation was successful

  • value (Object, nil) (defaults to: nil)

    The result value for successful operations

  • errors (Array, nil) (defaults to: nil)

    List of errors for failed operations



39
40
41
42
43
# File 'lib/verquest/result.rb', line 39

def initialize(success:, value: nil, errors: nil)
  @success = success
  @value = value
  @errors = errors
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



31
# File 'lib/verquest/result.rb', line 31

attr_reader :success, :value, :errors

#successBoolean (readonly)

Returns Whether the operation was successful.

Returns:

  • (Boolean)

    Whether the operation was successful



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

def success
  @success
end

#valueObject? (readonly)

Returns The result value if successful, nil otherwise.

Returns:

  • (Object, nil)

    The result value if successful, nil otherwise



31
# File 'lib/verquest/result.rb', line 31

attr_reader :success, :value, :errors

Class Method Details

.failure(errors) ⇒ Result

Create a failed result with errors

Parameters:

  • errors (Array, String)

    Error message(s) describing the failure

Returns:

  • (Result)

    A failed Result instance containing the errors



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

def self.failure(errors)
  new(success: false, errors: errors)
end

.success(value) ⇒ Result

Create a successful result with a value

Parameters:

  • value (Object)

    The successful operation’s result value

Returns:

  • (Result)

    A successful Result instance containing the value



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

def self.success(value)
  new(success: true, value: value)
end

Instance Method Details

#failure?Boolean

Check if the result represents a failed operation

Returns:

  • (Boolean)

    true if the operation failed, false otherwise



71
72
73
# File 'lib/verquest/result.rb', line 71

def failure?
  !success
end

#success?Boolean

Check if the result represents a successful operation

Returns:

  • (Boolean)

    true if the operation was successful, false otherwise



64
65
66
# File 'lib/verquest/result.rb', line 64

def success?
  success
end