Skip to content

Commit 58f4913

Browse files
committed
add: add yardoc to results
1 parent 27d3336 commit 58f4913

File tree

3 files changed

+203
-51
lines changed

3 files changed

+203
-51
lines changed

lib/hati_command/failure.rb

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,94 @@
11
# frozen_string_literal: true
22

3+
# @module HatiCommand
4+
# Provides command handling functionalities and result objects.
35
module HatiCommand
4-
# This class represents a failure result.
6+
# @class Failure
7+
# Represents a failure result in the Result pattern.
8+
# This class is used to wrap failure values and provide a consistent interface
9+
# for handling both successful and failed operations.
510
#
6-
# @example
7-
# failure_result = HatiCommand::Failure.new("An error occurred")
8-
# failure_result.failure # => "An error occurred"
9-
# failure_result.failure? # => true
10-
# failure_result.success # => nil
11-
# failure_result.success? # => false
11+
# The Failure class is part of the Result pattern implementation, working alongside
12+
# the Success class to provide a type-safe way to handle operation outcomes.
13+
#
14+
# @example Basic usage
15+
# failure = HatiCommand::Failure.new("Operation failed")
16+
# failure.failure? # => true
17+
# failure.success? # => false
18+
#
19+
# @example With error and metadata
20+
# error = StandardError.new("Database connection failed")
21+
# failure = HatiCommand::Failure.new(
22+
# "Could not save record",
23+
# err: error,
24+
# meta: { attempted_at: Time.now }
25+
# )
26+
#
27+
# @example Pattern matching
28+
# case result
29+
# when HatiCommand::Failure
30+
# handle_error(result.failure)
31+
# end
32+
#
33+
# @see HatiCommand::Success
34+
# @see HatiCommand::Result
1235
class Failure < Result
13-
# Returns the value associated with the failure.
36+
# Returns the failure value wrapped by this Failure instance.
37+
# This method provides access to the actual error value or message
38+
# that describes why the operation failed.
1439
#
15-
# @return [Object] the value of the failure
40+
# @return [Object] The wrapped failure value
41+
# @example
42+
# failure = Failure.new("Database error")
43+
# failure.failure # => "Database error"
1644
def failure
1745
value
1846
end
1947

20-
# Indicates whether the result is a failure.
48+
# Indicates that this is a failure result.
49+
# This method is part of the Result pattern interface and always
50+
# returns true for Failure instances.
2151
#
22-
# @return [Boolean] true if this is a failure, false otherwise
52+
# @return [Boolean] Always returns true
53+
# @example
54+
# failure = Failure.new("Error")
55+
# failure.failure? # => true
2356
def failure?
2457
true
2558
end
2659

27-
# Returns nil as there is no success value in a failure.
60+
# Returns nil since a Failure has no success value.
61+
# This method is part of the Result pattern interface and always
62+
# returns nil for Failure instances.
2863
#
29-
# @return [nil]
64+
# @return [nil] Always returns nil
65+
# @example
66+
# failure = Failure.new("Error")
67+
# failure.success # => nil
3068
def success
3169
nil
3270
end
3371

34-
# Indicates whether the result is a success.
72+
# Indicates that this is not a success result.
73+
# This method is part of the Result pattern interface and always
74+
# returns false for Failure instances.
3575
#
36-
# @return [Boolean] false since this is a failure
76+
# @return [Boolean] Always returns false
77+
# @example
78+
# failure = Failure.new("Error")
79+
# failure.success? # => false
3780
def success?
3881
false
3982
end
4083

84+
# Returns the symbolic representation of this result type.
85+
# Useful for pattern matching and result type checking.
86+
#
87+
# @return [Symbol] Always returns :failure
88+
# @api public
89+
# @example
90+
# failure = Failure.new("Error")
91+
# failure.to_sym # => :failure
4192
def to_sym
4293
:failure
4394
end

lib/hati_command/result.rb

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,105 @@
11
# frozen_string_literal: true
22

3+
# @module HatiCommand
4+
# Provides command handling functionalities and result objects.
35
module HatiCommand
4-
# This class represents a result.
6+
# @class Result
7+
# Base class for the Result pattern implementation.
8+
# This class serves as the foundation for Success and Failure result types,
9+
# providing common functionality and a consistent interface for handling
10+
# operation outcomes.
511
#
6-
# @example
7-
# result = HatiCommand::Result.new("Success", err: nil, meta: { key: "value" })
8-
# result.value # => "Success"
9-
# result.error # => nil
10-
# result.meta # => { key: "value" }
12+
# The Result pattern helps in handling operation outcomes in a type-safe way,
13+
# making it explicit whether an operation succeeded or failed, and carrying
14+
# additional context like error messages and metadata.
1115
#
12-
# @!attribute [r] value
13-
# @return [Object] the value associated with the result
16+
# @abstract Subclass and override {#to_sym} to implement a concrete result type
17+
#
18+
# @example Basic usage
19+
# result = HatiCommand::Result.new("Operation output")
20+
# result.value # => "Operation output"
21+
#
22+
# @example With error and metadata
23+
# result = HatiCommand::Result.new(
24+
# "Operation output",
25+
# err: "Warning: partial completion",
26+
# meta: { duration_ms: 150 }
27+
# )
28+
#
29+
# @example Using trace information
30+
# result = HatiCommand::Result.new("Output", trace: caller(1..1))
31+
# result.trace # => ["path/to/file.rb:42:in `method_name'"]
32+
#
33+
# @see HatiCommand::Success
34+
# @see HatiCommand::Failure
1435
#
15-
# @!attribute [r] err
16-
# @return [String, nil] an optional error message associated with the result
36+
# @!attribute [r] value
37+
# @return [Object] The wrapped value representing the operation's output
1738
#
1839
# @!attribute [r] meta
19-
# @return [Hash] optional metadata associated with the result
40+
# @return [Hash] Additional metadata associated with the result
41+
#
42+
# @!attribute [rw] trace
43+
# @return [Array<String>, nil] Execution trace information for debugging
2044
class Result
2145
attr_reader :value, :meta
2246
attr_accessor :trace
2347

24-
# Initializes a new Result instance.
48+
# Initializes a new Result instance with a value and optional context.
49+
#
50+
# @param value [Object] The value to be wrapped in the result
51+
# @param err [String, nil] Optional error message or error object
52+
# @param meta [Hash] Optional metadata for additional context
53+
# @param trace [Array<String>, nil] Optional execution trace for debugging
2554
#
26-
# @param value [Object] the value to be wrapped in the result
27-
# @param err [String, nil] an optional error message
28-
# @param meta [Hash] optional metadata
29-
# @param trace [Array] optional trace
55+
# @example Basic initialization
56+
# result = Result.new("Success")
57+
#
58+
# @example With full context
59+
# result = Result.new(
60+
# "Partial success",
61+
# err: "Some records failed",
62+
# meta: { processed: 10, failed: 2 },
63+
# trace: caller
64+
# )
3065
def initialize(value, err: nil, meta: {}, trace: nil)
3166
@value = value
3267
@err = err
3368
@meta = meta
3469
@trace = trace
3570
end
3671

37-
# Returns the result instance itself.
72+
# Returns self to provide a consistent interface across result types.
73+
# This method ensures that all result objects can be treated uniformly
74+
# when chaining operations.
3875
#
39-
# @return [HatiCommand::Result] the result instance
76+
# @return [HatiCommand::Result] The result instance itself
77+
# @api public
4078
def result
4179
self
4280
end
4381

44-
# Returns the error message associated with the result.
82+
# Returns the error associated with this result.
83+
# This can be used to check for warnings or errors even in successful results.
4584
#
46-
# @return [String, nil] the error message
47-
# @raise [StandardError] if there is an error message
85+
# @return [String, nil] The error message or object, if any
86+
# @raise [StandardError] If accessing the error triggers an error condition
87+
# @api public
88+
# @example
89+
# result = Result.new("Value", err: "Warning message")
90+
# result.error # => "Warning message"
4891
def error
4992
@err
5093
end
5194

95+
# Returns the symbolic representation of this result type.
96+
# This is an abstract method that should be overridden by concrete result types.
97+
#
98+
# @return [Symbol] Returns :undefined for the base class
99+
# @abstract Subclasses must override this method
100+
# @api public
101+
# @example
102+
# Result.new("value").to_sym # => :undefined
52103
def to_sym
53104
:undefined
54105
end

lib/hati_command/success.rb

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,93 @@
11
# frozen_string_literal: true
22

3+
# @module HatiCommand
4+
# Provides command handling functionalities and result objects.
35
module HatiCommand
4-
# This class represents a successful result.
6+
# @class Success
7+
# Represents a successful result in the Result pattern.
8+
# This class is used to wrap successful operation values and provide a consistent interface
9+
# for handling both successful and failed operations.
510
#
6-
# @example
7-
# success_result = HatiCommand::Success.new("Operation completed successfully")
8-
# success_result.success # => "Operation completed successfully"
9-
# success_result.success? # => true
10-
# success_result.failure # => nil
11-
# success_result.failure? # => false
11+
# The Success class is part of the Result pattern implementation, working alongside
12+
# the Failure class to provide a type-safe way to handle operation outcomes.
13+
#
14+
# @example Basic usage
15+
# success = HatiCommand::Success.new("Operation completed")
16+
# success.success? # => true
17+
# success.failure? # => false
18+
#
19+
# @example With metadata
20+
# success = HatiCommand::Success.new(
21+
# { id: 123, name: "Example" },
22+
# meta: { duration_ms: 50 }
23+
# )
24+
# success.success # => { id: 123, name: "Example" }
25+
#
26+
# @example Pattern matching
27+
# case result
28+
# when HatiCommand::Success
29+
# process_data(result.success)
30+
# end
31+
#
32+
# @see HatiCommand::Failure
33+
# @see HatiCommand::Result
1234
class Success < Result
13-
# Returns the value associated with the success.
35+
# Returns the success value wrapped by this Success instance.
36+
# This method provides access to the actual value or result
37+
# that was produced by the successful operation.
1438
#
15-
# @return [Object] the value of the success
39+
# @return [Object] The wrapped success value
40+
# @example
41+
# success = Success.new("Operation output")
42+
# success.success # => "Operation output"
1643
def success
1744
value
1845
end
1946

20-
# Indicates whether the result is a success.
47+
# Indicates that this is a success result.
48+
# This method is part of the Result pattern interface and always
49+
# returns true for Success instances.
2150
#
22-
# @return [Boolean] true since this is a success
51+
# @return [Boolean] Always returns true
52+
# @example
53+
# success = Success.new("Result")
54+
# success.success? # => true
2355
def success?
2456
true
2557
end
2658

27-
# Returns nil as there is no failure value in a success.
59+
# Returns nil since a Success has no failure value.
60+
# This method is part of the Result pattern interface and always
61+
# returns nil for Success instances.
2862
#
29-
# @return [nil]
63+
# @return [nil] Always returns nil
64+
# @example
65+
# success = Success.new("Result")
66+
# success.failure # => nil
3067
def failure
3168
nil
3269
end
3370

34-
# Indicates whether the result is a failure.
71+
# Indicates that this is not a failure result.
72+
# This method is part of the Result pattern interface and always
73+
# returns false for Success instances.
3574
#
36-
# @return [Boolean] false since this is a success
75+
# @return [Boolean] Always returns false
76+
# @example
77+
# success = Success.new("Result")
78+
# success.failure? # => false
3779
def failure?
3880
false
3981
end
4082

83+
# Returns the symbolic representation of this result type.
84+
# Useful for pattern matching and result type checking.
85+
#
86+
# @return [Symbol] Always returns :success
87+
# @api public
88+
# @example
89+
# success = Success.new("Result")
90+
# success.to_sym # => :success
4191
def to_sym
4292
:success
4393
end

0 commit comments

Comments
 (0)