u-service 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a21e14f14ad8d4b658660c94ed2e3687c4b589531a61f11fee63c9fcc0810615
4
- data.tar.gz: e504198f717114b9fd6794b795381ebfced5a6e29bda56ad83cc1f055296efc6
3
+ metadata.gz: 8b8bd72203e123ab248a7b0a510d1eb7f457d3b68a04bc257cdc08f8e6a949ab
4
+ data.tar.gz: 022a4b90b46b1d10a238b37bc9a168b984f21f9d75ab144d5c97159b864b04c9
5
5
  SHA512:
6
- metadata.gz: 74d66f07259c0d31c1469d70bd9a89190fafce614bdc4adced7e2e8a4dba07859483f020d90a87850804485f44e468835d481263c027fe86e4bbb9eee8af15ff
7
- data.tar.gz: c18b4b93cac117a7a5ea48d5bca5ef17811d62eedd18419a75d40ba6559b2518e5d7cb0c745dc38621034c0c8f802f247c602e4e6acb8ea1dee4b1365b5a25bc
6
+ metadata.gz: 9adc08c22019f4aab1559bdd017e1b92775acd612d102e27bf9a3726c9a3c88eacd250a556cfc11bf40da95dca85b08711b075c5f85806480dc15d09baacbf73
7
+ data.tar.gz: 68606ccb04d38d3ccd5549242a9faa7bc0004c751f4d72d834c9adfd649d04723b7267b3b8e84a57e2c54da01b5d9e6074bc93f0ad0537674344b529706b562a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- u-service (0.7.0)
4
+ u-service (0.8.0)
5
5
  u-attributes (~> 1.1)
6
6
 
7
7
  GEM
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 basic Service Object?](#how-to-create-a-basic-service-object)
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 basic Service Object?
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
- DoubleAllNumbers = Micro::Service::Pipeline[
168
- Steps::ConvertToNumbers,
169
- Steps::Double
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 (activemodel >= 3.2)(https://rubygems.org/gems/activemodel) as a dependency.
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 Success(number: a * b) if valid?
223
+ return Failure(errors: self.errors) unless valid?
217
224
 
218
- Failure(errors: self.errors)
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
- INVALID_SERVICES =
29
- 'argument must be a collection of `Micro::Service::Base` classes'.freeze
39
+ module Macros
40
+ def pipeline(*args)
41
+ @pipeline = Reducer.build(args)
42
+ end
30
43
 
31
- def self.[](*args)
32
- self.new(args)
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
- def self.new(args)
36
- services = Array(args)
53
+ private_constant :Macros
37
54
 
38
- raise ArgumentError, INVALID_SERVICES if services.any? { |klass| !(klass < ::Micro::Service::Base) }
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
- Reducer.new(services)
64
+ def call
65
+ self.class.pipeline_call(@options)
41
66
  end
42
67
  end
43
68
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Micro
4
4
  module Service
5
- VERSION = '0.7.0'.freeze
5
+ VERSION = '0.8.0'.freeze
6
6
  end
7
7
  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.7.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-06 00:00:00.000000000 Z
11
+ date: 2019-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: u-attributes