Class: NullResult
Overview
NullResult represents a fallback object returned when a valid result cannot be produced.
This class implements the Null Object pattern and can optionally carry additional failure details such as a custom message and a data payload.
Example:
result = SomeService.call
if result.failure?
puts "Error: #{result.}"
puts "Details: #{result.data.inspect}" if result.data
end
Instance Method Summary collapse
-
#data ⇒ Hash?
Returns additional failure details.
-
#failure? ⇒ Boolean
Indicates that the result is a failure.
-
#initialize(message: 'No valid result available', data: nil) ⇒ NullResult
constructor
Initializes a new NullResult.
-
#message ⇒ String
Returns the failure message.
-
#success? ⇒ Boolean
Indicates that the result is not a success.
-
#to_s ⇒ String
Returns a string representation of the NullResult.
Constructor Details
#initialize(message: 'No valid result available', data: nil) ⇒ NullResult
Initializes a new NullResult.
26 27 28 29 |
# File 'lib/null_result.rb', line 26 def initialize(message: 'No valid result available', data: nil) @message = @data = data end |
Instance Method Details
#data ⇒ Hash?
Returns additional failure details.
59 60 61 |
# File 'lib/null_result.rb', line 59 def data @data end |
#failure? ⇒ Boolean
Indicates that the result is a failure.
35 36 37 |
# File 'lib/null_result.rb', line 35 def failure? true end |
#message ⇒ String
Returns the failure message.
51 52 53 |
# File 'lib/null_result.rb', line 51 def @message end |
#success? ⇒ Boolean
Indicates that the result is not a success.
43 44 45 |
# File 'lib/null_result.rb', line 43 def success? false end |
#to_s ⇒ String
Returns a string representation of the NullResult.
67 68 69 |
# File 'lib/null_result.rb', line 67 def to_s "NullResult(message: #{@message.inspect}, data: #{@data.inspect})" end |