dryer_services 1.1.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: ba13ef8192d27da5fda2b291efb698e4fc11638b2e46e8faad4108aeba7129fa
4
- data.tar.gz: 6624e849a4b7c36ae41900bf41aee44d5303fb1443f0654d49a13b3bce60450d
3
+ metadata.gz: 5da96272749622ca4b98bae9f6d151a56826dc87641c2e5cc609f93e46a4dd13
4
+ data.tar.gz: a40fb9f8d0640629cdc1c9b6424786cbc3509169b448b66dfc328f079ffccb4b
5
5
  SHA512:
6
- metadata.gz: d01980e96ed796b2b0c292fd1763a579daddb0aaf03f5ad74ddcf6d25a76109c9481d49b9cd00aa6c1496df95d13d6d6addd4d6784dff33cdbdced444275f028
7
- data.tar.gz: 5a9447e7d141ba65dc7398e55ba22a35f47330d8c4b6f70f9ec12619aa513e472167eda4142a77f7dc421781d58bdef12dbe66339e2d0d7108d982102a236fa0
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)
@@ -53,7 +63,7 @@ class Divide < Dryer::Services::ResultService
53
63
 
54
64
  def call
55
65
  if b == 0
56
- StandError.new("Can not divide by zero")
66
+ StandardError.new("Can not divide by zero")
57
67
  else
58
68
  a/b
59
69
  end
@@ -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.1.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.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Bernier
@@ -89,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  - !ruby/object:Gem::Version
90
90
  version: '0'
91
91
  requirements: []
92
- rubygems_version: 3.4.13
92
+ rubygems_version: 3.4.22
93
93
  signing_key:
94
94
  specification_version: 4
95
95
  summary: A service object leveraging the dry-monads gem