monad-oxide 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/err.rb +155 -0
- data/lib/monad-oxide.rb +27 -0
- data/lib/ok.rb +127 -0
- data/lib/result.rb +209 -0
- data/lib/version.rb +7 -0
- metadata +7 -5
- data/lib/err_spec.rb +0 -300
- data/lib/ok_spec.rb +0 -347
- data/lib/result_spec.rb +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9e34a9c5d348e0d022e91919a9d4f8c3c3337cc4040e3c6003a21395db4a553
|
4
|
+
data.tar.gz: 589f27d397f02deddc8e4372fc1ddbf553803e301ed5cff6df176d7e0d740c79
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30d113b3edff4cc76192203e038606d62cdb306fc4baf386b40ef60b6bef1eb6638d5b26c2a2f0496ca990c96a12ae902b8d92c55babcff74de385c24bf99c39
|
7
|
+
data.tar.gz: 58f34dc9b1d67dceb2c229f8f53fce8cf6f309a8625bbe267166840482f77001c511306cada76212b5f7022d6ed1fcffbf05d2d675ae51be186ff26bed16bce9
|
data/lib/err.rb
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
|
2
|
+
module MonadOxide
|
3
|
+
##
|
4
|
+
# Err is the error case for Result.
|
5
|
+
#
|
6
|
+
# Any methods in Result that would process a successful Result (Ok) will fall
|
7
|
+
# through with Err instances.
|
8
|
+
class Err < Result
|
9
|
+
##
|
10
|
+
# Create an Err.
|
11
|
+
#
|
12
|
+
# If the Exception provided was not thrown (ie. created with Exception.new),
|
13
|
+
# it will not have a backtrace. The Err constructor takes care of this by
|
14
|
+
# raising the Exception and immediately capturing the Exception - this
|
15
|
+
# causes the backtrace to be populated and will be availabl to any cosumers
|
16
|
+
# automatically.
|
17
|
+
#
|
18
|
+
# @param data [Exception] This must be an Exception it will result in a
|
19
|
+
# TypeError.
|
20
|
+
# @raise [TypeError] Is raised if the input is not an Exception.
|
21
|
+
def initialize(data)
|
22
|
+
if !data.kind_of?(Exception)
|
23
|
+
raise TypeError.new("#{data.inspect} is not an Exception.")
|
24
|
+
else
|
25
|
+
begin
|
26
|
+
# Ruby Exceptions do not come with a backtrace. During the act of
|
27
|
+
# raising an Exception, that Exception is granted a backtrace. So any
|
28
|
+
# kind of `Exception.new()' invocations will have a `nil' value for
|
29
|
+
# `backtrace()'. To get around this, we can simply raise the Exception
|
30
|
+
# here, and then we get the backtrace we want.
|
31
|
+
#
|
32
|
+
# On a cursory search, this is not documented behavior.
|
33
|
+
if data.backtrace.nil?
|
34
|
+
raise data
|
35
|
+
else
|
36
|
+
@data = data
|
37
|
+
end
|
38
|
+
rescue => e
|
39
|
+
@data = e
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
##
|
45
|
+
# Falls through. @see Result#and_then for how this is handled in either
|
46
|
+
# Result case, and @see Ok.and_then for how this is handled in the Ok case.
|
47
|
+
# @param f [Proc] Optional Proc - ignored.
|
48
|
+
# @yield An ignored block.
|
49
|
+
# @return [Err] This Err.
|
50
|
+
def and_then(f=nil, &block)
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
54
|
+
##
|
55
|
+
# Applies `f' or the block over the `Exception' and returns the same `Err'.
|
56
|
+
# No changes are applied. This is ideal for logging. Exceptions raised
|
57
|
+
# during these transformations will return an `Err' with the Exception.
|
58
|
+
# @param f [Proc<A=Exception>] The function to call. Could be a block
|
59
|
+
# instead. Takes an [A] the return is ignored.
|
60
|
+
# @yield Will yield a block that takes an A the return is ignored. Same as
|
61
|
+
# `f' parameter.
|
62
|
+
# @return [Result<A, E>] returns self.
|
63
|
+
def inspect_err(f=nil, &block)
|
64
|
+
begin
|
65
|
+
(f || block).call(@data)
|
66
|
+
self
|
67
|
+
rescue => e
|
68
|
+
self.class.new(e)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
##
|
73
|
+
# Falls through. @see Result#inspect_ok for how this is handled in either
|
74
|
+
# Result case, and @see Ok.inspect_ok for how this is handled in the Ok
|
75
|
+
# case.
|
76
|
+
# @param f [Proc] Optional Proc - ignored.
|
77
|
+
# @yield An ignored block.
|
78
|
+
# @return [Err] This Err.
|
79
|
+
def inspect_ok(f=nil, &block)
|
80
|
+
self
|
81
|
+
end
|
82
|
+
|
83
|
+
##
|
84
|
+
# Falls through. @see Result#map for how this is handled in either
|
85
|
+
# Result case, and @see Ok.map for how this is handled in the Ok case.
|
86
|
+
# @param f [Proc] Optional Proc - ignored.
|
87
|
+
# @yield An ignored block.
|
88
|
+
# @return [Err] This Err.
|
89
|
+
def map(f=nil, &block)
|
90
|
+
self
|
91
|
+
end
|
92
|
+
|
93
|
+
##
|
94
|
+
# Applies `f' or the block over the data and returns a new new `Err' with
|
95
|
+
# the returned value.
|
96
|
+
# @param f [Proc<A, B>] The function to call. Could be a block
|
97
|
+
# instead. Takes an [A=Object] and returns a B.
|
98
|
+
# @yield Will yield a block that takes an A and returns an Err<B>. Same as
|
99
|
+
# `f' parameter.
|
100
|
+
# @return [Result<B>] A new `Err<B>' whose `B' is the return of `f' or the
|
101
|
+
# block. Errors raised when applying `f' or the block will result in
|
102
|
+
# a returned `Err<Exception>'.
|
103
|
+
def map_err(f=nil, &block)
|
104
|
+
begin
|
105
|
+
self.class.new((f || block).call(@data))
|
106
|
+
rescue => e
|
107
|
+
self.class.new(e)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
##
|
112
|
+
# Invokes `f' or the block with the data and returns the Result returned
|
113
|
+
# from that. Exceptions raised during `f' or the block will return an
|
114
|
+
# `Err<Exception>'. The return type is enforced.
|
115
|
+
# @param f [Proc<A, Result<B>>] The function to call. Could be a block
|
116
|
+
# instead. Takes an [A=Object] and must return a [Result<B>].
|
117
|
+
# @yield Will yield a block that takes an A and returns a Result<B>. Same as
|
118
|
+
# `f' parameter.
|
119
|
+
# @return [Err<C> | Err<Exception>] A new Result from `f' or the block.
|
120
|
+
# Exceptions raised will result in `Err<Exception>'. If `f' returns
|
121
|
+
# a non-Result, this will return `Err<ResultReturnExpectedError>'.
|
122
|
+
def or_else(f=nil, &block)
|
123
|
+
begin
|
124
|
+
r = (f || block).call(@data)
|
125
|
+
# Enforce that we always get a Result. Without a Result, coerce to an
|
126
|
+
# Err.
|
127
|
+
if !r.kind_of?(Result)
|
128
|
+
raise ResultReturnExpectedError.new(r)
|
129
|
+
else
|
130
|
+
r
|
131
|
+
end
|
132
|
+
rescue => e
|
133
|
+
Err.new(e)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
##
|
138
|
+
# Dangerously try to access the `Result' data. If this is an `Err', an
|
139
|
+
# exception will be raised. It is recommended to use this for tests only.
|
140
|
+
# @return [A] The inner data of this `Result'.
|
141
|
+
def unwrap()
|
142
|
+
raise UnwrapError.new(
|
143
|
+
"#{self.class} with #{@data.inspect} could not be unwrapped as an Ok.",
|
144
|
+
)
|
145
|
+
end
|
146
|
+
|
147
|
+
##
|
148
|
+
# Dangerously access the `Err' data. If this is an `Ok', an exception will
|
149
|
+
# be raised. It is recommended to use this for tests only.
|
150
|
+
# @return [E] The `Exception' of this `Err'.
|
151
|
+
def unwrap_err()
|
152
|
+
@data
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
data/lib/monad-oxide.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative './result'
|
2
|
+
require_relative './err'
|
3
|
+
require_relative './ok'
|
4
|
+
require_relative './version'
|
5
|
+
|
6
|
+
##
|
7
|
+
# The top level module for the monad-oxide library. Of interest, @see `Result',
|
8
|
+
# @see `Err', and @see `Ok'.
|
9
|
+
module MonadOxide
|
10
|
+
module_function
|
11
|
+
|
12
|
+
##
|
13
|
+
# Create an `Err' as a conveniece method.
|
14
|
+
# @param data [Exception] The `Exception' for this `Err'.
|
15
|
+
# @return [Result<A, E=Exception>] the `Err' from the provided `Exception'.
|
16
|
+
def err(data)
|
17
|
+
MonadOxide::Err.new(data)
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# Create an `Ok' as a conveniece method.
|
22
|
+
# @param data [Object] The inner data for this `Ok'.
|
23
|
+
# @return [Result<A, E=Exception>] the `Ok' from the provided inner data.
|
24
|
+
def ok(data)
|
25
|
+
MonadOxide::Ok.new(data)
|
26
|
+
end
|
27
|
+
end
|
data/lib/ok.rb
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
require_relative './result'
|
2
|
+
|
3
|
+
module MonadOxide
|
4
|
+
##
|
5
|
+
# `Ok' represents a success `Result'. For most operations, `Ok' will perform
|
6
|
+
# some operation. Exceptions raised during calls to `Ok' will coerce the chain
|
7
|
+
# into `Err', which generally causes execution to fall through the entire
|
8
|
+
# chain.
|
9
|
+
class Ok < Result
|
10
|
+
##
|
11
|
+
# Constructs an `Ok' with the data provided.
|
12
|
+
# @param data [Object] The inner data this Result encapsulates.
|
13
|
+
def initialize(data)
|
14
|
+
@data = data
|
15
|
+
end
|
16
|
+
|
17
|
+
##
|
18
|
+
# Invokes `f' or the block with the data and returns the Result returned
|
19
|
+
# from that. Exceptions raised during `f' or the block will return an
|
20
|
+
# `Err<Exception>'. The return type is enforced.
|
21
|
+
# @param f [Proc<A, Result<B>>] The function to call. Could be a block
|
22
|
+
# instead. Takes an [A=Object] and must return a [Result<B>].
|
23
|
+
# @yield Will yield a block that takes an A and returns a Result<B>. Same as
|
24
|
+
# `f' parameter.
|
25
|
+
# @return [Ok<B> | Err<C>] A new Result from `f' or the block. Exceptions
|
26
|
+
# raised will result in `Err<C>'. If `f' returns a non-Result, this
|
27
|
+
# will return `Err<ResultReturnExpectedError>'.
|
28
|
+
def and_then(f=nil, &block)
|
29
|
+
begin
|
30
|
+
r = (f || block).call(@data)
|
31
|
+
# Enforce that we always get a Result. Without a Result, coerce to an
|
32
|
+
# Err.
|
33
|
+
if !r.kind_of?(Result)
|
34
|
+
raise ResultReturnExpectedError.new(r)
|
35
|
+
else
|
36
|
+
r
|
37
|
+
end
|
38
|
+
rescue => e
|
39
|
+
Err.new(e)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
##
|
44
|
+
# Falls through. @see Result#inspect_err for how this is handled in either
|
45
|
+
# Result case, and @see Err.inspect_err for how this is handled in the Err
|
46
|
+
# case.
|
47
|
+
# @param f [Proc] Optional Proc - ignored.
|
48
|
+
# @yield An ignored block.
|
49
|
+
# @return [Ok] This Ok.
|
50
|
+
def inspect_err(f=nil, &block)
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
54
|
+
##
|
55
|
+
# Applies `f' or the block over the data and returns the same `Ok'. No
|
56
|
+
# changes are applied. This is ideal for logging. Exceptions raised during
|
57
|
+
# these transformations will return an `Err' with the Exception.
|
58
|
+
# @param f [Proc<A, B>] The function to call. Could be a block instead.
|
59
|
+
# Takes an [A] the return is ignored.
|
60
|
+
# @yield Will yield a block that takes an A the return is ignored. Same as
|
61
|
+
# `f' parameter.
|
62
|
+
# @return [Result<A>] returns self.
|
63
|
+
def inspect_ok(f=nil, &block)
|
64
|
+
begin
|
65
|
+
(f || block).call(@data)
|
66
|
+
self
|
67
|
+
rescue => e
|
68
|
+
Err.new(e)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
##
|
73
|
+
# Applies `f' or the block over the data and returns a new new `Ok' with the
|
74
|
+
# returned value.
|
75
|
+
# @param f [Proc<A, B>] The function to call. Could be a block
|
76
|
+
# instead. Takes an [A=Object] and returns a B.
|
77
|
+
# @yield Will yield a block that takes an A and returns a B. Same as
|
78
|
+
# `f' parameter.
|
79
|
+
# @return [Result<B>] A new `Ok<B>' whose `B' is the return of `f' or the
|
80
|
+
# block. Errors raised when applying `f' or the block will result in
|
81
|
+
# a returned `Err<Exception>'.
|
82
|
+
def map(f=nil, &block)
|
83
|
+
begin
|
84
|
+
self.class.new((f || block).call(@data))
|
85
|
+
rescue => e
|
86
|
+
Err.new(e)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
##
|
91
|
+
# This is a no-op for Ok. @see Err#map_err.
|
92
|
+
# @param f [Proc<A, B>] A dummy function. Not used.
|
93
|
+
# @yield A dummy block. Not used.
|
94
|
+
# @return [Result<A>] This `Ok'.
|
95
|
+
def map_err(f=nil, &block)
|
96
|
+
self
|
97
|
+
end
|
98
|
+
|
99
|
+
##
|
100
|
+
# The Err equivalent to Ok#and_then. This is a no-op for Ok. @see
|
101
|
+
# Err#or_else.
|
102
|
+
# @param f [Proc<A, B>] A dummy function. Not used.
|
103
|
+
# @yield A dummy block. Not used.
|
104
|
+
# @return [Result<A>] This `Ok'.
|
105
|
+
def or_else(f=nil, &block)
|
106
|
+
self
|
107
|
+
end
|
108
|
+
|
109
|
+
##
|
110
|
+
# Dangerously access the `Ok' data. If this is an `Err', an exception will
|
111
|
+
# be raised. It is recommended to use this for tests only.
|
112
|
+
# @return [A] The inner data of this `Ok'.
|
113
|
+
def unwrap()
|
114
|
+
@data
|
115
|
+
end
|
116
|
+
|
117
|
+
##
|
118
|
+
# Dangerously access the `Err' data. If this is an `Ok', an exception will
|
119
|
+
# be raised. It is recommended to use this for tests only.
|
120
|
+
# @return [E] The `Exception' of this `Err'.
|
121
|
+
def unwrap_err()
|
122
|
+
raise UnwrapError.new(
|
123
|
+
"#{self.class} with #{@data.inspect} could not be unwrapped as an Err.",
|
124
|
+
)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
data/lib/result.rb
ADDED
@@ -0,0 +1,209 @@
|
|
1
|
+
module MonadOxide
|
2
|
+
##
|
3
|
+
# All errors in monad-oxide should inherit from this error. This makes it easy
|
4
|
+
# to handle library-wide errors on the consumer side.
|
5
|
+
class MonadOxideError < StandardError; end
|
6
|
+
|
7
|
+
##
|
8
|
+
# Thie Exception signals an area under construction, or somehow the consumer
|
9
|
+
# wound up with a `Result' and not one of its subclasses (@see Ok and @see
|
10
|
+
# Err). Implementors of new methods on `Ok' and `Err' should create methods on
|
11
|
+
# `Result' as well which immediately raise this `Exception'.
|
12
|
+
class ResultMethodNotImplementedError < MonadOxideError; end
|
13
|
+
|
14
|
+
##
|
15
|
+
# This `Exception' is produced when a method that expects the function or
|
16
|
+
# block to provide a `Result' but was given something else. Generally this
|
17
|
+
# Exception is not raised, but instead converts the Result into a an Err.
|
18
|
+
# Example methods with this behavior are Result#and_then and Result#or_else.
|
19
|
+
class ResultReturnExpectedError < MonadOxideError
|
20
|
+
##
|
21
|
+
# The transformation expected a `Result' but got something else.
|
22
|
+
# @param data [Object] Whatever we got that wasn't a `Result'.
|
23
|
+
def initialize(data)
|
24
|
+
super("A Result was expected but got #{data.inspect}.")
|
25
|
+
data = @data
|
26
|
+
end
|
27
|
+
attr_reader(:data)
|
28
|
+
end
|
29
|
+
|
30
|
+
##
|
31
|
+
# This `Exception' is raised when the consumer makes a dangerous wager
|
32
|
+
# about which state the `Result' is in, and lost. More specifically: An `Ok'
|
33
|
+
# cannot unwrap an Exception, and an `Err' cannot unwrap the `Ok' data.
|
34
|
+
class UnwrapError < MonadOxideError; end
|
35
|
+
|
36
|
+
##
|
37
|
+
# A Result is a chainable series of sequential transformations. The Result
|
38
|
+
# structure contains an `Ok<A> | Err<Exception>`. This is the central location
|
39
|
+
# for documentation between both `Ok` and `Err`. It is best to think of any
|
40
|
+
# given `Ok` or `Err` as a `Result` instead. All methods on `Ok` are present
|
41
|
+
# on `Err` and vice versa. This way we can interchange one for the other
|
42
|
+
# during virtually any call.
|
43
|
+
#
|
44
|
+
# This is a base-class only, and you should never see instances of these in
|
45
|
+
# the wild.
|
46
|
+
class Result
|
47
|
+
# None of these work.
|
48
|
+
# Option 1:
|
49
|
+
# private_class_method :new
|
50
|
+
# Option 2:
|
51
|
+
# class << self
|
52
|
+
# protected :new
|
53
|
+
# end
|
54
|
+
|
55
|
+
def initialize(data)
|
56
|
+
raise NoMethodError.new('Do not use Result directly. See Ok and Err.')
|
57
|
+
end
|
58
|
+
|
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
|
71
|
+
end
|
72
|
+
|
73
|
+
##
|
74
|
+
# For `Ok', invokes `f' or the block with the data and returns the Result
|
75
|
+
# returned from that. Exceptions raised during `f' or the block will return
|
76
|
+
# an `Err<Exception>'.
|
77
|
+
#
|
78
|
+
# For `Err', returns itself and the function/block are ignored.
|
79
|
+
#
|
80
|
+
# This method is used for control flow based on `Result' values.
|
81
|
+
#
|
82
|
+
# `and_then' is desirable for chaining together other Result based
|
83
|
+
# operations, or doing transformations where flipping from an `Ok' to an
|
84
|
+
# `Err' is desired. In cases where there is little/no risk of an `Err'
|
85
|
+
# state, @see Result#map.
|
86
|
+
#
|
87
|
+
# The `Err' equivalent operation is @see Result#or_else.
|
88
|
+
#
|
89
|
+
# The return type is enforced.
|
90
|
+
#
|
91
|
+
# @param f [Proc<A, Result<B>>] The function to call. Could be a block
|
92
|
+
# instead. Takes an [A=Object] and must return a [Result<B>].
|
93
|
+
# @yield Will yield a block that takes an A and returns a Result<B>. Same as
|
94
|
+
# `f' parameter.
|
95
|
+
# @return [Ok<B> | Err<C>] A new Result from `f' or the block. Exceptions
|
96
|
+
# raised will result in `Err<C>'. If `f' returns a non-Result, this
|
97
|
+
# will return `Err<ResultReturnExpectedError>'.
|
98
|
+
def and_then(f=nil, &block)
|
99
|
+
Err.new(ResultMethodNotImplementedError.new())
|
100
|
+
end
|
101
|
+
|
102
|
+
##
|
103
|
+
# In the case of `Ok', applies `f' or the block over the data and returns a
|
104
|
+
# new new `Ok' with the returned value. For `Err', this method falls
|
105
|
+
# through. Exceptions raised during these transformations will return an
|
106
|
+
# `Err' with the Exception.
|
107
|
+
# @param f [Proc<A, B>] The function to call. Could be a block
|
108
|
+
# instead. Takes an [A=Object] and returns a B.
|
109
|
+
# @yield Will yield a block that takes an A and returns a B. Same as
|
110
|
+
# `f' parameter.
|
111
|
+
# @return [Result<B>] A new `Result<B>' whose `B' is the return of `f' or
|
112
|
+
# the block. Errors raised when applying `f' or the block will
|
113
|
+
# result in a returned `Err<Exception>'.
|
114
|
+
def map(f=nil, &block)
|
115
|
+
Err.new(ResultMethodNotImplementedError.new())
|
116
|
+
end
|
117
|
+
|
118
|
+
##
|
119
|
+
# In the case of `Err', applies `f' or the block over the `Exception' and
|
120
|
+
# returns a new new `Err' with the returned value. For `Ok', this method
|
121
|
+
# falls through. Exceptions raised during these transformations will return
|
122
|
+
# an `Err' with the Exception.
|
123
|
+
# @param f [Proc<A, B>] The function to call. Could be a block
|
124
|
+
# instead. Takes an [A=Exception] and returns a [B=Exception].
|
125
|
+
# @yield Will yield a block that takes an A and returns a B. Same as
|
126
|
+
# `f' parameter.
|
127
|
+
# @return [Result<B>] A new `Result<A, ErrorB>' whose `ErrorB' is the return
|
128
|
+
# of `f' or the block. Errors raised when applying `f' or the block
|
129
|
+
# will result in a returned `Err<Exception>'.
|
130
|
+
def map_err(f=nil, &block)
|
131
|
+
Err.new(ResultMethodNotImplementedError.new())
|
132
|
+
end
|
133
|
+
|
134
|
+
##
|
135
|
+
# In the case of `Err', applies `f' or the block over the `Exception' and
|
136
|
+
# returns the same `Err'. No changes are applied. This is ideal for logging.
|
137
|
+
# For `Ok', this method falls through. Exceptions raised during these
|
138
|
+
# transformations will return an `Err' with the Exception.
|
139
|
+
# @param f [Proc<A, B>] The function to call. Could be a block instead.
|
140
|
+
# Takes an [A=Exception] the return is ignored.
|
141
|
+
# @yield Will yield a block that takes an A the return is ignored. Same as
|
142
|
+
# `f' parameter.
|
143
|
+
# @return [Result<A>] returns self.
|
144
|
+
def inspect_err(f=nil, &block)
|
145
|
+
Err.new(ResultMethodNotImplementedError.new())
|
146
|
+
end
|
147
|
+
|
148
|
+
##
|
149
|
+
# In the case of `Ok', applies `f' or the block over the data and returns
|
150
|
+
# the same `Ok'. No changes are applied. This is ideal for logging. For
|
151
|
+
# `Err', this method falls through. Exceptions raised during these
|
152
|
+
# transformations will return an `Err' with the Exception.
|
153
|
+
# @param f [Proc<A, B>] The function to call. Could be a block instead.
|
154
|
+
# Takes an [A] the return is ignored.
|
155
|
+
# @yield Will yield a block that takes an A the return is ignored. Same as
|
156
|
+
# `f' parameter.
|
157
|
+
# @return [Result<A>] returns self.
|
158
|
+
def inspect_ok(f=nil, &block)
|
159
|
+
Err.new(ResultMethodNotImplementedError.new())
|
160
|
+
end
|
161
|
+
|
162
|
+
##
|
163
|
+
# For `Err', invokes `f' or the block with the data and returns the Result
|
164
|
+
# returned from that. Exceptions raised during `f' or the block will return
|
165
|
+
# an `Err<Exception>'.
|
166
|
+
#
|
167
|
+
# For `Ok', returns itself and the function/block are ignored.
|
168
|
+
#
|
169
|
+
# This method is used for control flow based on `Result' values.
|
170
|
+
#
|
171
|
+
# `or_else' is desirable for chaining together other Result based
|
172
|
+
# operations, or doing transformations where flipping from an `Ok' to an
|
173
|
+
# `Err' is desired. In cases where there is little/no risk of an `Err'
|
174
|
+
# state, @see Result#map.
|
175
|
+
#
|
176
|
+
# The `Ok' equivalent operation is @see Result#and_then.
|
177
|
+
#
|
178
|
+
# The return type is enforced.
|
179
|
+
#
|
180
|
+
# @param f [Proc<A, Result<B>>] The function to call. Could be a block
|
181
|
+
# instead. Takes an [A=Object] and must return a [Result<B>].
|
182
|
+
# @yield Will yield a block that takes an A and returns a Result<B>. Same as
|
183
|
+
# `f' parameter.
|
184
|
+
# @return [Ok<B> | Err<C>] A new Result from `f' or the block. Exceptions
|
185
|
+
# raised will result in `Err<C>'. If `f' returns a non-Result, this
|
186
|
+
# will return `Err<ResultReturnExpectedError>'.
|
187
|
+
def or_else(f=nil, &block)
|
188
|
+
Err.new(ResultMethodNotImplementedError.new())
|
189
|
+
end
|
190
|
+
|
191
|
+
##
|
192
|
+
# Dangerously access the `Result' data. If this is an `Err', an exception
|
193
|
+
# will be raised. It is recommended to use this for tests only.
|
194
|
+
# @raise [UnwrapError] if called on an `Err'.
|
195
|
+
# @return [A] - The inner data of this `Ok'.
|
196
|
+
def unwrap()
|
197
|
+
Err.new(ResultMethodNotImplementedError.new())
|
198
|
+
end
|
199
|
+
|
200
|
+
##
|
201
|
+
# Dangerously access the `Result' data. If this is an `Ok', an exception
|
202
|
+
# will be raised. It is recommended to use this for tests only.
|
203
|
+
# @raise [UnwrapError] if called on an `Ok'.
|
204
|
+
# @return [E] - The inner data of this `Err'.
|
205
|
+
def unwrap_err()
|
206
|
+
Err.new(ResultMethodNotImplementedError.new())
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
data/lib/version.rb
ADDED
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.
|
4
|
+
version: 0.3.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: 2022-07-
|
11
|
+
date: 2022-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: org-ruby
|
@@ -75,9 +75,11 @@ executables: []
|
|
75
75
|
extensions: []
|
76
76
|
extra_rdoc_files: []
|
77
77
|
files:
|
78
|
-
- lib/
|
79
|
-
- lib/
|
80
|
-
- lib/
|
78
|
+
- lib/err.rb
|
79
|
+
- lib/monad-oxide.rb
|
80
|
+
- lib/ok.rb
|
81
|
+
- lib/result.rb
|
82
|
+
- lib/version.rb
|
81
83
|
homepage:
|
82
84
|
licenses:
|
83
85
|
- Apache-2.0
|
data/lib/err_spec.rb
DELETED
@@ -1,300 +0,0 @@
|
|
1
|
-
require 'monad-oxide'
|
2
|
-
require 'ok'
|
3
|
-
require 'err'
|
4
|
-
|
5
|
-
describe MonadOxide::Err do
|
6
|
-
|
7
|
-
# The constructor tests (MonadOxide::Err.new()) are exactly the same as the
|
8
|
-
# helper (MonadOxide.err()).
|
9
|
-
ctor_tests = ->(ctor) {
|
10
|
-
it 'raises a TypeError if not provided an Exception' do
|
11
|
-
expect { ctor.call('foo') }.to raise_error(TypeError)
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'establishes a backtrace for unraised Exceptions' do
|
15
|
-
expect(
|
16
|
-
ctor.call(StandardError.new('foo'))
|
17
|
-
.unwrap_err()
|
18
|
-
.backtrace()
|
19
|
-
).not_to(be_nil())
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'retains backtraces for raised Exceptions' do
|
23
|
-
begin
|
24
|
-
raise StandardError.new('foo')
|
25
|
-
rescue => e
|
26
|
-
expect(
|
27
|
-
ctor.call(e)
|
28
|
-
.unwrap_err()
|
29
|
-
.backtrace()
|
30
|
-
).to(be(e.backtrace()))
|
31
|
-
end
|
32
|
-
end
|
33
|
-
}
|
34
|
-
|
35
|
-
context '#initialize' do
|
36
|
-
ctor_tests.call(MonadOxide::Err.method(:new))
|
37
|
-
end
|
38
|
-
|
39
|
-
context 'MonadOxide.err' do
|
40
|
-
ctor_tests.call(MonadOxide.method(:err))
|
41
|
-
end
|
42
|
-
|
43
|
-
context '#and_then' do
|
44
|
-
|
45
|
-
context 'with blocks' do
|
46
|
-
it 'does nothing with the block' do
|
47
|
-
effected = 'unset'
|
48
|
-
MonadOxide.err(StandardError.new('foo'))
|
49
|
-
.and_then() {|s| effected = "effect: #{s}"}
|
50
|
-
expect(effected).not_to(eq('effect: foo'))
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'does not change the underlying data' do
|
54
|
-
expect(
|
55
|
-
MonadOxide.err(StandardError.new('foo'))
|
56
|
-
.and_then() { 'bar' }
|
57
|
-
.unwrap_err()
|
58
|
-
.message()
|
59
|
-
).to(eq('foo'))
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
context 'with Procs' do
|
64
|
-
it 'does nothing with the function' do
|
65
|
-
effected = 'unset'
|
66
|
-
MonadOxide.err(StandardError.new('foo'))
|
67
|
-
.and_then(->(s) { effected = "effect: #{s}"})
|
68
|
-
expect(effected).not_to(eq('effect: foo'))
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'does not change the underlying data' do
|
72
|
-
expect(
|
73
|
-
MonadOxide.err(StandardError.new('foo'))
|
74
|
-
.and_then(->(_) { 'bar' })
|
75
|
-
.unwrap_err()
|
76
|
-
.message()
|
77
|
-
).to(eq('foo'))
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
end
|
82
|
-
|
83
|
-
|
84
|
-
context '#inspect_err' do
|
85
|
-
context 'with blocks' do
|
86
|
-
it 'applies the data to the block provided' do
|
87
|
-
effected = 'unset'
|
88
|
-
MonadOxide.err(StandardError.new('foo'))
|
89
|
-
.inspect_err(->(s) { effected = "effect: #{s}"})
|
90
|
-
expect(effected).to(eq('effect: foo'))
|
91
|
-
end
|
92
|
-
|
93
|
-
it 'does not change the underlying data' do
|
94
|
-
expect(
|
95
|
-
MonadOxide.err(StandardError.new('foo'))
|
96
|
-
.inspect_err() {|_| 'bar' }
|
97
|
-
.unwrap_err()
|
98
|
-
.message()
|
99
|
-
).to(eq('foo'))
|
100
|
-
end
|
101
|
-
|
102
|
-
it 'returns an Err with the error from the block' do
|
103
|
-
expect(
|
104
|
-
MonadOxide.err(StandardError.new('foo'))
|
105
|
-
.inspect_err() {|_| raise StandardError.new('flagrant') }
|
106
|
-
.unwrap_err()
|
107
|
-
.message()
|
108
|
-
).to(eq('flagrant'))
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
context 'with Procs' do
|
113
|
-
it 'applies the data to the function provided' do
|
114
|
-
effected = 'unset'
|
115
|
-
MonadOxide.err(StandardError.new('foo'))
|
116
|
-
.inspect_err(->(s) { effected = "effect: #{s}"})
|
117
|
-
expect(effected).to(eq('effect: foo'))
|
118
|
-
end
|
119
|
-
|
120
|
-
it 'does not change the underlying data' do
|
121
|
-
expect(
|
122
|
-
MonadOxide.err(StandardError.new('foo'))
|
123
|
-
.inspect_err(->(_) { 'bar' })
|
124
|
-
.unwrap_err()
|
125
|
-
.message()
|
126
|
-
).to(eq('foo'))
|
127
|
-
end
|
128
|
-
|
129
|
-
it 'returns an Err with the error from the function' do
|
130
|
-
expect(
|
131
|
-
MonadOxide.err(StandardError.new('foo'))
|
132
|
-
.inspect_err(->(_) { raise StandardError.new('flagrant') })
|
133
|
-
.unwrap_err()
|
134
|
-
.message()
|
135
|
-
).to(eq('flagrant'))
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
context '#inspect_ok' do
|
141
|
-
context 'with blocks' do
|
142
|
-
it 'does nothing with the block' do
|
143
|
-
effected = 'unset'
|
144
|
-
MonadOxide.err(StandardError.new('foo'))
|
145
|
-
.inspect_ok() {|s| effected = "effect: #{s}"}
|
146
|
-
expect(effected).not_to(eq('effect: foo'))
|
147
|
-
end
|
148
|
-
|
149
|
-
it 'does not change the underlying data' do
|
150
|
-
expect(
|
151
|
-
MonadOxide.err(StandardError.new('foo'))
|
152
|
-
.inspect_ok() { 'bar' }
|
153
|
-
.unwrap_err()
|
154
|
-
.message()
|
155
|
-
).to(eq('foo'))
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
context 'with Procs' do
|
160
|
-
it 'does nothing with the function' do
|
161
|
-
effected = 'unset'
|
162
|
-
MonadOxide.err(StandardError.new('foo'))
|
163
|
-
.inspect_ok(->(s) { effected = "effect: #{s}"})
|
164
|
-
expect(effected).not_to(eq('effect: foo'))
|
165
|
-
end
|
166
|
-
|
167
|
-
it 'does not change the underlying data' do
|
168
|
-
expect(
|
169
|
-
MonadOxide.err(StandardError.new('foo'))
|
170
|
-
.inspect_ok(->(_) { 'bar' })
|
171
|
-
.unwrap_err()
|
172
|
-
.message()
|
173
|
-
).to(eq('foo'))
|
174
|
-
end
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
|
179
|
-
context '#map' do
|
180
|
-
context 'with blocks' do
|
181
|
-
it 'does nothing with the block' do
|
182
|
-
effected = 'unset'
|
183
|
-
MonadOxide.err(StandardError.new('foo'))
|
184
|
-
.map() {|s| effected = "effect: #{s}"}
|
185
|
-
expect(effected).not_to(eq('effect: foo'))
|
186
|
-
end
|
187
|
-
|
188
|
-
it 'does not change the underlying data' do
|
189
|
-
expect(
|
190
|
-
MonadOxide.err(StandardError.new('foo'))
|
191
|
-
.map() { 'bar' }
|
192
|
-
.unwrap_err()
|
193
|
-
.message()
|
194
|
-
).to(eq('foo'))
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
context 'with Procs' do
|
199
|
-
it 'does nothing with the function' do
|
200
|
-
effected = 'unset'
|
201
|
-
MonadOxide.err(StandardError.new('foo'))
|
202
|
-
.map(->(s) { effected = "effect: #{s}"})
|
203
|
-
expect(effected).not_to(eq('effect: foo'))
|
204
|
-
end
|
205
|
-
|
206
|
-
it 'does not change the underlying data' do
|
207
|
-
expect(
|
208
|
-
MonadOxide.err(StandardError.new('foo'))
|
209
|
-
.map(->(_) { 'bar' })
|
210
|
-
.unwrap_err()
|
211
|
-
.message()
|
212
|
-
).to(eq('foo'))
|
213
|
-
end
|
214
|
-
end
|
215
|
-
end
|
216
|
-
|
217
|
-
context '#map_err' do
|
218
|
-
context 'with blocks' do
|
219
|
-
it 'returns an Err if an error is raised in the block' do
|
220
|
-
expect(
|
221
|
-
MonadOxide.err(StandardError.new('foo'))
|
222
|
-
.map_err() {|_| raise StandardError.new('bar') }
|
223
|
-
.unwrap_err()
|
224
|
-
.message()
|
225
|
-
).to(eq('bar'))
|
226
|
-
end
|
227
|
-
|
228
|
-
it 'returns a new Err' do
|
229
|
-
expect(
|
230
|
-
MonadOxide.err(StandardError.new('foo'))
|
231
|
-
.map_err() {|_| StandardErr.new('bar') }
|
232
|
-
.class()
|
233
|
-
).to(be(MonadOxide::Err))
|
234
|
-
end
|
235
|
-
|
236
|
-
it 'applies the block to the data for the new Ok' do
|
237
|
-
expect(
|
238
|
-
MonadOxide.err(StandardError.new('foo'))
|
239
|
-
.map_err() {|e| StandardError.new(e.message() + 'bar') }
|
240
|
-
.unwrap_err()
|
241
|
-
.message()
|
242
|
-
).to(eq('foobar'))
|
243
|
-
end
|
244
|
-
|
245
|
-
it 'requires the mapped value is an Exception still' do
|
246
|
-
expect(
|
247
|
-
MonadOxide.err(StandardError.new('foo'))
|
248
|
-
.map_err() {|_| 'bar'}
|
249
|
-
.unwrap_err()
|
250
|
-
.class()
|
251
|
-
).to(be(TypeError))
|
252
|
-
end
|
253
|
-
end
|
254
|
-
|
255
|
-
context 'with Procs' do
|
256
|
-
it 'returns an Err if an error is raised in the function' do
|
257
|
-
expect(
|
258
|
-
MonadOxide.err(StandardError.new('foo'))
|
259
|
-
.map_err(->(_) { raise StandardError.new })
|
260
|
-
.unwrap_err()
|
261
|
-
.class()
|
262
|
-
).to(be(StandardError))
|
263
|
-
end
|
264
|
-
|
265
|
-
it 'applies the function to the data for the new Ok' do
|
266
|
-
expect(
|
267
|
-
MonadOxide.err(StandardError.new('foo'))
|
268
|
-
.map_err(->(e) { StandardError.new(e.message() + 'bar') })
|
269
|
-
.unwrap_err()
|
270
|
-
.message()
|
271
|
-
).to(eq('foobar'))
|
272
|
-
end
|
273
|
-
|
274
|
-
it 'requires the mapped value is an Exception still' do
|
275
|
-
expect(
|
276
|
-
MonadOxide.err(StandardError.new('foo'))
|
277
|
-
.map_err(->(_) { 'bar' })
|
278
|
-
.unwrap_err()
|
279
|
-
.class()
|
280
|
-
).to(be(TypeError))
|
281
|
-
end
|
282
|
-
end
|
283
|
-
end
|
284
|
-
|
285
|
-
context '#unwrap' do
|
286
|
-
it 'raises an UnwrapError' do
|
287
|
-
expect {
|
288
|
-
MonadOxide.err(StandardError.new('foo')).unwrap()
|
289
|
-
}.to(raise_error(MonadOxide::UnwrapError))
|
290
|
-
end
|
291
|
-
end
|
292
|
-
|
293
|
-
context '#unwrap_err' do
|
294
|
-
it 'provides the underlying value' do
|
295
|
-
expect(
|
296
|
-
MonadOxide.err(StandardError.new('foo')).unwrap_err().message(),
|
297
|
-
).to(eq('foo'))
|
298
|
-
end
|
299
|
-
end
|
300
|
-
end
|
data/lib/ok_spec.rb
DELETED
@@ -1,347 +0,0 @@
|
|
1
|
-
require 'monad-oxide'
|
2
|
-
require 'ok'
|
3
|
-
require 'err'
|
4
|
-
|
5
|
-
describe MonadOxide::Ok do
|
6
|
-
|
7
|
-
context '#and_then' do
|
8
|
-
|
9
|
-
context 'with blocks' do
|
10
|
-
it 'passes the data from the Ok to the block' do
|
11
|
-
expect(
|
12
|
-
MonadOxide.ok('foo')
|
13
|
-
.and_then() {|s| MonadOxide.ok(s + 'bar') }
|
14
|
-
.unwrap()
|
15
|
-
).to(eq('foobar'))
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'converts to an Err if the block raises an error' do
|
19
|
-
expect(
|
20
|
-
MonadOxide.ok('foo')
|
21
|
-
.and_then() {|_| raise StandardError.new('flagrant') }
|
22
|
-
.class
|
23
|
-
).to(be(MonadOxide::Err))
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'converts to an Err if the block returns a non-Result' do
|
27
|
-
expect(
|
28
|
-
MonadOxide.ok('foo')
|
29
|
-
.and_then() {|_| 'bar' }
|
30
|
-
.class()
|
31
|
-
).to(be(MonadOxide::Err))
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'provides a ResultReturnExpectedError in the Err for non-Result returns' do
|
35
|
-
expect(
|
36
|
-
MonadOxide.ok('foo')
|
37
|
-
.and_then() {|_| 'bar' }
|
38
|
-
.unwrap_err()
|
39
|
-
.class()
|
40
|
-
).to(be(MonadOxide::ResultReturnExpectedError))
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'returns the same Ok returned by the block' do
|
44
|
-
expect(
|
45
|
-
MonadOxide.ok('foo')
|
46
|
-
.and_then() {|_| MonadOxide.ok('bar') }
|
47
|
-
.unwrap()
|
48
|
-
).to(eq('bar'))
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'returns the same Err returned by the block' do
|
52
|
-
expect(
|
53
|
-
MonadOxide.ok('foo')
|
54
|
-
.and_then() {|_| MonadOxide.err(StandardError.new()) }
|
55
|
-
.unwrap_err()
|
56
|
-
.class()
|
57
|
-
).to(be(StandardError))
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
context 'with Procs' do
|
62
|
-
it 'passes the data from the Ok to the function' do
|
63
|
-
expect(
|
64
|
-
MonadOxide.ok('foo')
|
65
|
-
.and_then(->(s) { MonadOxide.ok(s + 'bar') })
|
66
|
-
.unwrap()
|
67
|
-
).to(eq('foobar'))
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'converts to an Err if the function raises an error' do
|
71
|
-
expect(
|
72
|
-
MonadOxide.ok('foo')
|
73
|
-
.and_then(->(_) { raise StandardError.new('flagrant') })
|
74
|
-
.class
|
75
|
-
).to(be(MonadOxide::Err))
|
76
|
-
end
|
77
|
-
|
78
|
-
it 'converts to an Err if the function returns a non-Result' do
|
79
|
-
expect(
|
80
|
-
MonadOxide.ok('foo')
|
81
|
-
.and_then(->(_) { 'bar' })
|
82
|
-
.class()
|
83
|
-
).to(be(MonadOxide::Err))
|
84
|
-
end
|
85
|
-
|
86
|
-
it 'provides a ResultReturnExpectedError in the Err for non-Result returns' do
|
87
|
-
expect(
|
88
|
-
MonadOxide.ok('foo')
|
89
|
-
.and_then(->(_) { 'bar' })
|
90
|
-
.unwrap_err()
|
91
|
-
.class()
|
92
|
-
).to(be(MonadOxide::ResultReturnExpectedError))
|
93
|
-
end
|
94
|
-
|
95
|
-
it 'returns the same Ok returned by the function' do
|
96
|
-
expect(
|
97
|
-
MonadOxide.ok('foo')
|
98
|
-
.and_then(->(_) { MonadOxide.ok('bar') })
|
99
|
-
.unwrap()
|
100
|
-
).to(eq('bar'))
|
101
|
-
end
|
102
|
-
|
103
|
-
it 'returns the same Err returned by the function' do
|
104
|
-
expect(
|
105
|
-
MonadOxide.ok('foo')
|
106
|
-
.and_then(->(_) { MonadOxide.err(StandardError.new()) })
|
107
|
-
.unwrap_err()
|
108
|
-
.class()
|
109
|
-
).to(be(StandardError))
|
110
|
-
end
|
111
|
-
|
112
|
-
end
|
113
|
-
|
114
|
-
end
|
115
|
-
|
116
|
-
context '#map' do
|
117
|
-
context 'with blocks' do
|
118
|
-
it 'returns an Err if an error is raised in the block' do
|
119
|
-
expect(
|
120
|
-
MonadOxide.ok('foo')
|
121
|
-
.map() {|_| raise StandardError.new('bar') }
|
122
|
-
.unwrap_err()
|
123
|
-
.class()
|
124
|
-
).to(be(StandardError))
|
125
|
-
end
|
126
|
-
|
127
|
-
it 'returns a new Ok' do
|
128
|
-
expect(
|
129
|
-
MonadOxide.ok('foo')
|
130
|
-
.map() {|_| 'bar' }
|
131
|
-
.class()
|
132
|
-
).to(be(MonadOxide::Ok))
|
133
|
-
end
|
134
|
-
|
135
|
-
it 'applies the block to the data for the new Ok' do
|
136
|
-
expect(
|
137
|
-
MonadOxide.ok('foo')
|
138
|
-
.map() {|s| s + 'bar' }
|
139
|
-
.unwrap()
|
140
|
-
).to(eq('foobar'))
|
141
|
-
end
|
142
|
-
|
143
|
-
it 'allows nesting of Ok' do
|
144
|
-
expect(
|
145
|
-
MonadOxide.ok('foo')
|
146
|
-
.map() {|s| MonadOxide.ok(s) }
|
147
|
-
.unwrap()
|
148
|
-
.class()
|
149
|
-
).to(be(MonadOxide::Ok))
|
150
|
-
end
|
151
|
-
|
152
|
-
it 'allows nesting of Err' do
|
153
|
-
expect(
|
154
|
-
MonadOxide.ok('foo')
|
155
|
-
.map() {|_| MonadOxide.err(StandardError.new('bar')) }
|
156
|
-
.unwrap()
|
157
|
-
.class()
|
158
|
-
).to(be(MonadOxide::Err))
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
context 'with Procs' do
|
163
|
-
it 'returns an Err if an error is raised in the function' do
|
164
|
-
expect(
|
165
|
-
MonadOxide.ok('foo')
|
166
|
-
.map(->(_) { raise StandardError.new })
|
167
|
-
.unwrap_err()
|
168
|
-
.class()
|
169
|
-
).to(be(StandardError))
|
170
|
-
end
|
171
|
-
|
172
|
-
it 'returns a new Ok' do
|
173
|
-
expect(
|
174
|
-
MonadOxide.ok('foo')
|
175
|
-
.map(->(_) { 'bar' })
|
176
|
-
.class()
|
177
|
-
).to(be(MonadOxide::Ok))
|
178
|
-
end
|
179
|
-
|
180
|
-
it 'applies the function to the data for the new Ok' do
|
181
|
-
expect(
|
182
|
-
MonadOxide.ok('foo')
|
183
|
-
.map(->(s) { s + 'bar' })
|
184
|
-
.unwrap()
|
185
|
-
).to(eq('foobar'))
|
186
|
-
end
|
187
|
-
|
188
|
-
it 'allows nesting of Ok' do
|
189
|
-
expect(
|
190
|
-
MonadOxide.ok('foo')
|
191
|
-
.map(->(s) { MonadOxide.ok(s) })
|
192
|
-
.unwrap()
|
193
|
-
.class()
|
194
|
-
).to(be(MonadOxide::Ok))
|
195
|
-
end
|
196
|
-
|
197
|
-
it 'allows nesting of Err' do
|
198
|
-
expect(
|
199
|
-
MonadOxide.ok('foo')
|
200
|
-
.map(->(_) { MonadOxide.err(StandardError.new()) })
|
201
|
-
.unwrap()
|
202
|
-
.class()
|
203
|
-
).to(be(MonadOxide::Err))
|
204
|
-
end
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
context '#map_err' do
|
209
|
-
context 'with blocks' do
|
210
|
-
it 'does nothing with the block' do
|
211
|
-
effected = 'unset'
|
212
|
-
MonadOxide.ok('foo')
|
213
|
-
.map_err() {|s| effected = "effect: #{s}"}
|
214
|
-
expect(effected).not_to(eq('effect: foo'))
|
215
|
-
end
|
216
|
-
|
217
|
-
it 'does not change the underlying data' do
|
218
|
-
expect(
|
219
|
-
MonadOxide.ok('foo')
|
220
|
-
.map_err() { 'bar' }
|
221
|
-
.unwrap()
|
222
|
-
).to(eq('foo'))
|
223
|
-
end
|
224
|
-
end
|
225
|
-
|
226
|
-
context 'with Procs' do
|
227
|
-
it 'does nothing with the function' do
|
228
|
-
effected = 'unset'
|
229
|
-
MonadOxide.ok('foo')
|
230
|
-
.map_err(->(s) { effected = "effect: #{s}"})
|
231
|
-
expect(effected).not_to(eq('effect: foo'))
|
232
|
-
end
|
233
|
-
|
234
|
-
it 'does not change the underlying data' do
|
235
|
-
expect(
|
236
|
-
MonadOxide.ok('foo')
|
237
|
-
.map_err(->(_) { 'bar' })
|
238
|
-
.unwrap()
|
239
|
-
).to(eq('foo'))
|
240
|
-
end
|
241
|
-
end
|
242
|
-
end
|
243
|
-
|
244
|
-
context '#inspect_err' do
|
245
|
-
context 'with blocks' do
|
246
|
-
it 'does nothing with the block' do
|
247
|
-
effected = 'unset'
|
248
|
-
MonadOxide.ok('foo')
|
249
|
-
.inspect_err() {|s| effected = "effect: #{s}"}
|
250
|
-
expect(effected).not_to(eq('effect: foo'))
|
251
|
-
end
|
252
|
-
|
253
|
-
it 'does not change the underlying data' do
|
254
|
-
expect(
|
255
|
-
MonadOxide.ok('foo')
|
256
|
-
.inspect_err() { 'bar' }
|
257
|
-
.unwrap()
|
258
|
-
).to(eq('foo'))
|
259
|
-
end
|
260
|
-
end
|
261
|
-
|
262
|
-
context 'with Procs' do
|
263
|
-
it 'does nothing with the function' do
|
264
|
-
effected = 'unset'
|
265
|
-
MonadOxide.ok('foo')
|
266
|
-
.inspect_err(->(s) { effected = "effect: #{s}"})
|
267
|
-
expect(effected).not_to(eq('effect: foo'))
|
268
|
-
end
|
269
|
-
|
270
|
-
it 'does not change the underlying data' do
|
271
|
-
expect(
|
272
|
-
MonadOxide.ok('foo')
|
273
|
-
.inspect_err(->(_) { 'bar' })
|
274
|
-
.unwrap()
|
275
|
-
).to(eq('foo'))
|
276
|
-
end
|
277
|
-
end
|
278
|
-
end
|
279
|
-
|
280
|
-
context '#inspect_ok' do
|
281
|
-
context 'with blocks' do
|
282
|
-
it 'applies the data to the block provided' do
|
283
|
-
effected = 'unset'
|
284
|
-
MonadOxide.ok('foo')
|
285
|
-
.inspect_ok(->(s) { effected = "effect: #{s}"})
|
286
|
-
expect(effected).to(eq('effect: foo'))
|
287
|
-
end
|
288
|
-
|
289
|
-
it 'does not change the underlying data' do
|
290
|
-
expect(
|
291
|
-
MonadOxide.ok('foo')
|
292
|
-
.inspect_ok() {|_| 'bar' }
|
293
|
-
.unwrap()
|
294
|
-
).to(eq('foo'))
|
295
|
-
end
|
296
|
-
|
297
|
-
it 'returns an Err with the error from the block' do
|
298
|
-
expect(
|
299
|
-
MonadOxide.ok('foo')
|
300
|
-
.inspect_ok() {|_| raise StandardError.new('flagrant') }
|
301
|
-
.unwrap_err()
|
302
|
-
.class()
|
303
|
-
).to(be(StandardError))
|
304
|
-
end
|
305
|
-
end
|
306
|
-
|
307
|
-
context 'with Procs' do
|
308
|
-
it 'applies the data to the function provided' do
|
309
|
-
effected = 'unset'
|
310
|
-
MonadOxide.ok('foo')
|
311
|
-
.inspect_ok(->(s) { effected = "effect: #{s}"})
|
312
|
-
expect(effected).to(eq('effect: foo'))
|
313
|
-
end
|
314
|
-
|
315
|
-
it 'does not change the underlying data' do
|
316
|
-
expect(
|
317
|
-
MonadOxide.ok('foo')
|
318
|
-
.inspect_ok(->(_) { 'bar' })
|
319
|
-
.unwrap()
|
320
|
-
).to(eq('foo'))
|
321
|
-
end
|
322
|
-
|
323
|
-
it 'returns an Err with the error from the function' do
|
324
|
-
expect(
|
325
|
-
MonadOxide.ok('foo')
|
326
|
-
.inspect_ok(->(_) { raise StandardError.new('flagrant') })
|
327
|
-
.unwrap_err()
|
328
|
-
.class()
|
329
|
-
).to(be(StandardError))
|
330
|
-
end
|
331
|
-
end
|
332
|
-
end
|
333
|
-
|
334
|
-
context '#unwrap' do
|
335
|
-
it 'provides the underlying value' do
|
336
|
-
expect(MonadOxide.ok('foo').unwrap()).to(eq('foo'))
|
337
|
-
end
|
338
|
-
end
|
339
|
-
|
340
|
-
context '#unwrap_err' do
|
341
|
-
it 'raises an UnwrapError' do
|
342
|
-
expect {
|
343
|
-
MonadOxide.ok('foo').unwrap_err()
|
344
|
-
}.to(raise_error(MonadOxide::UnwrapError))
|
345
|
-
end
|
346
|
-
end
|
347
|
-
end
|
data/lib/result_spec.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
require 'monad-oxide'
|
2
|
-
require 'result'
|
3
|
-
|
4
|
-
describe MonadOxide::Result do
|
5
|
-
context '#initialize' do
|
6
|
-
it 'is protected from external consumption' do
|
7
|
-
expect { MonadOxide::Result.new('hi') }.to(raise_exception(NoMethodError))
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
context '#flatten' do
|
12
|
-
it 'returns itself when the Result is already flat' do
|
13
|
-
r = MonadOxide.ok('flat')
|
14
|
-
expect(r.flatten()).to(be(r))
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'returns the inner result' do
|
18
|
-
inner = MonadOxide.ok('inner')
|
19
|
-
outer = MonadOxide.ok(inner)
|
20
|
-
expect(outer.flatten()).to(be(inner))
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'returns the innermost result regardless of nesting' do
|
24
|
-
innermost = MonadOxide.ok('innermost')
|
25
|
-
inner = MonadOxide.ok(innermost)
|
26
|
-
outer = MonadOxide.ok(inner)
|
27
|
-
expect(outer.flatten()).to(be(innermost))
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|