dry-monads 1.3.0 → 1.3.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +10 -39
  3. data/.github/ISSUE_TEMPLATE/----please-don-t-ask-for-support-via-issues.md +10 -0
  4. data/.github/ISSUE_TEMPLATE/---bug-report.md +30 -0
  5. data/.github/ISSUE_TEMPLATE/---feature-request.md +18 -0
  6. data/.github/workflows/ci.yml +52 -0
  7. data/.github/workflows/docsite.yml +34 -0
  8. data/.github/workflows/sync_configs.yml +56 -0
  9. data/.rspec +1 -0
  10. data/.rubocop.yml +101 -0
  11. data/CHANGELOG.md +130 -62
  12. data/CODE_OF_CONDUCT.md +13 -0
  13. data/CONTRIBUTING.md +4 -4
  14. data/Gemfile +7 -8
  15. data/Gemfile.devtools +14 -0
  16. data/LICENSE +17 -17
  17. data/README.md +17 -38
  18. data/Rakefile +2 -0
  19. data/bin/.gitkeep +0 -0
  20. data/bin/console +1 -0
  21. data/docsite/source/case-equality.html.md +42 -0
  22. data/docsite/source/do-notation.html.md +207 -0
  23. data/docsite/source/getting-started.html.md +142 -0
  24. data/docsite/source/index.html.md +179 -0
  25. data/docsite/source/list.html.md +87 -0
  26. data/docsite/source/maybe.html.md +146 -0
  27. data/docsite/source/pattern-matching.html.md +68 -0
  28. data/docsite/source/result.html.md +190 -0
  29. data/docsite/source/task.html.md +126 -0
  30. data/docsite/source/tracing-failures.html.md +32 -0
  31. data/docsite/source/try.html.md +76 -0
  32. data/docsite/source/unit.html.md +36 -0
  33. data/docsite/source/validated.html.md +88 -0
  34. data/dry-monads.gemspec +7 -5
  35. data/lib/dry-monads.rb +2 -0
  36. data/lib/dry/monads.rb +3 -0
  37. data/lib/dry/monads/all.rb +2 -0
  38. data/lib/dry/monads/{undefined.rb → constants.rb} +3 -1
  39. data/lib/dry/monads/conversion_stubs.rb +2 -0
  40. data/lib/dry/monads/curry.rb +2 -0
  41. data/lib/dry/monads/do.rb +7 -2
  42. data/lib/dry/monads/do/all.rb +5 -2
  43. data/lib/dry/monads/do/mixin.rb +4 -3
  44. data/lib/dry/monads/either.rb +2 -0
  45. data/lib/dry/monads/errors.rb +4 -2
  46. data/lib/dry/monads/lazy.rb +4 -2
  47. data/lib/dry/monads/list.rb +10 -9
  48. data/lib/dry/monads/maybe.rb +9 -4
  49. data/lib/dry/monads/registry.rb +5 -2
  50. data/lib/dry/monads/result.rb +5 -4
  51. data/lib/dry/monads/result/fixed.rb +4 -2
  52. data/lib/dry/monads/right_biased.rb +55 -8
  53. data/lib/dry/monads/task.rb +6 -3
  54. data/lib/dry/monads/transformer.rb +2 -0
  55. data/lib/dry/monads/traverse.rb +2 -0
  56. data/lib/dry/monads/try.rb +6 -1
  57. data/lib/dry/monads/validated.rb +12 -9
  58. data/lib/dry/monads/version.rb +3 -1
  59. data/lib/json/add/dry/monads/maybe.rb +1 -0
  60. data/project.yml +2 -0
  61. metadata +45 -22
  62. data/.travis.yml +0 -41
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.4.0, available at [https://www.contributor-covenant.org/version/1/4/code-of-conduct](https://www.contributor-covenant.org/version/1/4/code-of-conduct)
@@ -6,11 +6,11 @@ If you found a bug, report an issue and describe what's the expected behavior ve
6
6
 
7
7
  ## Reporting feature requests
8
8
 
9
- Report a feature request **only after discourseing it first on [discourse.dry-rb.org](https://discourse.dry-rb.org)** where it was accepted. Please provide a concise description of the feature, don't link to a discourseion thread, and instead summarize what was discourseed.
9
+ Report a feature request **only after discussing it first on [discourse.dry-rb.org](https://discourse.dry-rb.org)** where it was accepted. Please provide a concise description of the feature, don't link to a discussion thread, and instead summarize what was discussed.
10
10
 
11
11
  ## Reporting questions, support requests, ideas, concerns etc.
12
12
 
13
- **PLEASE DON'T** - use [discourse.dry-rb.org](https://discourse.dry-rb.org) instead.
13
+ **PLEASE DON'T** - use [discourse.dry-rb.org](http://discourse.dry-rb.org) instead.
14
14
 
15
15
  # Pull Request Guidelines
16
16
 
@@ -22,8 +22,8 @@ Other requirements:
22
22
  2) Follow the style conventions of the surrounding code. In most cases, this is standard ruby style.
23
23
  3) Add API documentation if it's a new feature
24
24
  4) Update API documentation if it changes an existing feature
25
- 5) Bonus points for sending a PR to [github.com/dry-rb/dry-rb.org](https://github.com/dry-rb/dry-rb.org) which updates user documentation and guides
25
+ 5) Bonus points for sending a PR to [github.com/dry-rb/dry-rb.org](github.com/dry-rb/dry-rb.org) which updates user documentation and guides
26
26
 
27
27
  # Asking for help
28
28
 
29
- If these guidelines aren't helpful, and you're stuck, please post a message on [discourse.dry-rb.org](https://discourse.dry-rb.org).
29
+ If these guidelines aren't helpful, and you're stuck, please post a message on [discourse.dry-rb.org](https://discourse.dry-rb.org) or join [our chat](https://dry-rb.zulipchat.com).
data/Gemfile CHANGED
@@ -1,20 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
- gemspec
5
+ eval_gemfile 'Gemfile.devtools'
4
6
 
5
- group :test do
6
- gem 'codeclimate-test-reporter', require: false
7
- gem 'simplecov', require: false
8
- gem 'warning'
9
- end
7
+ gemspec
10
8
 
11
9
  group :tools do
12
- gem 'pry-byebug', platform: :mri
10
+ gem 'benchmark-ips'
13
11
  gem 'pry', platform: :jruby
12
+ gem 'pry-byebug', platform: :mri
14
13
  end
15
14
 
16
15
  group :docs do
16
+ gem 'redcarpet'
17
17
  gem 'yard'
18
18
  gem 'yard-junk'
19
- gem 'redcarpet'
20
19
  end
@@ -0,0 +1,14 @@
1
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
2
+
3
+ gem "ossy", github: "solnic/ossy", branch: "master", platforms: :ruby
4
+
5
+ group :test do
6
+ gem 'codacy-coverage', require: false, platforms: :ruby
7
+ gem 'simplecov', require: false, platforms: :ruby
8
+ gem 'warning'
9
+ end
10
+
11
+ group :tools do
12
+ # this is the same version that we use on codacy
13
+ gem 'rubocop', '0.71.0'
14
+ end
data/LICENSE CHANGED
@@ -1,20 +1,20 @@
1
- Copyright (c) 2016-2018 Nikita Shilnikov
1
+ The MIT License (MIT)
2
2
 
3
- Permission is hereby granted, free of charge, to any person obtaining
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-2020 dry-rb team
10
4
 
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
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
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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,50 +1,29 @@
1
- [gitter]: https://gitter.im/dry-rb/chat
2
1
  [gem]: https://rubygems.org/gems/dry-monads
3
- [travis]: https://travis-ci.com/dry-rb/dry-monads
4
- [code_climate]: https://codeclimate.com/github/dry-rb/dry-monads
5
- [inch]: http://inch-ci.org/github/dry-rb/dry-monads
2
+ [actions]: https://github.com/dry-rb/dry-monads/actions
3
+ [codacy]: https://www.codacy.com/gh/dry-rb/dry-monads
6
4
  [chat]: https://dry-rb.zulipchat.com
5
+ [inchpages]: http://inch-ci.org/github/dry-rb/dry-monads
7
6
 
8
7
  # dry-monads [![Join the chat at https://dry-rb.zulipchat.com](https://img.shields.io/badge/dry--rb-join%20chat-%23346b7a.svg)][chat]
9
8
 
10
- [![Gem Version](https://img.shields.io/gem/v/dry-monads.svg)][gem]
11
- [![Build Status](https://img.shields.io/travis/dry-rb/dry-monads.svg)][travis]
12
- [![Code Climate](https://api.codeclimate.com/v1/badges/b0ea4d8023d53b7f0f50/maintainability)][code_climate]
13
- [![Test Coverage](https://api.codeclimate.com/v1/badges/b0ea4d8023d53b7f0f50/test_coverage)][code_climate]
14
- [![API Documentation Coverage](http://inch-ci.org/github/dry-rb/dry-monads.svg)][inch]
15
-
16
- Monads for Ruby.
17
-
18
- ## Installation
19
-
20
- Add this line to your application's Gemfile:
21
-
22
- ```ruby
23
- gem 'dry-monads'
24
- ```
25
-
26
- And then execute:
27
-
28
- ```sh
29
- $ bundle
30
- ```
31
-
32
- Or install it yourself as:
33
-
34
- ```sh
35
- $ gem install dry-monads
36
- ```
9
+ [![Gem Version](https://badge.fury.io/rb/dry-monads.svg)][gem]
10
+ [![CI Status](https://github.com/dry-rb/dry-monads/workflows/ci/badge.svg)][actions]
11
+ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/f2eed41bf7f04b38b0a7691c2cf6e73c)][codacy]
12
+ [![Codacy Badge](https://api.codacy.com/project/badge/Coverage/f2eed41bf7f04b38b0a7691c2cf6e73c)][codacy]
13
+ [![Inline docs](http://inch-ci.org/github/dry-rb/dry-monads.svg?branch=master)][inchpages]
37
14
 
38
15
  ## Links
39
16
 
40
- - [Documentation](http://dry-rb.org/gems/dry-monads)
17
+ * [User documentation](http://dry-rb.org/gems/dry-monads)
18
+ * [API documentation](http://rubydoc.info/gems/dry-monads)
19
+
20
+ ## Supported Ruby versions
41
21
 
42
- ## Development
22
+ This library officially supports the following Ruby versions:
43
23
 
44
- After checking out the repo, run `bin/setup` to install dependencies. Then, run
45
- `rake spec` to run the tests. You can also run `bin/console` for an interactive
46
- prompt that will allow you to experiment.
24
+ * MRI >= `2.4`
25
+ * jruby >= `9.2`
47
26
 
48
- ## Contributing
27
+ ## License
49
28
 
50
- Bug reports and pull requests are welcome on GitHub at <https://github.com/dry-rb/dry-monads>.
29
+ See `LICENSE` file.
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
  require 'rspec/core/rake_task'
3
5
 
File without changes
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'bundler/setup'
4
5
  require 'dry/monads/all'
@@ -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
+ ```