lab42_result 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +43 -3
- data/lib/lab42/result.rb +7 -0
- data/lib/lab42/result/version.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6920980da6412b0edd1ff5e0d2c35066454f7cf1ab690901466755f7e3911678
|
4
|
+
data.tar.gz: b8fc3146ae849c55af68408798c9af35e232222892188d42067605516704b61c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f062d735291bbac1d4bb7f17b27c8f1cfb33c97753ae6e775cbf0d90b20836652e03f25572e4c82d3a9f0e1212cf0d8b3d946dea9dcbd6ae4df26ae5f5cb18c
|
7
|
+
data.tar.gz: afdd6e1e1397ecb4c1e66f9813374aa2aa9e1333ceef4d03448bf930da7d47d8fb506d65990982650cbecf9d9db0157b9107f940b3c41573290ea7cb23e2ab0a
|
data/README.md
CHANGED
@@ -4,6 +4,8 @@ A result encapsulation class, like the `Either` type in Haskell.
|
|
4
4
|
|
5
5
|
## Context Quick Starting Guide
|
6
6
|
|
7
|
+
Given
|
8
|
+
|
7
9
|
```ruby
|
8
10
|
|
9
11
|
Result = Lab42::Result
|
@@ -54,6 +56,47 @@ Example: And will certainly raise this time
|
|
54
56
|
expect{ error.raise! }.to raise_error(RuntimeError, "oh no!")
|
55
57
|
```
|
56
58
|
|
59
|
+
#### Capturing Exceptions
|
60
|
+
|
61
|
+
If you have a piece of code like this:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
Result.ok(some_computation)
|
65
|
+
rescue MyError
|
66
|
+
Result.error("Oh my", error: MyError)
|
67
|
+
```
|
68
|
+
|
69
|
+
you can replace it with the convenient
|
70
|
+
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
Result.from("Oh my") {some_computation}
|
74
|
+
```
|
75
|
+
|
76
|
+
Given
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
def divide by
|
80
|
+
Result.from("Zero Division") { 100 / by }
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
Example: Zero Division, not a problem anymore
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
error = divide(0)
|
88
|
+
expect( error ).not_to be_ok
|
89
|
+
expect( error.status ).to eq(ZeroDivisionError)
|
90
|
+
|
91
|
+
```
|
92
|
+
|
93
|
+
Example: Correct Division still works
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
divide(4) in [_, value]
|
97
|
+
expect( value ).to eq(25)
|
98
|
+
```
|
99
|
+
|
57
100
|
## Context A More Detailed View
|
58
101
|
|
59
102
|
### Ok without a value
|
@@ -128,9 +171,6 @@ Example: Look Mam, no default constructor!
|
|
128
171
|
expect{ Result.new }.to raise_error(NoMethodError)
|
129
172
|
```
|
130
173
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
174
|
# LICENSE
|
135
175
|
|
136
176
|
Copyright 2020 Robert Dober robert.dober@gmail.com
|
data/lib/lab42/result.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
Warning[:experimental] = false
|
1
2
|
module Lab42
|
2
3
|
class Result
|
3
4
|
attr_reader :status, :value
|
@@ -28,6 +29,12 @@ module Lab42
|
|
28
29
|
o.freeze
|
29
30
|
end
|
30
31
|
|
32
|
+
def from(error_message, &blk)
|
33
|
+
ok(blk.())
|
34
|
+
rescue Exception => e
|
35
|
+
error(error_message, error: e.class)
|
36
|
+
end
|
37
|
+
|
31
38
|
def ok(value=nil)
|
32
39
|
o = allocate
|
33
40
|
o.instance_exec do
|
data/lib/lab42/result/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lab42_result
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Dober
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-11-
|
11
|
+
date: 2020-11-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -65,8 +65,7 @@ files:
|
|
65
65
|
- lib/lab42/result/version.rb
|
66
66
|
homepage: https://bitbucket.org/robertdober/lab42_result
|
67
67
|
licenses:
|
68
|
-
- Apache
|
69
|
-
- '2'
|
68
|
+
- Apache-2.0
|
70
69
|
metadata: {}
|
71
70
|
post_install_message:
|
72
71
|
rdoc_options: []
|