lab42_result 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +39 -1
- data/lib/lab42/result.rb +9 -0
- data/lib/lab42/result/autoimport.rb +2 -0
- data/lib/lab42/result/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 724a3a863f90a510af10f920ee91a08c24f353be0008838bf7e2f4f9a650c5a0
|
4
|
+
data.tar.gz: 6ec0266791b57e50275fffef814eb102dd60e8492cbe21439b1880c7410130fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: caeb386d5893ad6f23921535ff2fc580aedbecb77013f27c876b087be5ef345cc8e6ce69942ca190cef6072eb0f0f414e6cdf9233eb69e04a79661c75268f3b4
|
7
|
+
data.tar.gz: 6ce4b7a39dc5678f21d99eba1127de2dda5c22042892669ac10596961162b15e24b1120f35f2dab7357591dc9ea293d168db81f72159b923f01421602e4b5389
|
data/README.md
CHANGED
@@ -11,7 +11,9 @@ Given
|
|
11
11
|
|
12
12
|
```ruby
|
13
13
|
|
14
|
-
|
14
|
+
require "lab42/result/autoimport" # equivalent to: the following lines:
|
15
|
+
# require "lab42/result"
|
16
|
+
# Result = Lab42::Result
|
15
17
|
|
16
18
|
let(:ok) { Result.ok(42) }
|
17
19
|
let(:error) { Result.error("oh no!") }
|
@@ -33,6 +35,24 @@ Example: Its value might be of interest
|
|
33
35
|
expect( ok.value ).to eq(42)
|
34
36
|
```
|
35
37
|
|
38
|
+
And it will execute the block passed to the `if_ok` method
|
39
|
+
```ruby
|
40
|
+
x = nil
|
41
|
+
expect(ok.if_ok {x = 42}).to eq(42)
|
42
|
+
expect(x).to eq(42)
|
43
|
+
```
|
44
|
+
And the value is passed in
|
45
|
+
```ruby
|
46
|
+
expect(ok.if_ok{ _1/2}).to eq(21)
|
47
|
+
```
|
48
|
+
|
49
|
+
But not the one passed to the `if_error` method
|
50
|
+
```ruby
|
51
|
+
x = nil
|
52
|
+
expect(ok.if_error {x = 42}).to be_nil
|
53
|
+
expect(x).to be_nil
|
54
|
+
```
|
55
|
+
|
36
56
|
Example: And will not raise any error
|
37
57
|
|
38
58
|
```ruby
|
@@ -74,6 +94,24 @@ And also you might like to have access to the original message
|
|
74
94
|
.to raise_error(KeyError, "key not found oh no!")
|
75
95
|
```
|
76
96
|
|
97
|
+
And it will execute the block passed to the `if_error` method
|
98
|
+
```ruby
|
99
|
+
x = nil
|
100
|
+
expect(error.if_error {x = 42}).to eq(42)
|
101
|
+
expect(x).to eq(42)
|
102
|
+
```
|
103
|
+
And the error and message are passed in
|
104
|
+
```ruby
|
105
|
+
expect(error.if_error {[_1, _2]}).to eq([RuntimeError, "oh no!"])
|
106
|
+
```
|
107
|
+
|
108
|
+
But not the one passed to the `if_ok` method
|
109
|
+
```ruby
|
110
|
+
x = nil
|
111
|
+
expect(error.if_ok {x = 42}).to be_nil
|
112
|
+
expect(x).to be_nil
|
113
|
+
```
|
114
|
+
|
77
115
|
#### Capturing Exceptions
|
78
116
|
|
79
117
|
If you have a piece of code like this:
|
data/lib/lab42/result.rb
CHANGED
@@ -6,6 +6,14 @@ module Lab42
|
|
6
6
|
[status, value].freeze
|
7
7
|
end
|
8
8
|
|
9
|
+
def if_error &blk
|
10
|
+
blk.(status, value) unless ok?
|
11
|
+
end
|
12
|
+
|
13
|
+
def if_ok &blk
|
14
|
+
blk.(value) if ok?
|
15
|
+
end
|
16
|
+
|
9
17
|
def ok?
|
10
18
|
status == :ok
|
11
19
|
end
|
@@ -15,6 +23,7 @@ module Lab42
|
|
15
23
|
message = blk ? blk.(value) : value
|
16
24
|
raise((alternative_exception||status), message)
|
17
25
|
end
|
26
|
+
|
18
27
|
class << self
|
19
28
|
def new(*,**)
|
20
29
|
raise NoMethodError, "only use the constructors `.ok` and `.error`"
|
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.5
|
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-12-
|
11
|
+
date: 2020-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- LICENSE
|
63
63
|
- README.md
|
64
64
|
- lib/lab42/result.rb
|
65
|
+
- lib/lab42/result/autoimport.rb
|
65
66
|
- lib/lab42/result/version.rb
|
66
67
|
homepage: https://bitbucket.org/robertdober/lab42_result
|
67
68
|
licenses:
|
@@ -82,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
83
|
- !ruby/object:Gem::Version
|
83
84
|
version: '0'
|
84
85
|
requirements: []
|
85
|
-
rubygems_version: 3.1.
|
86
|
+
rubygems_version: 3.1.4
|
86
87
|
signing_key:
|
87
88
|
specification_version: 4
|
88
89
|
summary: Result à la Either (of Haskell)
|