Class: OmnifocusMcp::Result
- Inherits:
-
Data
- Object
- Data
- OmnifocusMcp::Result
- Defined in:
- lib/omnifocus_mcp/result.rb
Overview
Instance Attribute Summary collapse
-
#creation_location ⇒ Object
readonly
Returns the value of attribute creation_location.
-
#error_message ⇒ Object
readonly
Returns the value of attribute error_message.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.all(results) ⇒ Object
Sequence a
HashorArrayof Results into a single Result. - .error(message) ⇒ Object
- .ok(value) ⇒ Object
-
.zip(left, right) ⇒ Object
Pair two results; first error wins.
Instance Method Summary collapse
- #and_then {|ok| ... } ⇒ Object
- #deconstruct ⇒ Object
- #deconstruct_keys(keys) ⇒ Object
- #error ⇒ Object
- #error? ⇒ Boolean
-
#fold(on_ok:, on_error:) ⇒ Object
Collapses the +if ok? …
- #map ⇒ Object
- #ok ⇒ Object
- #ok? ⇒ Boolean
- #ok_or(default_value) ⇒ Object
- #or_else {|error| ... } ⇒ Object
- #where ⇒ Object
Instance Attribute Details
#creation_location ⇒ Object (readonly)
Returns the value of attribute creation_location
7 8 9 |
# File 'lib/omnifocus_mcp/result.rb', line 7 def creation_location @creation_location end |
#error_message ⇒ Object (readonly)
Returns the value of attribute error_message
7 8 9 |
# File 'lib/omnifocus_mcp/result.rb', line 7 def @error_message end |
#value ⇒ Object (readonly)
Returns the value of attribute value
7 8 9 |
# File 'lib/omnifocus_mcp/result.rb', line 7 def value @value end |
Class Method Details
.all(results) ⇒ Object
Sequence a Hash or Array of Results into a single Result. Fail-fast: the first error in iteration order wins (insertion order for Hashes). On success the wrapped value mirrors the input container’s shape: array-in → array-of-values-out, hash-in → hash-of-values-out.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/omnifocus_mcp/result.rb', line 28 def self.all(results) case results when Array first_error = results.find(&:error?) first_error || Result.ok(results.map(&:ok)) when Hash first_error = results.each_value.find(&:error?) first_error || Result.ok(results.transform_values(&:ok)) else raise ArgumentError, "Result.all expects an Array or Hash, got #{results.class}" end end |
.error(message) ⇒ Object
13 14 15 |
# File 'lib/omnifocus_mcp/result.rb', line 13 def self.error() new(value: nil, error_message: , creation_location: caller_locations(1, 1).first) end |
.ok(value) ⇒ Object
8 9 10 |
# File 'lib/omnifocus_mcp/result.rb', line 8 def self.ok(value) new(value: value, error_message: nil, creation_location: caller_locations(1, 1).first) end |
Instance Method Details
#and_then {|ok| ... } ⇒ Object
67 68 69 70 71 |
# File 'lib/omnifocus_mcp/result.rb', line 67 def and_then return self if error? yield(ok) end |
#deconstruct ⇒ Object
86 |
# File 'lib/omnifocus_mcp/result.rb', line 86 def deconstruct = [value, ] |
#deconstruct_keys(keys) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/omnifocus_mcp/result.rb', line 88 def deconstruct_keys(keys) if keys.nil? return error? ? { error_message: } : { value: value } end keys.filter_map do |key| val = public_send(key) [key, val] unless val.nil? end.to_h end |
#error ⇒ Object
53 54 55 56 57 |
# File 'lib/omnifocus_mcp/result.rb', line 53 def error raise "Cannot get error from ok result" if ok? end |
#error? ⇒ Boolean
43 |
# File 'lib/omnifocus_mcp/result.rb', line 43 def error? = !.nil? |
#fold(on_ok:, on_error:) ⇒ Object
Collapses the if ok? … else … end shape into a single expression. The chosen branch is called with the wrapped value (or error message) and its return value is passed through verbatim; this is a terminator, not a chainable Result-returning combinator.
82 |
# File 'lib/omnifocus_mcp/result.rb', line 82 def fold(on_ok:, on_error:) = ok? ? on_ok.call(ok) : on_error.call(error) |
#map ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/omnifocus_mcp/result.rb', line 59 def map return self if error? Result.ok(yield(ok)) rescue StandardError => e Result.error(e.) end |
#ok ⇒ Object
45 46 47 48 49 |
# File 'lib/omnifocus_mcp/result.rb', line 45 def ok raise "Cannot get value from error result: #{}, #{creation_location}" if error? value end |
#ok? ⇒ Boolean
41 |
# File 'lib/omnifocus_mcp/result.rb', line 41 def ok? = !value.nil? |
#ok_or(default_value) ⇒ Object
51 |
# File 'lib/omnifocus_mcp/result.rb', line 51 def ok_or(default_value) = ok? ? value : default_value |
#or_else {|error| ... } ⇒ Object
73 74 75 76 77 |
# File 'lib/omnifocus_mcp/result.rb', line 73 def or_else return self if ok? yield(error) end |
#where ⇒ Object
84 |
# File 'lib/omnifocus_mcp/result.rb', line 84 def where = creation_location |