|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
| 3 | +# @module HatiCommand |
| 4 | +# Provides command handling functionalities and result objects. |
3 | 5 | 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. |
5 | 11 | # |
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. |
11 | 15 | # |
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 |
14 | 35 | # |
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 |
17 | 38 | # |
18 | 39 | # @!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 |
20 | 44 | class Result |
21 | 45 | attr_reader :value, :meta |
22 | 46 | attr_accessor :trace |
23 | 47 |
|
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 |
25 | 54 | # |
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 | + # ) |
30 | 65 | def initialize(value, err: nil, meta: {}, trace: nil) |
31 | 66 | @value = value |
32 | 67 | @err = err |
33 | 68 | @meta = meta |
34 | 69 | @trace = trace |
35 | 70 | end |
36 | 71 |
|
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. |
38 | 75 | # |
39 | | - # @return [HatiCommand::Result] the result instance |
| 76 | + # @return [HatiCommand::Result] The result instance itself |
| 77 | + # @api public |
40 | 78 | def result |
41 | 79 | self |
42 | 80 | end |
43 | 81 |
|
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. |
45 | 84 | # |
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" |
48 | 91 | def error |
49 | 92 | @err |
50 | 93 | end |
51 | 94 |
|
| 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 |
52 | 103 | def to_sym |
53 | 104 | :undefined |
54 | 105 | end |
|
0 commit comments