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,36 +0,0 @@
1
- ---
2
- title: Unit
3
- layout: gem-single
4
- name: dry-monads
5
- ---
6
-
7
- Some constructors do not require you to pass a value. As a default they use `Unit`, a special singleton value:
8
-
9
- ```ruby
10
- extend Dry::Monads[:result]
11
-
12
- Success().value! # => Unit
13
- ```
14
-
15
- `Unit` doesn't have any special properties or methods, it's similar to `nil` except for it is not i.e. `if Unit` passes.
16
-
17
- `Unit` is usually excluded from the output:
18
-
19
- ```ruby
20
- extend Dry::Monads[:result]
21
-
22
- # Outputs as "Success()" but technically it's "Success(Unit)"
23
- Success()
24
- ```
25
-
26
- ### Discarding values
27
-
28
- When the outcome of an operation is not a caller's concern, call `.discard`, it will map the wrapped value to `Unit`:
29
-
30
- ```ruby
31
- extend Dry::Monads[:result]
32
-
33
- result = create_user # returns Success(#<User...>) or Failure(...)
34
-
35
- result.discard # => Maps Success(#<User ...>) to Success() but lefts Failure(...) intact
36
- ```
@@ -1,88 +0,0 @@
1
- ---
2
- title: Validated
3
- layout: gem-single
4
- name: dry-monads
5
- ---
6
-
7
- Suppose you've got a form to validate. If you are using `Result` combined with `Do` your code might look like this:
8
-
9
- ```ruby
10
- require 'dry/monads'
11
-
12
- class CreateAccount
13
- include Dry::Monads[:result, :do]
14
-
15
- def call(form)
16
- name = yield validate_name(form)
17
- email = yield validate_email(form)
18
- password = yield validate_password(form)
19
-
20
- user = repo.create_user(
21
- name: name,
22
- email: email,
23
- password: password
24
- )
25
-
26
- Success(user)
27
- end
28
-
29
- def validate_name(form)
30
- # Success(name) or Failure(:invalid_name)
31
- end
32
-
33
- def validate_email(form)
34
- # Success(email) or Failure(:invalid_email)
35
- end
36
-
37
- def validate_password(form)
38
- # Success(password) or Failure(:invalid_password)
39
- end
40
- end
41
- ```
42
-
43
- If any of the validation steps fails the user will see an error. The problem is if `name` is not valid the user won't see errors about invalid `email` and `password`, if any. `Validated` circumvents this particular problem.
44
-
45
- `Validated` is actually not a monad but an applicative functor. This means you can't call `bind` on it. Instead, it can accumulate values in combination with `List`:
46
-
47
- ```ruby
48
- require 'dry/monads'
49
-
50
- class CreateAccount
51
- include Dry::Monads[:list, :result, :validated, :do]
52
-
53
- def call(form)
54
- name, email, password = yield List::Validated[
55
- validate_name(form),
56
- validate_email(form),
57
- validate_password(form)
58
- ].traverse.to_result
59
-
60
- user = repo.create_user(
61
- name: name,
62
- email: email,
63
- password: password
64
- )
65
-
66
- Success(user)
67
- end
68
-
69
- def validate_name(form)
70
- # Valid(name) or Invalid(:invalid_name)
71
- end
72
-
73
- def validate_email(form)
74
- # Valid(email) or Invalid(:invalid_email)
75
- end
76
-
77
- def validate_password(form)
78
- # Valid(password) or Invalid(:invalid_password)
79
- end
80
- end
81
- ```
82
-
83
- Here all validations will be processed at once, if any of them fails the result will be converted to a `Failure` wrapping the `List` of errors:
84
-
85
- ```ruby
86
- create_account.(form)
87
- # => Failure(List[:invalid_name, :invalid_email])
88
- ```
data/log/.gitkeep DELETED
File without changes
data/project.yml DELETED
@@ -1,2 +0,0 @@
1
- name: dry-monads
2
- codacy_id: f2eed41bf7f04b38b0a7691c2cf6e73c