to-result 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/to_result.rb +14 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0bf957670d51788e15be5797daeb430d409cc38967e9f3ba93027ce66eefe93c
|
4
|
+
data.tar.gz: 5500df2709d9dd14780e53858cf6a2a10fe029d90d3c097e11280104983fdf44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80580831a173303c62330c741bccaa6bb6edad1155ef9639e33c4e1076132cff165b0a7a9d6d91d2a9d472a69841c3f6fd7af9ab177edcbc6488e8d34d958c14
|
7
|
+
data.tar.gz: 92eda88e706fbe5b330a5417190904d158487e469c726e64d07380e334efb07b9f30def99a2e6f19fa3e354d184cd9892287d18f02dbffe2130cadf5f351abf4
|
data/lib/to_result.rb
CHANGED
@@ -3,14 +3,25 @@ require 'dry/monads'
|
|
3
3
|
module ToResultMixin
|
4
4
|
include Dry::Monads[:do, :result, :try]
|
5
5
|
|
6
|
+
#
|
7
|
+
# ToResult executes a block of code and returns Success or Failure.
|
8
|
+
# All exceptions inherited from StandardError are catched and
|
9
|
+
# converted to Failure or you can pass a list of exceptions to catch.
|
10
|
+
#
|
11
|
+
# @param [Array<Class>] exceptions
|
12
|
+
# @param [Proc] &f
|
13
|
+
#
|
14
|
+
# @return [Success]
|
15
|
+
# @return [Failure]
|
16
|
+
#
|
6
17
|
def ToResult(exceptions = [StandardError], &f)
|
7
|
-
|
8
|
-
|
18
|
+
Try.run(
|
19
|
+
exceptions,
|
9
20
|
Proc.new do
|
10
21
|
f.call
|
11
22
|
rescue Dry::Monads::Do::Halt => e
|
12
23
|
return e.result
|
13
|
-
rescue
|
24
|
+
rescue *exceptions => e
|
14
25
|
raise e
|
15
26
|
end
|
16
27
|
).to_result
|