Class: OmnifocusMcp::Result

Inherits:
Data
  • Object
show all
Defined in:
lib/omnifocus_mcp/result.rb

Overview

Success/disjoint-error ADT. #and_then does not rescue: the block must return another Result and should not raise for domain failures (use #map or return Result.error). Programmer bugs and invariant violations should use raise, not Result.error.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#creation_locationObject (readonly)

Returns the value of attribute creation_location

Returns:

  • (Object)

    the current value of creation_location



7
8
9
# File 'lib/omnifocus_mcp/result.rb', line 7

def creation_location
  @creation_location
end

#error_messageObject (readonly)

Returns the value of attribute error_message

Returns:

  • (Object)

    the current value of error_message



7
8
9
# File 'lib/omnifocus_mcp/result.rb', line 7

def error_message
  @error_message
end

#valueObject (readonly)

Returns the value of attribute value

Returns:

  • (Object)

    the current value of 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

Parameters:

  • message (Object)

    user-facing String or other user-facing error payload.



13
14
15
# File 'lib/omnifocus_mcp/result.rb', line 13

def self.error(message)
  new(value: nil, error_message: 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

.zip(left, right) ⇒ Object

Pair two results; first error wins. Values become a two-element Array on success.



18
19
20
21
22
23
# File 'lib/omnifocus_mcp/result.rb', line 18

def self.zip(left, right)
  return left if left.error?
  return right if right.error?

  Result.ok([left.ok, right.ok])
end

Instance Method Details

#and_then {|ok| ... } ⇒ Object

Yields:



67
68
69
70
71
# File 'lib/omnifocus_mcp/result.rb', line 67

def and_then
  return self if error?

  yield(ok)
end

#deconstructObject



86
# File 'lib/omnifocus_mcp/result.rb', line 86

def deconstruct = [value, error_message]

#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: error_message } : { value: value }
  end

  keys.filter_map do |key|
    val = public_send(key)
    [key, val] unless val.nil?
  end.to_h
end

#errorObject



53
54
55
56
57
# File 'lib/omnifocus_mcp/result.rb', line 53

def error
  raise "Cannot get error from ok result" if ok?

  error_message
end

#error?Boolean

Returns:

  • (Boolean)


43
# File 'lib/omnifocus_mcp/result.rb', line 43

def error? = !error_message.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)

#mapObject



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.message)
end

#okObject



45
46
47
48
49
# File 'lib/omnifocus_mcp/result.rb', line 45

def ok
  raise "Cannot get value from error result: #{error_message}, #{creation_location}" if error?

  value
end

#ok?Boolean

Returns:

  • (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

Yields:



73
74
75
76
77
# File 'lib/omnifocus_mcp/result.rb', line 73

def or_else
  return self if ok?

  yield(error)
end

#whereObject



84
# File 'lib/omnifocus_mcp/result.rb', line 84

def where = creation_location