monad-oxide 0.8.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/lib/err.rb +45 -0
  3. data/lib/ok.rb +48 -2
  4. data/lib/result.rb +70 -11
  5. data/lib/version.rb +1 -1
  6. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c37567d8988443ab1076e07e5784fa454f430a12400acbb15526de2164a2686
4
- data.tar.gz: a09be57539e071b3094b1d052cff724f0b71e8ae22ecd3ed151ffc355984b041
3
+ metadata.gz: 12758d7a4698325f9f371dd587d52879bcb2088c89f6691600e56d4525d428a5
4
+ data.tar.gz: a7fad1a246adc14c63fcc1ac4d9c1f4e01b658b0077b0eb058218822eadcc9a8
5
5
  SHA512:
6
- metadata.gz: a0b562a479e20ec1f488fe476c60c1c7138aa62f67b9cb83ca526ce0ff4e97a8c193d75062a175a466e877ae27b163ab9162cf72352126122035aa397024ac88
7
- data.tar.gz: a78393ad99ebe353b9a9ccdbc31756e87a9578e768fa4b47fbef1bd756b08baa131b52ff69129a502d503e40daa44df283b678ec0ac26f5738b3bce562f003c9
6
+ metadata.gz: ebdc0c265cd9a2dfaef992c75fbb62675608fe74247e9fc00cb67201c6b6280999104d72926755990b38ab6b8ad1b5d20c3be9899446c787cf7765222129fce3
7
+ data.tar.gz: e27ae5ed41c7fed7f7fb90afc6a43b1165aeb41daf1295f756428cbf77a782f9210e33766b15ad973fae9760752cdcaa4d6ac96f53e9b34abbe63798da0ac2d4
data/lib/err.rb CHANGED
@@ -65,6 +65,17 @@ module MonadOxide
65
65
  self
66
66
  end
67
67
 
68
+ ##
69
+ # Identifies that this is an `Err`.
70
+ # For counterparts:
71
+ # @see MonadOxide::Ok#ok?
72
+ # @see MonadOxide::Ok#err?
73
+ # @see MonadOxide::Err#ok?
74
+ # @return [Boolean] `true` because this is an `Err`.
75
+ def err?()
76
+ true
77
+ end
78
+
68
79
  ##
69
80
  # Applies `f' or the block over the `Exception' and returns the same `Err'.
70
81
  # No changes are applied. This is ideal for logging. Exceptions raised
@@ -122,6 +133,17 @@ module MonadOxide
122
133
  end
123
134
  end
124
135
 
136
+ ##
137
+ # Identifies that this is not an `Ok`.
138
+ # For counterparts:
139
+ # @see MonadOxide::Ok#ok?
140
+ # @see MonadOxide::Ok#err?
141
+ # @see MonadOxide::Err#err?
142
+ # @return [Boolean] `false` because this is not an `Ok`.
143
+ def ok?()
144
+ false
145
+ end
146
+
125
147
  ##
126
148
  # Invokes `f' or the block with the data and returns the Result returned
127
149
  # from that. Exceptions raised during `f' or the block will return an
@@ -166,6 +188,16 @@ module MonadOxide
166
188
  @data
167
189
  end
168
190
 
191
+ ##
192
+ # Safely unwrap the `Result`. In the case of `Err`, this returns the
193
+ # wrapped value.
194
+ #
195
+ # @param _f A dummy function. Not used.
196
+ # @return [T] The wrapped value.
197
+ def unwrap_err_or_else(f=nil, &_block)
198
+ @data
199
+ end
200
+
169
201
  ##
170
202
  # Safely unwrap the `Result`. In the case of `Err`, this returns the
171
203
  # provided default value.
@@ -176,5 +208,18 @@ module MonadOxide
176
208
  x
177
209
  end
178
210
 
211
+ ##
212
+ # Safely unwrap the `Result`. In the case of `Err`, this uses the provided
213
+ # function to produce a value.
214
+ #
215
+ # @param f [Proc<B>] The function to call. Could be a block
216
+ # instead. Takes nothing and returns a [B=Object].
217
+ # @yield Will yield a block that takes nothing and returns a [B=Object].
218
+ # Same as `f' parameter.
219
+ # @return [B] The value returned from `f`.
220
+ def unwrap_or_else(f=nil, &block)
221
+ (f || block).call()
222
+ end
223
+
179
224
  end
180
225
  end
data/lib/ok.rb CHANGED
@@ -40,6 +40,17 @@ module MonadOxide
40
40
  end
41
41
  end
42
42
 
43
+ ##
44
+ # Identifies that this is not an `Err`.
45
+ # For counterparts:
46
+ # @see MonadOxide::Ok#ok?
47
+ # @see MonadOxide::Err#ok?
48
+ # @see MonadOxide::Err#err?
49
+ # @return [Boolean] `false` because this is not an `Err`.
50
+ def err?()
51
+ false
52
+ end
53
+
43
54
  ##
44
55
  # Falls through. @see Result#inspect_err for how this is handled in either
45
56
  # Result case, and @see Err.inspect_err for how this is handled in the Err
@@ -96,6 +107,17 @@ module MonadOxide
96
107
  self
97
108
  end
98
109
 
110
+ ##
111
+ # Identifies that this is an `Ok`.
112
+ # For counterparts:
113
+ # @see MonadOxide::Ok#err?
114
+ # @see MonadOxide::Err#ok?
115
+ # @see MonadOxide::Err#err?
116
+ # @return [Boolean] `true` because this is an `Ok`.
117
+ def ok?()
118
+ true
119
+ end
120
+
99
121
  ##
100
122
  # The Err equivalent to Ok#and_then. This is a no-op for Ok. @see
101
123
  # Err#or_else.
@@ -124,13 +146,37 @@ module MonadOxide
124
146
  )
125
147
  end
126
148
 
149
+ ##
150
+ # Safely unwrap the `Result`. In the case of `Ok`, this uses the provided
151
+ # function to produce a value.
152
+ #
153
+ # @param f [Proc<B>] The function to call. Could be a block
154
+ # instead. Takes nothing and returns a [B=Object].
155
+ # @yield Will yield a block that takes nothing and returns a [B=Object].
156
+ # Same as `f' parameter.
157
+ # @return [B] The value returned from `f`.
158
+ def unwrap_err_or_else(f=nil, &block)
159
+ (f || block).call()
160
+ end
161
+
127
162
  ##
128
163
  # Safely unwrap the `Result`. In the case of `Ok`, this returns the
129
164
  # data in the Ok.
130
165
  #
131
- # @param [B] x The value that will be returned.
166
+ # @param [B] _x The value that will be returned.
132
167
  # @return [A] The data in the Ok.
133
- def unwrap_or(_)
168
+ def unwrap_or(_x)
169
+ @data
170
+ end
171
+
172
+ ##
173
+ # Safely unwrap the `Result`. In the case of `Ok`, this returns the
174
+ # wrapped value.
175
+ #
176
+ # @param f [Proc<B>] A dummy function. Not used.
177
+ # instead. Takes nothing and returns a [B=Object].
178
+ # @return [B] The value returned from `f`.
179
+ def unwrap_or_else(_f=nil, &_block)
134
180
  @data
135
181
  end
136
182
 
data/lib/result.rb CHANGED
@@ -57,17 +57,10 @@ module MonadOxide
57
57
  end
58
58
 
59
59
  ##
60
- # Un-nest this `Result'. This implementation is shared between `Ok' and
61
- # `Err'. In both cases, the structure's data is operated upon.
62
- # @return [Result] If `A' is a `Result' (meaning this `Result` is nested),
63
- # return the inner-most `Result', regardless of the depth of nesting.
64
- # Otherwise return `self'.
65
- def flatten()
66
- if @data.kind_of?(Result)
67
- return @data.flatten()
68
- else
69
- self
70
- end
60
+ # Determine if this is a MonadOxide::Err.
61
+ # @return [Boolean] `true` if this is a MonadOxide::Err, `false` otherwise.
62
+ def err?()
63
+ false
71
64
  end
72
65
 
73
66
  ##
@@ -113,6 +106,20 @@ module MonadOxide
113
106
  Err.new(ResultMethodNotImplementedError.new())
114
107
  end
115
108
 
109
+ ##
110
+ # Un-nest this `Result'. This implementation is shared between `Ok' and
111
+ # `Err'. In both cases, the structure's data is operated upon.
112
+ # @return [Result] If `A' is a `Result' (meaning this `Result` is nested),
113
+ # return the inner-most `Result', regardless of the depth of nesting.
114
+ # Otherwise return `self'.
115
+ def flatten()
116
+ if @data.kind_of?(Result)
117
+ return @data.flatten()
118
+ else
119
+ self
120
+ end
121
+ end
122
+
116
123
  ##
117
124
  # In the case of `Ok', applies `f' or the block over the data and returns
118
125
  # the same `Ok'. No changes are applied. This is ideal for logging. For
@@ -179,6 +186,13 @@ module MonadOxide
179
186
  matcher[self.class].call(@data)
180
187
  end
181
188
 
189
+ ##
190
+ # Determine if this is a MonadOxide::Ok.
191
+ # @return [Boolean] `true` if this is a MonadOxide::Ok, `false` otherwise.
192
+ def ok?()
193
+ false
194
+ end
195
+
182
196
  ##
183
197
  # For `Err', invokes `f' or the block with the data and returns the Result
184
198
  # returned from that. Exceptions raised during `f' or the block will return
@@ -226,6 +240,21 @@ module MonadOxide
226
240
  Err.new(ResultMethodNotImplementedError.new())
227
241
  end
228
242
 
243
+ ##
244
+ # Safely unwrap the `Result` but with lazy evaluation. In the case of
245
+ # `Err`, this returns the wrapped value. For `Ok` the function provided is
246
+ # evaluated and its returned value is what is returned.
247
+ #
248
+ # @param f [Proc<B>] The function to call for `Ok`. Could be a block
249
+ # instead. Takes nothing and returns a [B=Object].
250
+ # @yield Will yield a block for `Ok` that takes nothing and returns a
251
+ # [B=Object]. Same as `f' parameter.
252
+ # @return [T|B] The wrapped value for `Err` and the returned from `f` for
253
+ # `Ok`.
254
+ def unwrap_err_or_else(_f)
255
+ raise ResultMethodNotImplementedError.new()
256
+ end
257
+
229
258
  ##
230
259
  # Attempt to safely access the `Result` data. This always returns a value
231
260
  # instead of raising an Exception. In the case of `Ok`, the data is
@@ -236,5 +265,35 @@ module MonadOxide
236
265
  Err.new(ResultMethodNotImplementedError.new())
237
266
  end
238
267
 
268
+ ##
269
+ # Safely unwrap the `Result` but with lazy evaluation. In the case of
270
+ # `Ok`, this returns the wrapped value. For `Err` the function provided is
271
+ # evaluated and its returned value is what is returned.
272
+ #
273
+ # @param f [Proc<B>] The function to call for `Err`. Could be a block
274
+ # instead. Takes nothing and returns a [B=Object].
275
+ # @yield Will yield a block for `Err` that takes nothing and returns a
276
+ # [B=Object]. Same as `f' parameter.
277
+ # @return [T|B] The wrapped value for `Ok` and the returned from `f` for
278
+ # `Err`.
279
+ def unwrap_or_else(_f)
280
+ raise ResultMethodNotImplementedError.new()
281
+ end
282
+
283
+ ##
284
+ # Safely unwrap the `Result` but with lazy evaluation. In the case of `Ok`,
285
+ # this returns the wrapped value. For `Err` the function provided is
286
+ # evaluated and its returned value is what is returned.
287
+ #
288
+ # @param f [Proc<B>] The function to call for `Err`. Could be a block
289
+ # instead. Takes nothing and returns a [B=Object].
290
+ # @yield Will yield a block for `Err` that takes nothing and returns a
291
+ # [B=Object]. Same as `f' parameter.
292
+ # @return [T|B] The wrapped value for `Ok` and the returned from `f` for
293
+ # `Err`.
294
+ def unwrap_or_else(_f)
295
+ raise ResultMethodNotImplementedError.new()
296
+ end
297
+
239
298
  end
240
299
  end
data/lib/version.rb CHANGED
@@ -3,5 +3,5 @@ module MonadOxide
3
3
  # This version is locked to 0.x.0 because semver is a lie and this project
4
4
  # uses CICD to push new versions. Any commits that appear on main will result
5
5
  # in a new version of the gem created and published.
6
- VERSION='0.8.0'
6
+ VERSION='0.10.0'
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monad-oxide
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Logan Barnett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-17 00:00:00.000000000 Z
11
+ date: 2024-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: org-ruby