Class: HatiCommand::Failure

Inherits:
Result
  • Object
show all
Defined in:
lib/hati_command/failure.rb

Overview

Represents a failure result in the Result pattern. This class is used to wrap failure values and provide a consistent interface for handling both successful and failed operations.

The Failure class is part of the Result pattern implementation, working alongside the Success class to provide a type-safe way to handle operation outcomes.

Examples:

Basic usage

failure = HatiCommand::Failure.new("Operation failed")
failure.failure?  # => true
failure.success?  # => false

With error and metadata

error = StandardError.new("Database connection failed")
failure = HatiCommand::Failure.new(
  "Could not save record",
  err: error,
  meta: { attempted_at: Time.now }
)

Pattern matching

case result
when HatiCommand::Failure
  handle_error(result.failure)
end

See Also:

Instance Attribute Summary

Attributes inherited from Result

#err, #meta, #trace, #value

Instance Method Summary collapse

Methods inherited from Result

#error, #initialize, #result

Constructor Details

This class inherits a constructor from HatiCommand::Result

Instance Method Details

#failureObject

Returns the failure value wrapped by this Failure instance. This method provides access to the actual error value or message that describes why the operation failed.

Examples:

failure = Failure.new("Database error")
failure.failure  # => "Database error"

Returns:

  • (Object)

    The wrapped failure value



44
45
46
# File 'lib/hati_command/failure.rb', line 44

def failure
  value
end

#failure?Boolean

Indicates that this is a failure result. This method is part of the Result pattern interface and always returns true for Failure instances.

Examples:

failure = Failure.new("Error")
failure.failure?  # => true

Returns:

  • (Boolean)

    Always returns true



56
57
58
# File 'lib/hati_command/failure.rb', line 56

def failure?
  true
end

#successnil

Returns nil since a Failure has no success value. This method is part of the Result pattern interface and always returns nil for Failure instances.

Examples:

failure = Failure.new("Error")
failure.success  # => nil

Returns:

  • (nil)

    Always returns nil



68
69
70
# File 'lib/hati_command/failure.rb', line 68

def success
  nil
end

#success?Boolean

Indicates that this is not a success result. This method is part of the Result pattern interface and always returns false for Failure instances.

Examples:

failure = Failure.new("Error")
failure.success?  # => false

Returns:

  • (Boolean)

    Always returns false



80
81
82
# File 'lib/hati_command/failure.rb', line 80

def success?
  false
end

#to_symSymbol

Returns the symbolic representation of this result type. Useful for pattern matching and result type checking.

Examples:

failure = Failure.new("Error")
failure.to_sym  # => :failure

Returns:

  • (Symbol)

    Always returns :failure



92
93
94
# File 'lib/hati_command/failure.rb', line 92

def to_sym
  :failure
end