Class: Ask::Result
- Inherits:
-
Object
- Object
- Ask::Result
- Defined in:
- lib/ask/tools/result.rb
Overview
Instance Attribute Summary collapse
-
#error ⇒ String?
readonly
The error message when the tool failed.
-
#metadata ⇒ Hash
readonly
Arbitrary metadata attached to the result.
-
#ok ⇒ Boolean
readonly
Whether the tool completed successfully.
-
#output ⇒ Object?
readonly
The output data when the tool succeeded.
Class Method Summary collapse
-
.error(message:, metadata: {}) ⇒ Ask::Result
Create a failed result.
-
.ok(data:, metadata: {}) ⇒ Ask::Result
Create a successful result.
Instance Method Summary collapse
-
#initialize(ok:, output: nil, error: nil, metadata: {}) ⇒ Result
constructor
A new instance of Result.
-
#inspect ⇒ String
Inspect string.
-
#ok? ⇒ Boolean
Whether the tool completed successfully.
-
#to_h ⇒ Hash
Hash representation suitable for serialization.
-
#to_s ⇒ String
Human-readable representation.
Constructor Details
#initialize(ok:, output: nil, error: nil, metadata: {}) ⇒ Result
Returns a new instance of Result.
27 28 29 30 31 32 |
# File 'lib/ask/tools/result.rb', line 27 def initialize(ok:, output: nil, error: nil, metadata: {}) @ok = ok @output = output @error = error @metadata = end |
Instance Attribute Details
#error ⇒ String? (readonly)
Returns the error message when the tool failed.
20 21 22 |
# File 'lib/ask/tools/result.rb', line 20 def error @error end |
#metadata ⇒ Hash (readonly)
Returns arbitrary metadata attached to the result.
23 24 25 |
# File 'lib/ask/tools/result.rb', line 23 def @metadata end |
#ok ⇒ Boolean (readonly)
Returns whether the tool completed successfully.
14 15 16 |
# File 'lib/ask/tools/result.rb', line 14 def ok @ok end |
#output ⇒ Object? (readonly)
Returns the output data when the tool succeeded.
17 18 19 |
# File 'lib/ask/tools/result.rb', line 17 def output @output end |
Class Method Details
.error(message:, metadata: {}) ⇒ Ask::Result
Create a failed result.
48 49 50 |
# File 'lib/ask/tools/result.rb', line 48 def self.error(message:, metadata: {}) new(ok: false, output: nil, error: , metadata: ) end |
.ok(data:, metadata: {}) ⇒ Ask::Result
Create a successful result.
39 40 41 |
# File 'lib/ask/tools/result.rb', line 39 def self.ok(data:, metadata: {}) new(ok: true, output: data, error: nil, metadata: ) end |
Instance Method Details
#inspect ⇒ String
Returns inspect string.
73 74 75 76 77 78 79 |
# File 'lib/ask/tools/result.rb', line 73 def inspect if ok? "#<Ask::Result ok=true output=#{output.inspect}>" else "#<Ask::Result ok=false error=#{error.inspect}>" end end |
#ok? ⇒ Boolean
Returns whether the tool completed successfully.
25 26 27 |
# File 'lib/ask/tools/result.rb', line 25 def ok @ok end |
#to_h ⇒ Hash
Hash representation suitable for serialization.
63 64 65 66 67 68 69 70 |
# File 'lib/ask/tools/result.rb', line 63 def to_h { ok: ok, output: output, error: error, metadata: } end |
#to_s ⇒ String
Human-readable representation. Returns the output for success or the error message for failure.
56 57 58 |
# File 'lib/ask/tools/result.rb', line 56 def to_s ok? ? output.to_s : error.to_s end |