dryer_services 1.2.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +46 -18
- data/dryer_services.gemspec +1 -1
- data/lib/dryer/services/services/result_service.rb +5 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5da96272749622ca4b98bae9f6d151a56826dc87641c2e5cc609f93e46a4dd13
|
4
|
+
data.tar.gz: a40fb9f8d0640629cdc1c9b6424786cbc3509169b448b66dfc328f079ffccb4b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 788649f164868cfbc12115a9f2a1292907aec9e59a7dde02ad16a581e228b38da8674059ef578de3b37f8db159f58bb23ab09ac9ae844b630612b288bee5ab34
|
7
|
+
data.tar.gz: a12edffd82da0f701c91c5e0f085f50dcf55a289c59adeabe2e7520296c149759e36c14f4bbf0131b738484b69e4627684359195ad522fbdce57c76e4f5bfbb9
|
data/README.md
CHANGED
@@ -37,13 +37,23 @@ end
|
|
37
37
|
Add.call(1,2) # returns 3
|
38
38
|
```
|
39
39
|
|
40
|
-
### ResultService
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
40
|
+
### ResultService Examples
|
41
|
+
|
42
|
+
ResultService wraps the value returned from `call` in a `Dry::Monad::Result`.
|
43
|
+
There are several cases
|
44
|
+
- If the return value is an Error, it will return a Failure
|
45
|
+
- If the return value is not an Error, and is not a Result, it will wrap the
|
46
|
+
return value in a Result
|
47
|
+
- If the return value is a list of Results eg [Success(1), Success(2)] it
|
48
|
+
will condense those results into a single Result eg Success([1,2]). If any
|
49
|
+
of the results in the list are Failures eg [Success(1), Failure(2)], It
|
50
|
+
will return the first failure encountered eg Failure(2). Changing a list
|
51
|
+
of Monads (Results) into a Monad containing a list of values, is called
|
52
|
+
'traversing'
|
53
|
+
- If the return value is already a `Dry::Monad::Result`, it will not wrap the
|
54
|
+
result
|
55
|
+
|
56
|
+
#### Wrapping an error in a Failure
|
47
57
|
```
|
48
58
|
class Divide < Dryer::Services::ResultService
|
49
59
|
def initialize(a, b)
|
@@ -63,23 +73,41 @@ class Divide < Dryer::Services::ResultService
|
|
63
73
|
attr_reader :a, :b
|
64
74
|
end
|
65
75
|
|
66
|
-
|
67
|
-
|
76
|
+
Divide.call(4,2) # returns Dry::Monads::Success(2)
|
77
|
+
Divide.call(4,0) # returns Dry::Monads::Failure("Can not divide by zero")
|
78
|
+
```
|
79
|
+
|
80
|
+
#### Traversing a list of results
|
81
|
+
```
|
82
|
+
class DivideMany < Dryer::Services::ResultService
|
83
|
+
def initialize(a, b)
|
84
|
+
@a = a
|
85
|
+
@b = b
|
86
|
+
end
|
87
|
+
|
88
|
+
def call
|
89
|
+
# each call to Divide returns a Result
|
90
|
+
# so we are returning a list of Results
|
91
|
+
a.map { |x| Divide.call(x,b) }
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
attr_reader :a, :b
|
96
|
+
end
|
97
|
+
|
98
|
+
DivideMany.call([2,4,6],2) # returns Dry::Monads::Success([1,2,3])
|
99
|
+
DivideMany.call([2,4,6],0) # returns Dry::Monads::Failure("Can not divide by zero")
|
68
100
|
```
|
69
101
|
|
70
102
|
## Advantages
|
71
103
|
Using the Service pattern can help to make code more modular, and make it easier
|
72
104
|
to separate data modeling from transformations.
|
73
105
|
|
74
|
-
|
75
|
-
This gem is set up to be developed using [Nix](https://nixos.org/)
|
76
|
-
|
77
|
-
`make env`
|
78
|
-
|
79
|
-
commands
|
80
|
-
|
81
|
-
If you don't want to use nix, all the scripts can be run directly from the
|
82
|
-
`scripts` directory.
|
106
|
+
## Development
|
107
|
+
This gem is set up to be developed using [Nix](https://nixos.org/) and
|
108
|
+
[ruby_gem_dev_shell](https://github.com/jbernie2/ruby_gem_dev_shell)
|
109
|
+
Once you have nix installed you can run `make env` to enter the development
|
110
|
+
environment and then `make` to see the list of available commands
|
83
111
|
|
84
112
|
## Contributing
|
85
113
|
Please create a github issue to report any problems using the Gem.
|
data/dryer_services.gemspec
CHANGED
@@ -15,6 +15,11 @@ module Dryer
|
|
15
15
|
result
|
16
16
|
elsif result.is_a?(StandardError)
|
17
17
|
Dry::Monads::Failure(result)
|
18
|
+
elsif result.is_a?(Array) && result.all?{ |x| x.is_a?(Dry::Monads::Result) }
|
19
|
+
Dry::Monads::List[*result]
|
20
|
+
.typed(Dry::Monads::Result)
|
21
|
+
.traverse
|
22
|
+
.fmap(&:value)
|
18
23
|
else
|
19
24
|
Dry::Monads::Success(result)
|
20
25
|
end
|