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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3fc730ff5d7dc75aed6d18e68f50f2de371535271974ed5d12d6d08ad3cf5207
4
- data.tar.gz: 61f6a0e307a4d0b9e7104a870b53ffc234f743515e67ef9a6232ce6f7a15648f
3
+ metadata.gz: 8517d9874a4dacf5dde964d7e89a133a76946db8f46b44cdd70eebf755726007
4
+ data.tar.gz: 2f07cb9670f4edc93622582bc876104c1a4346526b1acbb56b14b9e03443a693
5
5
  SHA512:
6
- metadata.gz: 1ada4c583fe9f5eded4af12b4bdda7e003376ae55d79de3d3609d9ee19d3d0e4c56483317139e9cee6fa43bf05a0af4df7a53741f01ca7dae6c97999e9649cd6
7
- data.tar.gz: 9cbf2836e9b0dde5377c7fc576300dc0894bce93697f4649445f238235b42daf1366820aa8535ec0da1b228fc5c67192b564cbd1b13601d9461105793d2409b1
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 Example
41
-
42
- Result Service wraps the value returned from `call` in a `Dry::Monad::Result`.
43
- If the return value is an Error, it will return a Failure. If the return value
44
- is already a `Dry::Monad::Result`, it will not wrap the result, otherwise it
45
- will wrap the result in a `Dry::Monads::Success`
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
- Add.call(4,2) # returns Dry::Monads::Success(2)
67
- Add.call(4,0) # returns Dry::Monads::Failure("Can not divide by zero")
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
- # Development
75
- This gem is set up to be developed using [Nix](https://nixos.org/)
76
- Once you have nix installed you can run
77
- `make env`
78
- to enter the development environment. Then run `make` to see other available
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.
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'dryer_services'
3
- spec.version = "1.2.0"
3
+ spec.version = "2.0.1"
4
4
  spec.authors = ['John Bernier']
5
5
  spec.email = ['john.b.bernier@gmail.com']
6
6
  spec.summary = 'A service object leveraging the dry-monads gem'
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dryer_services
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Bernier