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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3fc730ff5d7dc75aed6d18e68f50f2de371535271974ed5d12d6d08ad3cf5207
4
- data.tar.gz: 61f6a0e307a4d0b9e7104a870b53ffc234f743515e67ef9a6232ce6f7a15648f
3
+ metadata.gz: 5da96272749622ca4b98bae9f6d151a56826dc87641c2e5cc609f93e46a4dd13
4
+ data.tar.gz: a40fb9f8d0640629cdc1c9b6424786cbc3509169b448b66dfc328f079ffccb4b
5
5
  SHA512:
6
- metadata.gz: 1ada4c583fe9f5eded4af12b4bdda7e003376ae55d79de3d3609d9ee19d3d0e4c56483317139e9cee6fa43bf05a0af4df7a53741f01ca7dae6c97999e9649cd6
7
- data.tar.gz: 9cbf2836e9b0dde5377c7fc576300dc0894bce93697f4649445f238235b42daf1366820aa8535ec0da1b228fc5c67192b564cbd1b13601d9461105793d2409b1
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 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.0"
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'
@@ -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
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.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Bernier