dryer_services 1.2.0 → 2.0.1
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 +6 -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: 8517d9874a4dacf5dde964d7e89a133a76946db8f46b44cdd70eebf755726007
|
4
|
+
data.tar.gz: 2f07cb9670f4edc93622582bc876104c1a4346526b1acbb56b14b9e03443a693
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5753a1ae9d7a04c597d3a23aa41668cb4d6245ffb577696f69672cd9186026eda7e074438d1a17dc2598b2867f093154b0a742769efb186f6966c620836e9d52
|
7
|
+
data.tar.gz: d6c06c481ee47355b128784f839859f4bbab288ae87d0587097e28ad3f2b45ec2a304c5c80542f3494f7187d971917cfc86617ec91cc9a390e8d8d76145e2140
|
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
@@ -5,6 +5,7 @@ module Dryer
|
|
5
5
|
module Services
|
6
6
|
module Services
|
7
7
|
class ResultService < SimpleService
|
8
|
+
include Dry::Monads[:result]
|
8
9
|
|
9
10
|
def self.call(*args)
|
10
11
|
wrap_result(super(*args))
|
@@ -15,6 +16,11 @@ module Dryer
|
|
15
16
|
result
|
16
17
|
elsif result.is_a?(StandardError)
|
17
18
|
Dry::Monads::Failure(result)
|
19
|
+
elsif result.is_a?(Array) && result.all?{ |x| x.is_a?(Dry::Monads::Result) }
|
20
|
+
Dry::Monads::List[*result]
|
21
|
+
.typed(Dry::Monads::Result)
|
22
|
+
.traverse
|
23
|
+
.fmap(&:value)
|
18
24
|
else
|
19
25
|
Dry::Monads::Success(result)
|
20
26
|
end
|