u-service 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +16 -9
- data/lib/micro/service/pipeline.rb +33 -8
- data/lib/micro/service/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b8bd72203e123ab248a7b0a510d1eb7f457d3b68a04bc257cdc08f8e6a949ab
|
4
|
+
data.tar.gz: 022a4b90b46b1d10a238b37bc9a168b984f21f9d75ab144d5c97159b864b04c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9adc08c22019f4aab1559bdd017e1b92775acd612d102e27bf9a3726c9a3c88eacd250a556cfc11bf40da95dca85b08711b075c5f85806480dc15d09baacbf73
|
7
|
+
data.tar.gz: 68606ccb04d38d3ccd5549242a9faa7bc0004c751f4d72d834c9adfd649d04723b7267b3b8e84a57e2c54da01b5d9e6074bc93f0ad0537674344b529706b562a
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -12,7 +12,7 @@ Create simple and powerful service objects.
|
|
12
12
|
- [Required Ruby version](#required-ruby-version)
|
13
13
|
- [Installation](#installation)
|
14
14
|
- [Usage](#usage)
|
15
|
-
- [How to create a
|
15
|
+
- [How to create a Service Object?](#how-to-create-a-service-object)
|
16
16
|
- [How to use the Service Object result hooks?](#how-to-use-the-service-object-result-hooks)
|
17
17
|
- [How to create a pipeline of Service Objects?](#how-to-create-a-pipeline-of-service-objects)
|
18
18
|
- [What is a strict Service Object?](#what-is-a-strict-service-object)
|
@@ -44,7 +44,7 @@ Or install it yourself as:
|
|
44
44
|
|
45
45
|
## Usage
|
46
46
|
|
47
|
-
### How to create a
|
47
|
+
### How to create a Service Object?
|
48
48
|
|
49
49
|
```ruby
|
50
50
|
class Multiply < Micro::Service::Base
|
@@ -67,6 +67,10 @@ result = Multiply.call(a: 2, b: 2)
|
|
67
67
|
p result.success? # true
|
68
68
|
p result.value # 4
|
69
69
|
|
70
|
+
# Note:
|
71
|
+
# The result of a Micro::Service#call
|
72
|
+
# is an instance of Micro::Service::Result
|
73
|
+
|
70
74
|
#----------------------------#
|
71
75
|
# Calling a service instance #
|
72
76
|
#----------------------------#
|
@@ -164,10 +168,13 @@ Add2ToAllNumbers = Micro::Service::Pipeline[
|
|
164
168
|
Steps::Add2
|
165
169
|
]
|
166
170
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
+
# An alternative way to declare pipelines within classes.
|
172
|
+
|
173
|
+
class DoubleAllNumbers
|
174
|
+
include Micro::Service::Pipeline
|
175
|
+
|
176
|
+
pipeline Steps::ConvertToNumbers, Steps::Double
|
177
|
+
end
|
171
178
|
|
172
179
|
result = Add2ToAllNumbers.call(relation: %w[1 1 2 2 3 4])
|
173
180
|
|
@@ -200,7 +207,7 @@ Double.call({})
|
|
200
207
|
|
201
208
|
### How to validate Service Object attributes?
|
202
209
|
|
203
|
-
Note: To do this your application must have the
|
210
|
+
Note: To do this your application must have the [activemodel >= 3.2](https://rubygems.org/gems/activemodel) as a dependency.
|
204
211
|
|
205
212
|
```ruby
|
206
213
|
#
|
@@ -213,9 +220,9 @@ class Multiply < Micro::Service::Base
|
|
213
220
|
validates :a, :b, presence: true, numericality: true
|
214
221
|
|
215
222
|
def call!
|
216
|
-
return
|
223
|
+
return Failure(errors: self.errors) unless valid?
|
217
224
|
|
218
|
-
|
225
|
+
Success(number: a * b)
|
219
226
|
end
|
220
227
|
end
|
221
228
|
|
@@ -4,6 +4,17 @@ module Micro
|
|
4
4
|
module Service
|
5
5
|
module Pipeline
|
6
6
|
class Reducer
|
7
|
+
INVALID_SERVICES =
|
8
|
+
'argument must be a collection of `Micro::Service::Base` classes'.freeze
|
9
|
+
|
10
|
+
def self.build(args)
|
11
|
+
services = Array(args)
|
12
|
+
|
13
|
+
raise ArgumentError, INVALID_SERVICES if services.any? { |klass| !(klass < ::Micro::Service::Base) }
|
14
|
+
|
15
|
+
new(services)
|
16
|
+
end
|
17
|
+
|
7
18
|
def initialize(services)
|
8
19
|
@services = services
|
9
20
|
end
|
@@ -25,19 +36,33 @@ module Micro
|
|
25
36
|
|
26
37
|
private_constant :Reducer
|
27
38
|
|
28
|
-
|
29
|
-
|
39
|
+
module Macros
|
40
|
+
def pipeline(*args)
|
41
|
+
@pipeline = Reducer.build(args)
|
42
|
+
end
|
30
43
|
|
31
|
-
|
32
|
-
|
44
|
+
def pipeline_call(options)
|
45
|
+
@pipeline.call(options)
|
46
|
+
end
|
47
|
+
|
48
|
+
def call(options={})
|
49
|
+
new(options).call
|
50
|
+
end
|
33
51
|
end
|
34
52
|
|
35
|
-
|
36
|
-
services = Array(args)
|
53
|
+
private_constant :Macros
|
37
54
|
|
38
|
-
|
55
|
+
def self.[](*args)
|
56
|
+
Reducer.build(args)
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.included(base)
|
60
|
+
base.extend(Macros)
|
61
|
+
base.class_eval('def initialize(options); @options = options; end')
|
62
|
+
end
|
39
63
|
|
40
|
-
|
64
|
+
def call
|
65
|
+
self.class.pipeline_call(@options)
|
41
66
|
end
|
42
67
|
end
|
43
68
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: u-service
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-08-
|
11
|
+
date: 2019-08-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: u-attributes
|