-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathresult.rb
More file actions
229 lines (218 loc) · 5.38 KB
/
Copy pathresult.rb
File metadata and controls
229 lines (218 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
module GitHub
class Result
# Invokes the supplied block and wraps the return value in a
# GitHub::Result object.
#
# Exceptions raised by the block are caught and also wrapped.
#
# Example:
#
# GitHub::Result.new { 123 }
# # => #<GitHub::Result value: 123>
#
# GitHub::Result.new { raise "oops" }
# # => #<GitHub::Result error: #<RuntimeError: oops>>
#
def initialize
begin
@value = yield
@error = nil
rescue => e
@error = e
end
end
def to_s
if ok?
"#<GitHub::Result:0x%x value: %s>" % [object_id, @value.inspect]
else
"#<GitHub::Result:0x%x error: %s>" % [object_id, @error.inspect]
end
end
alias_method :inspect, :to_s
# If the result represents a value, invokes the supplied block with
# that value.
#
# If the result represents an error, returns self.
#
# The block must also return a GitHub::Result object.
# Use #map otherwise.
#
# Example:
#
# result = do_something().then { |val|
# do_other_thing(val)
# }
# # => #<GitHub::Result value: ...>
#
# do_something_that_fails().then { |val|
# # never invoked
# }
# # => #<GitHub::Result error: ...>
#
def then
if ok?
result = yield(@value)
raise TypeError, "block invoked in GitHub::Result#then did not return GitHub::Result" unless result.is_a?(Result)
result
else
self
end
end
# If the result represents an error, invokes the supplied block with that error.
#
# If the result represents a value, returns self.
#
# The block must also return a GitHub::Result object.
# Use #map otherwise.
#
# Example:
#
# result = do_something().rescue { |val|
# # never invoked
# }
# # => #<GitHub::Result value: ...>
#
# do_something_that_fails().rescue { |val|
# # handle_error(val)
# }
# # => #<GitHub::Result error: ...>
#
def rescue
return self if ok?
result = yield(@error)
raise TypeError, "block invoked in GitHub::Result#rescue did not return GitHub::Result" unless result.is_a?(Result)
result
end
# If the result represents a value, invokes the supplied block with that
# value and wraps the block's return value in a GitHub::Result.
#
# If the result represents an error, returns self.
#
# The block should not return a GitHub::Result object (unless you
# truly intend to create a GitHub::Result<GitHub::Result<T>>).
# Use #then if it does.
#
# Example:
#
# result = do_something()
# # => #<GitHub::Result value: 123>
#
# result.map { |val| val * 2 }
# # => #<GitHub::Result value: 246>
#
# do_something_that_fails().map { |val|
# # never invoked
# }
# # => #<GitHub::Result error: ...>
#
def map
if ok?
Result.new { yield(@value) }
else
self
end
end
# If the result represents a value, returns that value.
#
# If the result represents an error, invokes the supplied block with the
# exception object.
#
# Example:
#
# result = do_something()
# # => #<GitHub::Result value: "foo">
#
# result.value { "nope" }
# # => "foo"
#
# result = do_something_that_fails()
# # => #<GitHub::Result error: ...>
#
# result.value { "nope" }
# # => #<GitHub::Result value: "nope">
#
def value
unless block_given?
raise ArgumentError, "must provide a block to GitHub::Result#value to be invoked in case of error"
end
if ok?
@value
else
yield(@error)
end
end
# If the result represents a value, returns that value.
#
# If the result represents an error, raises that error.
#
# Example:
#
# result = do_something()
# # => #<GitHub::Result value: "foo">
#
# result.value!
# # => "foo"
#
# result = do_something_that_fails()
# # => #<GitHub::Result error: ...>
#
# result.value!
# # !! raises exception
#
def value!
if ok?
@value
else
raise @error
end
end
# Returns true if the result represents a value, false if an error.
#
# Example:
#
# result = do_something()
# # => #<GitHub::Result value: "foo">
#
# result.ok?
# # => true
#
# result = do_something_that_fails()
# # => #<GitHub::Result error: ...>
#
# result.ok?
# # => false
#
def ok?
!@error
end
# If the result represents a value, returns nil.
#
# If the result represents an error, returns that error.
#
# result = do_something()
# # => #<GitHub::Result value: "foo">
#
# result.error
# # => nil
#
# result = do_something_that_fails()
# # => #<GitHub::Result error: ...>
#
# result.error
# # => ...
#
def error
@error
end
# Create a GitHub::Result with only the error condition set.
#
# GitHub::Result.error(e)
# # => # <GitHub::Result error: ...>
#
def self.error(e)
result = allocate
result.instance_variable_set(:@error, e)
result
end
end
end