mona-result 0.1.0 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/CHANGELOG.md +12 -1
- data/Gemfile.lock +4 -4
- data/README.md +83 -4
- data/Rakefile +1 -0
- data/lib/mona/result/version.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af436f63f473104b9dd2b4bffb0c379e15032c5eee00fbd7b76491f8d7dc3ba0
|
4
|
+
data.tar.gz: 0a198685c28efc6a3d72b87daf6563fb5ca6876e1990063b659a311ba5bd91a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 545a03866806626c22141209e8e5a5b828f5a686cfa54d40a90a3015b5efa3ea6ba404fdb77b2b0869fec30b2f8e035e551b8d836601710324ef3f3cccab5e6a
|
7
|
+
data.tar.gz: 6886883d884629a9de5fe769016fbb0b44c5a107e6f49bc1c6688c7fd6efab2800da4d47c252985ca8676d7f9d69ae87fbb80e6faeda001c2385447137d6bbef
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
mona-result (0.1.
|
4
|
+
mona-result (0.1.3)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -33,14 +33,14 @@ GEM
|
|
33
33
|
rbs (2.6.0)
|
34
34
|
regexp_parser (2.5.0)
|
35
35
|
rexml (3.2.5)
|
36
|
-
rubocop (1.
|
36
|
+
rubocop (1.34.0)
|
37
37
|
json (~> 2.3)
|
38
38
|
parallel (~> 1.10)
|
39
|
-
parser (>= 3.1.
|
39
|
+
parser (>= 3.1.2.1)
|
40
40
|
rainbow (>= 2.2.2, < 4.0)
|
41
41
|
regexp_parser (>= 1.8, < 3.0)
|
42
42
|
rexml (>= 3.2.5, < 4.0)
|
43
|
-
rubocop-ast (>= 1.
|
43
|
+
rubocop-ast (>= 1.20.0, < 2.0)
|
44
44
|
ruby-progressbar (~> 1.7)
|
45
45
|
unicode-display_width (>= 1.4.0, < 3.0)
|
46
46
|
rubocop-ast (1.21.0)
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Mona::Result
|
2
2
|
|
3
|
-
|
3
|
+
Provides a result monad, with OK holding a value, and Err holding a failure, with optional reason, and metadata.
|
4
4
|
|
5
|
-
|
5
|
+
Also provides a dict result monad, a hash-like result, which provides `#sequence` whose semantics mirror _do notation_
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -16,7 +16,86 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
16
16
|
|
17
17
|
## Usage
|
18
18
|
|
19
|
-
|
19
|
+
```ruby
|
20
|
+
Result = Mona::Result
|
21
|
+
|
22
|
+
# basic OK
|
23
|
+
success = Result.ok(1) # => #<OK 1>
|
24
|
+
success.ok? # => true
|
25
|
+
success.err? # => false
|
26
|
+
success.value # => 1
|
27
|
+
success.and_tap { puts "it is #{_1}" }.and_then { _1 * 5 }.value_or { :nope }
|
28
|
+
# OUT: it is 1
|
29
|
+
# => 5
|
30
|
+
|
31
|
+
# basic Err
|
32
|
+
failure = Result.err(4, :not_prime) # => #<Err 4 reason: :not_prime>
|
33
|
+
failure.ok? # => false
|
34
|
+
failure.err? # => true
|
35
|
+
failure.failure # => 4
|
36
|
+
failure.reason # => :not_prime
|
37
|
+
failure.and_tap { puts "it is #{_1}" }.and_then { _1 * 5 }.value_or { :nope } # => :nope
|
38
|
+
|
39
|
+
# Dict with sequence, example using a fictional repository.
|
40
|
+
# #set can take a [Symbol, Symbol] key argument where left is OK key, right is Err key
|
41
|
+
result = Result.dict do |d|
|
42
|
+
d.set :user, UserRepo.find user_id
|
43
|
+
d.set :input, PostInput.valid(attributes)
|
44
|
+
d.set [:post, :input], PostRepo.create user_id: user_id, **d[:input]
|
45
|
+
end
|
46
|
+
# if find user fails: # => #<Err {:user=>NotFoundError} reason: :not_found, key: :user>
|
47
|
+
# if validation fails: # => #<Err {:user=>User, :input=>PostInput(invalid)} reason: :invalid, key: :input>
|
48
|
+
# if create fails: # => #<Err {:user=>User, :input=>PostInput(db err)} reason: :db_err, key: :input>
|
49
|
+
# if all ok: # => #<OK: {:user=>User, :post=>Post}>
|
50
|
+
|
51
|
+
# Action - a callable sequence
|
52
|
+
divide = Result.action do |x, y|
|
53
|
+
set :numerator, x
|
54
|
+
set :divisor, y.zero? ? Result.err(y, :is_zero) : y
|
55
|
+
puts "ready to calculate answer"
|
56
|
+
set :answer, numerator / divisor
|
57
|
+
puts "answer was: #{answer}"
|
58
|
+
end
|
59
|
+
|
60
|
+
divide.call(12, 4)
|
61
|
+
# OUT: ready to calculate answer
|
62
|
+
# OUT: answer was: 3
|
63
|
+
# => #<OK {:numerator=>12, :divisor=>4, :answer=>3}>
|
64
|
+
|
65
|
+
result = divide.call(12, 0) # => #<Err {:numerator=>12, :divisor=>0} reason: :is_zero, key: :divisor>
|
66
|
+
result.err? # => true
|
67
|
+
result.failure # => {:numerator=>12, :divisor=>0}
|
68
|
+
result.reason # => :is_zero
|
69
|
+
result.meta # => {:key=>:divisor}
|
70
|
+
```
|
71
|
+
|
72
|
+
Pattern matching is supported in the following way:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
case result
|
76
|
+
in :ok, value
|
77
|
+
in :err, failure, reason, { **meta }
|
78
|
+
end
|
79
|
+
|
80
|
+
case result
|
81
|
+
in ok:
|
82
|
+
in err:, reason:, **meta
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
You can also match a result, using similar semantics, the difference from using case/pattern-match is that a
|
87
|
+
`Mona::Result::NoMatchError` is raised, which keeps a reference to the result (which is useful for differentially
|
88
|
+
handling errors in an enclosing scope - not currently possible with ruby's `NoMatchingPatternError` which does not
|
89
|
+
contain any information about the failed match)
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
Mona::Result.match(result) do |on|
|
93
|
+
on.ok { |value| puts "ok" }
|
94
|
+
on.err(key: :x) { |failure, reason, **meta| puts "error with meta :key => :x" }
|
95
|
+
on.err(:invalid) { |failure, reason, **meta| puts "error with reason :invalid" }
|
96
|
+
on.err { |failure, reason, **meta| puts "error" }
|
97
|
+
end
|
98
|
+
```
|
20
99
|
|
21
100
|
## Development
|
22
101
|
|
@@ -26,7 +105,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
26
105
|
|
27
106
|
## Contributing
|
28
107
|
|
29
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
108
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mona-rb/mona-result.
|
30
109
|
|
31
110
|
## License
|
32
111
|
|
data/Rakefile
CHANGED
data/lib/mona/result/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mona-result
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ian White
|
@@ -10,7 +10,7 @@ bindir: exe
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2022-08-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: Mona::Result provides a result monad, and
|
13
|
+
description: Mona::Result provides a result monad, and dict result monad with do-notation
|
14
14
|
email:
|
15
15
|
- ian.w.white@gmail.com
|
16
16
|
executables: []
|
@@ -35,14 +35,14 @@ files:
|
|
35
35
|
- lib/mona/result/sequence.rb
|
36
36
|
- lib/mona/result/version.rb
|
37
37
|
- sig/mona/result.rbs
|
38
|
-
homepage: https://github.com/mona/mona-result
|
38
|
+
homepage: https://github.com/mona-rb/mona-result
|
39
39
|
licenses:
|
40
40
|
- MIT
|
41
41
|
metadata:
|
42
42
|
rubygems_mfa_required: 'true'
|
43
|
-
homepage_uri: https://github.com/mona/mona-result
|
44
|
-
source_code_uri: https://github.com/mona/mona-result
|
45
|
-
changelog_uri: https://github.com/mona/mona-result/CHANGELOG.md
|
43
|
+
homepage_uri: https://github.com/mona-rb/mona-result
|
44
|
+
source_code_uri: https://github.com/mona-rb/mona-result
|
45
|
+
changelog_uri: https://github.com/mona-rb/mona-result/blob/main/CHANGELOG.md
|
46
46
|
post_install_message:
|
47
47
|
rdoc_options: []
|
48
48
|
require_paths:
|
@@ -61,5 +61,5 @@ requirements: []
|
|
61
61
|
rubygems_version: 3.3.7
|
62
62
|
signing_key:
|
63
63
|
specification_version: 4
|
64
|
-
summary: Mona::Result is a result
|
64
|
+
summary: Mona::Result is a result monad for ruby
|
65
65
|
test_files: []
|