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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7db2b1626106618a7db8df44054facf9d718443739600db1fe6930894b0df4f2
4
- data.tar.gz: d2204c043e4a32b834836d5af246b1a7739fdd6e993a0dcf1132aab8e2fb71e5
3
+ metadata.gz: 6920980da6412b0edd1ff5e0d2c35066454f7cf1ab690901466755f7e3911678
4
+ data.tar.gz: b8fc3146ae849c55af68408798c9af35e232222892188d42067605516704b61c
5
5
  SHA512:
6
- metadata.gz: ed06a0259e5fb20c5feb031151b092aabca5b17573ab80b672db434015ba9d71ede382053ac64b33f650d2f7669fe4a7538a816adf430ed7a1425071a6688d9b
7
- data.tar.gz: 4dbf2923474bdee3159a056da892787fa24a6a9cd42638cf287892a769ab8bfac06420930c8086309124b24f154d58c306ef3310cf5968bee52fedda1f72a36c
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
@@ -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
@@ -1,5 +1,5 @@
1
1
  module Lab42
2
2
  class Result
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
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.0
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-14 00:00:00.000000000 Z
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: []