sorbet-result 0.3.0 → 0.3.1

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: 4721bbbd3c051fd826ebc2721567a032a6f64ffb7545c990cef2d5b82fd78305
4
- data.tar.gz: bd1cf4a0d186f3ef0be4f8e8aba11908fe4e4546198a647e10140901f4c16069
3
+ metadata.gz: 55fcf298c304ef8f0101c1a6461c7ded68f9bfb87fead9d12ad23760b4d2268b
4
+ data.tar.gz: 96ee6aaac4f115f97065977d0cd21f510c9dcdab08b73f14855760171c3e5053
5
5
  SHA512:
6
- metadata.gz: d767cc69f61e9de10862ff3ceca4a9fbf880eb748cb7fb12c58f992b6a573a566ea15b6d5867bd47b42eac6a890195d29bd79731de56ef7b1bee90c51d31691d
7
- data.tar.gz: 52577942ef7db2a668b3d505263507758c6a720271ed9e883a346e962c48e53ba3b4850f3fffd590f4580ff2bfb352c08dc2f2d1e503c4c790b58f896ff84a3a
6
+ metadata.gz: 9a0e1ed605028d5d98109173d11a530a7f7025c33462a918ced64bf240e06e740f78371d5a249bc0866cfd4ac1ebeb4cfef2fe58e17a0cad2a8cfd6a646a5796
7
+ data.tar.gz: 454f9d376b8a1066aaa2cbbd350f20b43fc71c1ffece65cee72dc8299118260d2dfb0b5b38e73eb2427d9df6c6ce69e222e21243c32292d1a5859c6fcad3cfb9
data/CHANGELOG.md CHANGED
@@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.3.1] - 2023-06-20
10
+
11
+ ### Added
12
+
13
+ - Add `#on_error` to `Typed::Result` to allow behavior when an error is encountered during chaining.
14
+ - Add `#payload_or` to `Typed::Result` to allow callers to specify a default value if `Failure` is returned.
15
+
9
16
  ## [0.3.0] - 2023-06-06
10
17
 
11
18
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sorbet-result (0.3.0)
4
+ sorbet-result (0.3.1)
5
5
  sorbet-runtime (~> 0.5)
6
6
  zeitwerk (~> 2.6)
7
7
 
@@ -17,11 +17,13 @@ GEM
17
17
  irb (1.7.0)
18
18
  reline (>= 0.3.0)
19
19
  json (2.6.3)
20
- minitest (5.18.0)
20
+ minitest (5.18.1)
21
21
  netrc (0.11.0)
22
22
  parallel (1.23.0)
23
- parser (3.2.2.1)
23
+ parser (3.2.2.3)
24
24
  ast (~> 2.4.1)
25
+ racc
26
+ racc (1.7.1)
25
27
  rainbow (3.1.1)
26
28
  rake (13.0.6)
27
29
  rbi (0.0.16)
@@ -29,14 +31,14 @@ GEM
29
31
  parser (>= 2.6.4.0)
30
32
  sorbet-runtime (>= 0.5.9204)
31
33
  unparser
32
- regexp_parser (2.8.0)
34
+ regexp_parser (2.8.1)
33
35
  reline (0.3.5)
34
36
  io-console (~> 0.5)
35
37
  rexml (3.2.5)
36
- rubocop (1.52.0)
38
+ rubocop (1.52.1)
37
39
  json (~> 2.3)
38
40
  parallel (~> 1.10)
39
- parser (>= 3.2.0.0)
41
+ parser (>= 3.2.2.3)
40
42
  rainbow (>= 2.2.2, < 4.0)
41
43
  regexp_parser (>= 1.8, < 3.0)
42
44
  rexml (>= 3.2.5, < 4.0)
@@ -52,14 +54,14 @@ GEM
52
54
  rubocop-sorbet (0.7.0)
53
55
  rubocop (>= 0.90.0)
54
56
  ruby-progressbar (1.13.0)
55
- sorbet (0.5.10864)
56
- sorbet-static (= 0.5.10864)
57
- sorbet-runtime (0.5.10864)
58
- sorbet-static (0.5.10864-universal-darwin-22)
59
- sorbet-static (0.5.10864-x86_64-linux)
60
- sorbet-static-and-runtime (0.5.10864)
61
- sorbet (= 0.5.10864)
62
- sorbet-runtime (= 0.5.10864)
57
+ sorbet (0.5.10880)
58
+ sorbet-static (= 0.5.10880)
59
+ sorbet-runtime (0.5.10880)
60
+ sorbet-static (0.5.10880-universal-darwin-22)
61
+ sorbet-static (0.5.10880-x86_64-linux)
62
+ sorbet-static-and-runtime (0.5.10880)
63
+ sorbet (= 0.5.10880)
64
+ sorbet-runtime (= 0.5.10880)
63
65
  spoom (1.2.1)
64
66
  sorbet (>= 0.5.10187)
65
67
  sorbet-runtime (>= 0.5.9204)
@@ -75,7 +77,7 @@ GEM
75
77
  yard-sorbet
76
78
  thor (1.2.2)
77
79
  unicode-display_width (2.4.2)
78
- unparser (0.6.7)
80
+ unparser (0.6.8)
79
81
  diff-lcs (~> 1.3)
80
82
  parser (>= 3.2.0)
81
83
  yard (0.9.34)
data/README.md CHANGED
@@ -76,10 +76,11 @@ Finally, there are a few methods you can use on both `Typed::Result` types.
76
76
  ```ruby
77
77
  result = call_api(1)
78
78
 
79
- result.success? # => true if success, false if failure
80
- result.failure? # => true if failure, false if success
79
+ result.success? # => true on success, false on failure
80
+ result.failure? # => true on failure, false on success
81
81
  result.payload # => nil on failure, payload type on failure
82
82
  result.error # => nil on success, error type on failure
83
+ result.payload_or("fallback") # => returns payload on success, given value on failure
83
84
 
84
85
  # You can combine all the above to write flow-sensitive type-checked code
85
86
  if result.success?
@@ -113,6 +114,19 @@ else
113
114
  end
114
115
  ```
115
116
 
117
+ You can also use the `#on_error` chain to take an action only on failure, such as logging or capturing error information in an error monitoring service.
118
+
119
+ ```ruby
120
+ # In this example, retrieve_user and send_notification both return a Typed::Result
121
+ # retrieve_user: Typed::Result[User, RetrieveUserError
122
+ # send_notification: Typed::Result[T::Boolean, SendNotificationError]
123
+ res = retrieve_user(user_id)
124
+ .and_then { |user| send_notification(user.email) } # this block will only run if retrieve_user returns a Typed::Success
125
+ .on_error { |error| puts "Encountered this error: #{error}"}
126
+ ```
127
+
128
+ If the above chain does not fail, the `puts` statement is never run. If the chain does yield a `Failure`, the `puts` block is executed and the `Failure` is ultimately returned.
129
+
116
130
  ## Why use Results?
117
131
 
118
132
  Let's say you're working on a method that reaches out to an API and fetches a resource. We hope to get a successful response and continue on in our program, but you can imagine several scenarios where we don't get that response: our authentication could fail, the server could return a 5XX response code, or the resource we were querying could have moved or not exist any more.
data/lib/typed/failure.rb CHANGED
@@ -57,5 +57,25 @@ module Typed
57
57
  def and_then(&_block)
58
58
  self
59
59
  end
60
+
61
+ sig do
62
+ override
63
+ .params(block: T.proc.params(arg0: Error).void)
64
+ .returns(T.self_type)
65
+ end
66
+ def on_error(&block)
67
+ block.call(error)
68
+ self
69
+ end
70
+
71
+ sig do
72
+ override
73
+ .type_parameters(:Fallback)
74
+ .params(value: T.type_parameter(:Fallback))
75
+ .returns(T.any(Payload, T.type_parameter(:Fallback)))
76
+ end
77
+ def payload_or(value)
78
+ value
79
+ end
60
80
  end
61
81
  end
data/lib/typed/result.rb CHANGED
@@ -32,5 +32,20 @@ module Typed
32
32
  .returns(T.any(Result[T.type_parameter(:U), T.type_parameter(:T)], Result[T.type_parameter(:U), Error]))
33
33
  end
34
34
  def and_then(&_block); end
35
+
36
+ sig do
37
+ abstract
38
+ .params(block: T.proc.params(arg0: Error).void)
39
+ .returns(T.self_type)
40
+ end
41
+ def on_error(&block); end
42
+
43
+ sig do
44
+ abstract
45
+ .type_parameters(:Fallback)
46
+ .params(value: T.type_parameter(:Fallback))
47
+ .returns(T.any(Payload, T.type_parameter(:Fallback)))
48
+ end
49
+ def payload_or(value); end
35
50
  end
36
51
  end
data/lib/typed/success.rb CHANGED
@@ -57,5 +57,24 @@ module Typed
57
57
  def and_then(&block)
58
58
  block.call(payload)
59
59
  end
60
+
61
+ sig do
62
+ override
63
+ .params(_block: T.proc.params(arg0: Error).void)
64
+ .returns(T.self_type)
65
+ end
66
+ def on_error(&_block)
67
+ self
68
+ end
69
+
70
+ sig do
71
+ override
72
+ .type_parameters(:Fallback)
73
+ .params(_value: T.type_parameter(:Fallback))
74
+ .returns(T.any(Payload, T.type_parameter(:Fallback)))
75
+ end
76
+ def payload_or(_value)
77
+ payload
78
+ end
60
79
  end
61
80
  end