dry-monads 1.3.1 → 1.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +10 -39
- data/.github/ISSUE_TEMPLATE/----please-don-t-ask-for-support-via-issues.md +10 -0
- data/.github/ISSUE_TEMPLATE/---bug-report.md +34 -0
- data/.github/ISSUE_TEMPLATE/---feature-request.md +18 -0
- data/.github/workflows/ci.yml +74 -0
- data/.github/workflows/docsite.yml +34 -0
- data/.github/workflows/sync_configs.yml +34 -0
- data/.rspec +1 -0
- data/.rubocop.yml +89 -0
- data/CHANGELOG.md +21 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/CONTRIBUTING.md +4 -4
- data/Gemfile +4 -1
- data/LICENSE +17 -17
- data/README.md +2 -2
- data/docsite/source/case-equality.html.md +42 -0
- data/docsite/source/do-notation.html.md +207 -0
- data/docsite/source/getting-started.html.md +142 -0
- data/docsite/source/index.html.md +179 -0
- data/docsite/source/list.html.md +87 -0
- data/docsite/source/maybe.html.md +146 -0
- data/docsite/source/pattern-matching.html.md +68 -0
- data/docsite/source/result.html.md +190 -0
- data/docsite/source/task.html.md +126 -0
- data/docsite/source/tracing-failures.html.md +32 -0
- data/docsite/source/try.html.md +76 -0
- data/docsite/source/unit.html.md +36 -0
- data/docsite/source/validated.html.md +88 -0
- data/lib/dry/monads.rb +1 -0
- data/lib/dry/monads/{undefined.rb → constants.rb} +3 -1
- data/lib/dry/monads/do.rb +2 -1
- data/lib/dry/monads/do/all.rb +2 -2
- data/lib/dry/monads/do/mixin.rb +2 -3
- data/lib/dry/monads/maybe.rb +2 -4
- data/lib/dry/monads/result.rb +1 -1
- data/lib/dry/monads/right_biased.rb +52 -7
- data/lib/dry/monads/validated.rb +1 -1
- data/lib/dry/monads/version.rb +1 -1
- metadata +25 -5
- data/.travis.yml +0 -38
data/LICENSE
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
|
1
|
+
The MIT License (MIT)
|
2
2
|
|
3
|
-
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
3
|
+
Copyright (c) 2015-2019 dry-rb team
|
10
4
|
|
11
|
-
|
12
|
-
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
13
11
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
[gitter]: https://gitter.im/dry-rb/chat
|
2
2
|
[gem]: https://rubygems.org/gems/dry-monads
|
3
|
-
[
|
3
|
+
[ci]: https://github.com/dry-rb/dry-monads/actions?query=workflow%3Aci
|
4
4
|
[code_climate]: https://codeclimate.com/github/dry-rb/dry-monads
|
5
5
|
[inch]: http://inch-ci.org/github/dry-rb/dry-monads
|
6
6
|
[chat]: https://dry-rb.zulipchat.com
|
@@ -8,7 +8,7 @@
|
|
8
8
|
# dry-monads [![Join the chat at https://dry-rb.zulipchat.com](https://img.shields.io/badge/dry--rb-join%20chat-%23346b7a.svg)][chat]
|
9
9
|
|
10
10
|
[![Gem Version](https://img.shields.io/gem/v/dry-monads.svg)][gem]
|
11
|
-
[![Build Status](https://
|
11
|
+
[![Build Status](https://github.com/dry-rb/dry-monads/workflows/ci/badge.svg)][ci]
|
12
12
|
[![Code Climate](https://api.codeclimate.com/v1/badges/b0ea4d8023d53b7f0f50/maintainability)][code_climate]
|
13
13
|
[![Test Coverage](https://api.codeclimate.com/v1/badges/b0ea4d8023d53b7f0f50/test_coverage)][code_climate]
|
14
14
|
[![API Documentation Coverage](http://inch-ci.org/github/dry-rb/dry-monads.svg)][inch]
|
@@ -0,0 +1,42 @@
|
|
1
|
+
---
|
2
|
+
title: Case equality
|
3
|
+
layout: gem-single
|
4
|
+
name: dry-monads
|
5
|
+
---
|
6
|
+
|
7
|
+
### Case equality
|
8
|
+
|
9
|
+
Monads allow to use default ruby `case` operator for matching result:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
case value
|
13
|
+
when Some(1), Some(2) then :one_or_two
|
14
|
+
when Some(3..5) then :three_to_five
|
15
|
+
else
|
16
|
+
:something_else
|
17
|
+
end
|
18
|
+
```
|
19
|
+
|
20
|
+
You can use specific `Failure` options too:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
case value
|
24
|
+
when Success then [:ok, value.value!]
|
25
|
+
when Failure(TimeoutError) then [:timeout]
|
26
|
+
when Failure(ConnectionClosed) then [:net_error]
|
27
|
+
when Failure then [:generic_error]
|
28
|
+
else
|
29
|
+
raise "Unhandled case"
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
#### Nested structures
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
case value
|
37
|
+
when Success(None()) then :nothing
|
38
|
+
when Success(Some { |x| x > 10 }) then :something
|
39
|
+
when Success(Some) then :something_else
|
40
|
+
when Failure then :error
|
41
|
+
end
|
42
|
+
```
|
@@ -0,0 +1,207 @@
|
|
1
|
+
---
|
2
|
+
title: Do notation
|
3
|
+
layout: gem-single
|
4
|
+
name: dry-monads
|
5
|
+
---
|
6
|
+
|
7
|
+
Composing several monadic values can become tedious because you need to pass around unwrapped values in lambdas (aka blocks). Haskell was one of the first languages faced this problem. To work around it Haskell has a special syntax for combining monadic operations called the "do notation". If you're familiar with Scala it has `for`-comprehensions for a similar purpose. It is not possible to implement `do` in Ruby but it is possible to emulate it to some extent, i.e. achieve comparable usefulness.
|
8
|
+
|
9
|
+
What `Do` does is passing an unwrapping block to certain methods. The block tries to extract the underlying value from a monadic object and either short-circuits the execution (in case of a failure) or returns the unwrapped value back.
|
10
|
+
|
11
|
+
See the following example written using `bind` and `fmap`:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
require 'dry/monads'
|
15
|
+
|
16
|
+
class CreateAccount
|
17
|
+
include Dry::Monads[:result]
|
18
|
+
|
19
|
+
def call(params)
|
20
|
+
validate(params).bind { |values|
|
21
|
+
create_account(values[:account]).bind { |account|
|
22
|
+
create_owner(account, values[:owner]).fmap { |owner|
|
23
|
+
[account, owner]
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate(params)
|
30
|
+
# returns Success(values) or Failure(:invalid_data)
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_account(account_values)
|
34
|
+
# returns Success(account) or Failure(:account_not_created)
|
35
|
+
end
|
36
|
+
|
37
|
+
def create_owner(account, owner_values)
|
38
|
+
# returns Success(owner) or Failure(:owner_not_created)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
The more monadic steps you need to combine the harder it becomes, not to mention how difficult it can be to refactor code written in such way.
|
44
|
+
|
45
|
+
Embrace `Do`:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
require 'dry/monads'
|
49
|
+
require 'dry/monads/do'
|
50
|
+
|
51
|
+
class CreateAccount
|
52
|
+
include Dry::Monads[:result]
|
53
|
+
include Dry::Monads::Do.for(:call)
|
54
|
+
|
55
|
+
def call(params)
|
56
|
+
values = yield validate(params)
|
57
|
+
account = yield create_account(values[:account])
|
58
|
+
owner = yield create_owner(account, values[:owner])
|
59
|
+
|
60
|
+
Success([account, owner])
|
61
|
+
end
|
62
|
+
|
63
|
+
def validate(params)
|
64
|
+
# returns Success(values) or Failure(:invalid_data)
|
65
|
+
end
|
66
|
+
|
67
|
+
def create_account(account_values)
|
68
|
+
# returns Success(account) or Failure(:account_not_created)
|
69
|
+
end
|
70
|
+
|
71
|
+
def create_owner(account, owner_values)
|
72
|
+
# returns Success(owner) or Failure(:owner_not_created)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
Both snippets do the same thing yet the second one is a lot easier to deal with. All what `Do` does here is prepending `CreateAccount` with a module which passes a block to `CreateAccount#call`. That simple.
|
78
|
+
|
79
|
+
### Transaction safety
|
80
|
+
|
81
|
+
Under the hood, `Do` uses exceptions to halt unsuccessful operations, this can be slower if you are dealing with unsuccessful paths a lot, but usually, this is not an issue. Check out [this article](https://www.morozov.is/2018/05/27/do-notation-ruby.html) for actual benchmarks.
|
82
|
+
|
83
|
+
One particular reason to use exceptions is the ability to make code transaction-friendly. In the example above, this piece of code is not atomic:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
account = yield create_account(values[:account])
|
87
|
+
owner = yield create_owner(account, values[:owner])
|
88
|
+
|
89
|
+
Success[account, owner]
|
90
|
+
```
|
91
|
+
|
92
|
+
What if `create_account` succeeds and `create_owner` fails? This will leave your database in an inconsistent state. Let's wrap it with a transaction block:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
repo.transaction do
|
96
|
+
account = yield create_account(values[:account])
|
97
|
+
owner = yield create_owner(account, values[:owner])
|
98
|
+
|
99
|
+
Success[account, owner]
|
100
|
+
end
|
101
|
+
```
|
102
|
+
|
103
|
+
Since `yield` internally uses exceptions to control the flow, the exception will be detected by the `transaction` call and the whole operation will be rolled back. No more garbage in your database, yay!
|
104
|
+
|
105
|
+
### Limitations
|
106
|
+
|
107
|
+
`Do` only works with single-value monads, i.e. most of them. At the moment, there is no way to make it work with `List`, though.
|
108
|
+
|
109
|
+
### Adding batteries
|
110
|
+
|
111
|
+
The `Do::All` module takes one step ahead, it tracks all new methods defined in the class and passes a block to every one of them. However, if you pass a block yourself then it takes precedence. This way, in most cases you can use `Do::All` instead of listing methods with `Do.for(...)`:
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
require 'dry/monads'
|
115
|
+
|
116
|
+
class CreateAccount
|
117
|
+
# This will include Do::All by default
|
118
|
+
include Dry::Monads[:result, :do]
|
119
|
+
|
120
|
+
def call(account_params, owner_params)
|
121
|
+
repo.transaction do
|
122
|
+
account = yield create_account(account_params)
|
123
|
+
owner = yield create_owner(account, owner_params)
|
124
|
+
|
125
|
+
Success[account, owner]
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def create_account(params)
|
130
|
+
values = yield validate_account(params)
|
131
|
+
account = repo.create_account(values)
|
132
|
+
|
133
|
+
Success(account)
|
134
|
+
end
|
135
|
+
|
136
|
+
def create_owner(account, params)
|
137
|
+
values = yield validate_owner(params)
|
138
|
+
owner = repo.create_owner(account, values)
|
139
|
+
|
140
|
+
Success(owner)
|
141
|
+
end
|
142
|
+
|
143
|
+
def validate_account(params)
|
144
|
+
# returns Success/Failure
|
145
|
+
end
|
146
|
+
|
147
|
+
def validate_owner(params)
|
148
|
+
# returns Success/Failure
|
149
|
+
end
|
150
|
+
end
|
151
|
+
```
|
152
|
+
|
153
|
+
### Using `Do` methods in other contexts
|
154
|
+
|
155
|
+
You can use methods from the `Do` module directly (starting with 1.3):
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
require 'dry/monads/do'
|
159
|
+
require 'dry/monads/result'
|
160
|
+
|
161
|
+
# some random place in your code
|
162
|
+
Dry::Monads.Do.() do
|
163
|
+
user = Dry::Monads::Do.bind create_user
|
164
|
+
account = Dry::Monads::Do.bind create_account(user)
|
165
|
+
|
166
|
+
Dry::Monads::Success[user, account]
|
167
|
+
end
|
168
|
+
```
|
169
|
+
|
170
|
+
Or you can use `extend`:
|
171
|
+
|
172
|
+
```ruby
|
173
|
+
require 'dry/monads'
|
174
|
+
|
175
|
+
class VeryComplexAndUglyCode
|
176
|
+
extend Dry::Monads::Do::Mixin
|
177
|
+
extend Dry::Monads[:result]
|
178
|
+
|
179
|
+
def self.create_something(result_value)
|
180
|
+
call do
|
181
|
+
extracted = bind result_value
|
182
|
+
processed = bind process(extracted)
|
183
|
+
|
184
|
+
Success(processed)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
```
|
189
|
+
|
190
|
+
`Do::All` also works with class methods:
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
require 'dry/monads'
|
194
|
+
|
195
|
+
class SomeClassLevelLogic
|
196
|
+
extend Dry::Monads[:result, :do]
|
197
|
+
|
198
|
+
def self.call
|
199
|
+
x = yield Success(5)
|
200
|
+
y = yield Success(20)
|
201
|
+
|
202
|
+
Success(x * y)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
SomeClassLevelLogic.() # => Success(100)
|
207
|
+
```
|
@@ -0,0 +1,142 @@
|
|
1
|
+
---
|
2
|
+
title: Getting started
|
3
|
+
layout: gem-single
|
4
|
+
name: dry-monads
|
5
|
+
---
|
6
|
+
|
7
|
+
### Installation
|
8
|
+
|
9
|
+
Add this line to your Gemfile
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'dry-monads'
|
13
|
+
```
|
14
|
+
|
15
|
+
Then run
|
16
|
+
|
17
|
+
```
|
18
|
+
$ bundle
|
19
|
+
```
|
20
|
+
|
21
|
+
### Usage
|
22
|
+
|
23
|
+
Every monad has corresponding value constructors. For example, the `Maybe` monad has two of them: `Some(...)` and `None()`. It also has the `Maybe(...)` method. All three methods start with a capital letter similarly to built-in Ruby methods like `Kernel#Array(...)` and `Kernel#Hash(...)`. Value constructors are not available globally, you need to add them with a mixin.
|
24
|
+
|
25
|
+
To add the `Maybe` constructors add `Dry::Monads[:maybe]` to your class:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'dry/monads'
|
29
|
+
|
30
|
+
class CreateUser
|
31
|
+
# this line loads the Maybe monad and adds
|
32
|
+
# Some(...), None(), and Maybe(...) to CreateUser
|
33
|
+
include Dry::Monads[:maybe]
|
34
|
+
|
35
|
+
def call(params)
|
36
|
+
# ...
|
37
|
+
if valid?(params)
|
38
|
+
None()
|
39
|
+
else
|
40
|
+
Some(create_user(params))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
Example in the docs may use `extend Dry::Monads[...]` for brevity but you normally want to use `include` in production code.
|
47
|
+
|
48
|
+
### Including multiple monads
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
require 'dry/monads'
|
52
|
+
|
53
|
+
class CreateUser
|
54
|
+
# Adds Maybe and Result. The order doesn't matter
|
55
|
+
include Dry::Monads[:maybe, :result]
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
### Using with do notation
|
60
|
+
|
61
|
+
A very common case is using the [Result](docs::result) monad with [do notation](docs::do-notation):
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
require 'dry/monads'
|
65
|
+
|
66
|
+
class ResultCalculator
|
67
|
+
include Dry::Monads[:result, :do]
|
68
|
+
|
69
|
+
def calculate(input)
|
70
|
+
value = Integer(input)
|
71
|
+
|
72
|
+
value = yield add_3(value)
|
73
|
+
value = yield mult_2(value)
|
74
|
+
|
75
|
+
Success(value)
|
76
|
+
end
|
77
|
+
|
78
|
+
def add_3(value)
|
79
|
+
if value > 1
|
80
|
+
Success(value + 3)
|
81
|
+
else
|
82
|
+
Failure("value was less than 1")
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def mult_2(value)
|
87
|
+
if value % 2 == 0
|
88
|
+
Success(value * 2)
|
89
|
+
else
|
90
|
+
Failure("value was not even")
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
c = ResultCalculator.new
|
97
|
+
c.calculate(3) # => Success(12)
|
98
|
+
c.calculate(0) # => Failure("value was less than 1")
|
99
|
+
c.calculate(2) # => Failure("value was not even")
|
100
|
+
```
|
101
|
+
|
102
|
+
### Constructing array values
|
103
|
+
|
104
|
+
Some constructors have shortcuts for wrapping arrays:
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
require 'dry/monads'
|
108
|
+
|
109
|
+
class CreateUser
|
110
|
+
include Dry::Monads[:result]
|
111
|
+
|
112
|
+
def call(params)
|
113
|
+
# ...
|
114
|
+
# Same as Failure([:user_exists, params: params])
|
115
|
+
Failure[:user_exists, params: params]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
```
|
119
|
+
|
120
|
+
### Interaction between monads and constructors availability
|
121
|
+
|
122
|
+
Some values can be converted to others or they can have methods that use other monads. By default, dry-monads doesn't load all monads so you may have troubles like this:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
extend Dry::Monads[:result]
|
126
|
+
|
127
|
+
Success(:foo).to_maybe # RuntimeError: Load Maybe first with require 'dry/monads/maybe'
|
128
|
+
```
|
129
|
+
|
130
|
+
To work around you may either load `dry/monads/maybe` add `maybe` to the mixin:
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
extend Dry::Monads[:result, :maybe]
|
134
|
+
|
135
|
+
Success(:foo).to_maybe # => Some(:foo)
|
136
|
+
```
|
137
|
+
|
138
|
+
For the same reason `Dry::Monads.Some(...)`, `Dry::Monads.Success(...)`, and some other constructors are not available until you explicitly load the monads with `require 'dry/monads/%{monad_name}'`.
|
139
|
+
|
140
|
+
### Loading everything
|
141
|
+
|
142
|
+
Just `require 'dry/monads/all'`
|