dry-monads 1.3.5 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +140 -80
  3. data/LICENSE +1 -1
  4. data/README.md +5 -4
  5. data/dry-monads.gemspec +30 -30
  6. data/lib/dry-monads.rb +1 -1
  7. data/lib/dry/monads.rb +2 -2
  8. data/lib/dry/monads/all.rb +2 -2
  9. data/lib/dry/monads/constants.rb +1 -1
  10. data/lib/dry/monads/do.rb +52 -18
  11. data/lib/dry/monads/do/all.rb +36 -17
  12. data/lib/dry/monads/either.rb +7 -7
  13. data/lib/dry/monads/errors.rb +5 -2
  14. data/lib/dry/monads/lazy.rb +15 -4
  15. data/lib/dry/monads/list.rb +28 -28
  16. data/lib/dry/monads/maybe.rb +87 -19
  17. data/lib/dry/monads/registry.rb +10 -10
  18. data/lib/dry/monads/result.rb +38 -12
  19. data/lib/dry/monads/result/fixed.rb +33 -24
  20. data/lib/dry/monads/right_biased.rb +35 -16
  21. data/lib/dry/monads/task.rb +20 -20
  22. data/lib/dry/monads/transformer.rb +2 -1
  23. data/lib/dry/monads/traverse.rb +7 -1
  24. data/lib/dry/monads/try.rb +45 -12
  25. data/lib/dry/monads/unit.rb +6 -2
  26. data/lib/dry/monads/validated.rb +20 -16
  27. data/lib/dry/monads/version.rb +1 -1
  28. data/lib/json/add/dry/monads/maybe.rb +3 -3
  29. metadata +18 -69
  30. data/.codeclimate.yml +0 -12
  31. data/.github/ISSUE_TEMPLATE/----please-don-t-ask-for-support-via-issues.md +0 -10
  32. data/.github/ISSUE_TEMPLATE/---bug-report.md +0 -30
  33. data/.github/ISSUE_TEMPLATE/---feature-request.md +0 -18
  34. data/.github/workflows/ci.yml +0 -52
  35. data/.github/workflows/docsite.yml +0 -34
  36. data/.github/workflows/sync_configs.yml +0 -56
  37. data/.gitignore +0 -10
  38. data/.rspec +0 -4
  39. data/.rubocop.yml +0 -101
  40. data/.yardopts +0 -4
  41. data/CODE_OF_CONDUCT.md +0 -13
  42. data/CONTRIBUTING.md +0 -29
  43. data/Gemfile +0 -19
  44. data/Gemfile.devtools +0 -14
  45. data/Rakefile +0 -8
  46. data/bin/.gitkeep +0 -0
  47. data/bin/console +0 -17
  48. data/bin/setup +0 -7
  49. data/docsite/source/case-equality.html.md +0 -42
  50. data/docsite/source/do-notation.html.md +0 -207
  51. data/docsite/source/getting-started.html.md +0 -142
  52. data/docsite/source/index.html.md +0 -179
  53. data/docsite/source/list.html.md +0 -87
  54. data/docsite/source/maybe.html.md +0 -146
  55. data/docsite/source/pattern-matching.html.md +0 -68
  56. data/docsite/source/result.html.md +0 -190
  57. data/docsite/source/task.html.md +0 -126
  58. data/docsite/source/tracing-failures.html.md +0 -32
  59. data/docsite/source/try.html.md +0 -76
  60. data/docsite/source/unit.html.md +0 -36
  61. data/docsite/source/validated.html.md +0 -88
  62. data/log/.gitkeep +0 -0
  63. data/project.yml +0 -2
@@ -1,56 +0,0 @@
1
- # this file is managed by dry-rb/devtools project
2
-
3
- name: sync_configs
4
-
5
- on:
6
- repository_dispatch:
7
-
8
- jobs:
9
- sync-configs:
10
- runs-on: ubuntu-latest
11
- if: github.event.action == 'sync_configs'
12
- steps:
13
- - uses: actions/checkout@v1
14
- - name: Set up Ruby
15
- uses: eregon/use-ruby-action@master
16
- with:
17
- ruby-version: 2.6
18
- - name: Install latest bundler
19
- run: |
20
- gem install bundler
21
- bundle config set without 'tools benchmarks docs'
22
- - name: Install dependencies
23
- run: bundle install --jobs 4 --retry 3 --without tools test benchmarks docs
24
- - name: Symlink ossy
25
- run: ln -sf "$(bundle info ossy --path)/bin/ossy" bin/ossy
26
- - name: Clone devtools
27
- run: git clone https://github.com/dry-rb/devtools.git tmp/devtools
28
- - name: Compile file templates
29
- env:
30
- GITHUB_LOGIN: dry-bot
31
- GITHUB_TOKEN: ${{ secrets.GH_PAT }}
32
- run: |
33
- if [ -f "project.yml" ]; then
34
- for t in `ls tmp/devtools/templates`
35
- do
36
- bin/ossy t c tmp/devtools/templates/$t $(basename $t .erb) project.yml
37
- done
38
- fi
39
- - name: Update configuration files from devtools
40
- run: |
41
- if [ -f ".github/workflows/custom_ci.yml" ]; then
42
- rsync -arv --exclude '.github/workflows/ci.yml' tmp/devtools/shared/ . ;
43
- else
44
- rsync -arv tmp/devtools/shared/ . ;
45
- fi
46
- - name: Commit changes
47
- run: |
48
- rm bin/ossy
49
- git config --local user.email "dry-bot@dry-rb.org"
50
- git config --local user.name "dry-bot"
51
- git add -A
52
- git commit -m "[devtools] sync configs" || echo "nothing changed"
53
- - name: Push changes
54
- uses: ad-m/github-push-action@master
55
- with:
56
- github_token: ${{ secrets.GH_PAT }}
data/.gitignore DELETED
@@ -1,10 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
10
- log/
data/.rspec DELETED
@@ -1,4 +0,0 @@
1
- --color
2
- --require spec_helper
3
- --order random
4
- --warnings
data/.rubocop.yml DELETED
@@ -1,101 +0,0 @@
1
- # this file is managed by dry-rb/devtools project
2
-
3
- AllCops:
4
- TargetRubyVersion: 2.4
5
-
6
- Style/EachWithObject:
7
- Enabled: false
8
-
9
- Style/StringLiterals:
10
- Enabled: true
11
- EnforcedStyle: single_quotes
12
-
13
- Style/ParallelAssignment:
14
- Enabled: false
15
-
16
- Style/Alias:
17
- Enabled: false
18
-
19
- Style/LambdaCall:
20
- Enabled: false
21
-
22
- Style/StabbyLambdaParentheses:
23
- Enabled: false
24
-
25
- Style/FormatString:
26
- Enabled: false
27
-
28
- Style/Documentation:
29
- Enabled: false
30
-
31
- Layout/SpaceInLambdaLiteral:
32
- Enabled: false
33
-
34
- Layout/MultilineMethodCallIndentation:
35
- Enabled: true
36
- EnforcedStyle: indented
37
-
38
- Metrics/LineLength:
39
- Max: 100
40
-
41
- Metrics/MethodLength:
42
- Enabled: false
43
-
44
- Metrics/ClassLength:
45
- Enabled: false
46
-
47
- Metrics/BlockLength:
48
- Enabled: false
49
-
50
- Metrics/AbcSize:
51
- Max: 20
52
-
53
- Metrics/CyclomaticComplexity:
54
- Enabled: true
55
- Max: 10
56
-
57
- Lint/BooleanSymbol:
58
- Enabled: false
59
-
60
- Style/AccessModifierDeclarations:
61
- Enabled: false
62
-
63
- Style/BlockDelimiters:
64
- Enabled: false
65
-
66
- Layout/IndentFirstArrayElement:
67
- EnforcedStyle: consistent
68
-
69
- Style/ClassAndModuleChildren:
70
- Exclude:
71
- - "spec/**/*_spec.rb"
72
-
73
- Lint/HandleExceptions:
74
- Exclude:
75
- - "spec/spec_helper.rb"
76
-
77
- Naming/PredicateName:
78
- Enabled: false
79
-
80
- Naming/FileName:
81
- Exclude:
82
- - "lib/dry-*.rb"
83
-
84
- Style/SymbolArray:
85
- Exclude:
86
- - "spec/**/*_spec.rb"
87
-
88
- Style/ConditionalAssignment:
89
- Enabled: false
90
-
91
- Naming/MethodName:
92
- Enabled: false
93
-
94
- Style/AsciiComments:
95
- Enabled: false
96
-
97
- Style/DateTime:
98
- Enabled: false
99
-
100
- Style/IfUnlessModifier:
101
- Enabled: false
data/.yardopts DELETED
@@ -1,4 +0,0 @@
1
- --markup-provider=redcarpet
2
- --markup=markdown
3
- --plugin junk
4
- lib/**/*.rb
data/CODE_OF_CONDUCT.md DELETED
@@ -1,13 +0,0 @@
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)
data/CONTRIBUTING.md DELETED
@@ -1,29 +0,0 @@
1
- # Issue Guidelines
2
-
3
- ## Reporting bugs
4
-
5
- If you found a bug, report an issue and describe what's the expected behavior versus what actually happens. If the bug causes a crash, attach a full backtrace. If possible, a reproduction script showing the problem is highly appreciated.
6
-
7
- ## Reporting feature requests
8
-
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
-
11
- ## Reporting questions, support requests, ideas, concerns etc.
12
-
13
- **PLEASE DON'T** - use [discourse.dry-rb.org](http://discourse.dry-rb.org) instead.
14
-
15
- # Pull Request Guidelines
16
-
17
- A Pull Request will only be accepted if it addresses a specific issue that was reported previously, or fixes typos, mistakes in documentation etc.
18
-
19
- Other requirements:
20
-
21
- 1) Do not open a pull request if you can't provide tests along with it. If you have problems writing tests, ask for help in the related issue.
22
- 2) Follow the style conventions of the surrounding code. In most cases, this is standard ruby style.
23
- 3) Add API documentation if it's a new feature
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](github.com/dry-rb/dry-rb.org) which updates user documentation and guides
26
-
27
- # Asking for help
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) or join [our chat](https://dry-rb.zulipchat.com).
data/Gemfile DELETED
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source 'https://rubygems.org'
4
-
5
- eval_gemfile 'Gemfile.devtools'
6
-
7
- gemspec
8
-
9
- group :tools do
10
- gem 'benchmark-ips'
11
- gem 'pry', platform: :jruby
12
- gem 'pry-byebug', platform: :mri
13
- end
14
-
15
- group :docs do
16
- gem 'redcarpet'
17
- gem 'yard'
18
- gem 'yard-junk'
19
- end
data/Gemfile.devtools DELETED
@@ -1,14 +0,0 @@
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/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/gem_tasks'
4
- require 'rspec/core/rake_task'
5
-
6
- RSpec::Core::RakeTask.new(:spec)
7
-
8
- task default: :spec
data/bin/.gitkeep DELETED
File without changes
data/bin/console DELETED
@@ -1,17 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'bundler/setup'
5
- require 'dry/monads/all'
6
-
7
- M = Dry::Monads
8
-
9
- # You can add fixtures and/or initialization code here to make experimenting
10
- # with your gem easier. You can also use a different console, if you like.
11
-
12
- # (If you use this, don't forget to add pry to your Gemfile!)
13
- require 'pry'
14
-
15
- binding.pry
16
-
17
- p
data/bin/setup DELETED
@@ -1,7 +0,0 @@
1
- #!/bin/bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
-
5
- bundle install
6
-
7
- # Do any other automated setup that you need to do here
@@ -1,42 +0,0 @@
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
- ```
@@ -1,207 +0,0 @@
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
- ```