bbservices 3.0.1 → 3.1.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 +4 -4
- data/lib/bbservices.rb +10 -0
- data/lib/bbservices/service.rb +138 -55
- data/lib/bbservices/service_chain.rb +92 -0
- data/lib/bbservices/service_provider.rb +40 -13
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 662a72a39dbe2e481513c47eba00edcde5288c6018aea566c881c5834c4020ea
|
4
|
+
data.tar.gz: fdfb42ddcd57afa106896271602c3aa715ac5d5093835b289de48de868e0ff1a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 95a00382d51a488d15de9a3260793966052a8de0a80b24cf1b7eb25902f27438c67b5b997349e3762081b02423d8431c27e2c112c36f898a0ac3d3761aa61e40
|
7
|
+
data.tar.gz: 2248179e4cd90ad84e6f1af0bcf5c9e8c705f5d5fcabb918c6f690a58073d4d736deb4f2f85e1887aa469975fb9b7fd51fadd1718c908eb085820e07fa557ce6
|
data/lib/bbservices.rb
CHANGED
@@ -1,4 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative 'bbservices/service'
|
4
|
+
require_relative 'bbservices/service_chain'
|
4
5
|
require_relative 'bbservices/service_provider'
|
6
|
+
|
7
|
+
# The BBServices namespace.
|
8
|
+
module BBServices
|
9
|
+
def self.chain(params = {}, &block)
|
10
|
+
BBServices::ServiceChain.new.tap do |service_chain|
|
11
|
+
service_chain.chain(params, &block)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/bbservices/service.rb
CHANGED
@@ -1,68 +1,78 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'service_chain'
|
4
|
+
|
5
|
+
# The BBServices namespace.
|
3
6
|
module BBServices
|
4
|
-
|
5
|
-
|
7
|
+
# Error thrown when a Hash type isn't given
|
8
|
+
class ServiceHashTypeError < StandardError
|
9
|
+
def message
|
10
|
+
'Params need to be a Hash'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# The lightweight service object provided by BBServices.
|
6
15
|
class Service
|
7
16
|
attr_reader :params, :object, :error
|
8
17
|
|
9
18
|
class << self
|
10
|
-
|
19
|
+
|
20
|
+
# Creates the service instances and calls run upon said instance
|
21
|
+
# @param [Hash] params The params which are passed to the service
|
22
|
+
# @param [Block] block The block which will be called upon the service finishing running
|
23
|
+
# @return [BBServices.Service] returns the service instance
|
11
24
|
def run(params = {}, &block)
|
12
25
|
new(params).tap do |service|
|
13
26
|
service.run(&block)
|
14
27
|
end
|
15
28
|
end
|
16
29
|
|
17
|
-
|
18
|
-
#
|
30
|
+
# Creates the service instances and calls run! upon said instance
|
31
|
+
# @param [Hash] params The params which are passed to the service
|
32
|
+
# @param [Block] block The block which will be called upon the service finishing running
|
33
|
+
# @return [BBServices.Service] returns the service instance
|
19
34
|
def run!(params = {}, &block)
|
20
35
|
new(params).tap do |service|
|
21
36
|
service.run!(&block)
|
22
37
|
end
|
23
38
|
end
|
24
39
|
|
25
|
-
|
26
|
-
|
40
|
+
# An alias to {BBServices::Service}'s run method
|
41
|
+
alias call run
|
42
|
+
|
43
|
+
# An alias to {BBServices::Service}'s run! method
|
44
|
+
alias call! run!
|
45
|
+
|
46
|
+
# Sets the service class on the Class. Please note this is an internal method.
|
47
|
+
# @param [Class] klass The class which will be set as the service_class
|
48
|
+
# @return [BBServices.Service] returns the service instance
|
27
49
|
def service_class(klass)
|
28
50
|
@service_class = klass
|
29
51
|
end
|
30
52
|
|
31
|
-
|
53
|
+
# Gets the current service class
|
54
|
+
# @return [Class] returns the service class. Please note this is an internal method.
|
55
|
+
def internal_service_class
|
32
56
|
@service_class
|
33
57
|
end
|
34
58
|
end
|
35
59
|
|
60
|
+
# Initializes the service with a hash of params
|
61
|
+
# @param [Hash] params The params which are passed to the service
|
36
62
|
def initialize(params = {})
|
37
|
-
##
|
38
|
-
# The object which will be assigned to the service
|
39
63
|
@object = nil
|
40
|
-
|
41
|
-
##
|
42
|
-
# The state of success, was the service successful
|
43
64
|
@successful = false
|
44
|
-
|
45
|
-
##
|
46
|
-
# The state of the run, has the service being ran
|
47
65
|
@ran = false
|
48
|
-
|
49
|
-
##
|
50
|
-
# The error that has been throw by the service
|
51
66
|
@error = nil
|
52
|
-
|
53
|
-
##
|
54
|
-
# The service class stored on the instance. This will override the
|
55
|
-
# service class set statically
|
56
67
|
@service_class = nil
|
57
68
|
|
58
|
-
##
|
59
|
-
# The params passed to the resource
|
60
69
|
@params = params
|
61
70
|
end
|
62
71
|
|
63
|
-
|
64
|
-
#
|
65
|
-
#
|
72
|
+
# Runs the service using 'safe' execution. The @run variable will be set to true, initialize_service and run_service
|
73
|
+
# will then be called.
|
74
|
+
# @param [Block] block The block which will be called upon the service finishing running
|
75
|
+
# @return [BBServices.Service] returns the service instance
|
66
76
|
def run(&block)
|
67
77
|
set_ran
|
68
78
|
begin
|
@@ -76,8 +86,10 @@ module BBServices
|
|
76
86
|
end
|
77
87
|
end
|
78
88
|
|
79
|
-
|
80
|
-
#
|
89
|
+
# Runs the service using 'unsafe' execution. The @run variable will be set to true,
|
90
|
+
# initialize_service and run_service will then be called.
|
91
|
+
# @param [Block] block The block which will be called upon the service finishing running
|
92
|
+
# @return [BBServices.Service] returns the service instance
|
81
93
|
def run!(&block)
|
82
94
|
set_ran
|
83
95
|
begin
|
@@ -91,102 +103,173 @@ module BBServices
|
|
91
103
|
end
|
92
104
|
end
|
93
105
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
106
|
+
# An alias to {BBServices::Service}'s run method
|
107
|
+
alias call run
|
108
|
+
|
109
|
+
# An alias to {BBServices::Service}'s run! method
|
110
|
+
alias call! run!
|
111
|
+
|
112
|
+
# Sets the service_class on the instance. This will override the self.class.internal_service_class.
|
113
|
+
# @param [Class] new_service_class The new service class.
|
114
|
+
def set_service_class(new_service_class)
|
115
|
+
@service_class = new_service_class
|
116
|
+
end
|
117
|
+
|
118
|
+
# Sets the service_class on the instance. This will override the self.class.internal_service_class.
|
119
|
+
# @param [Class] new_service_class The new service class.
|
120
|
+
def service_class=(new_service_class)
|
121
|
+
set_service_class(new_service_class)
|
98
122
|
end
|
99
123
|
|
100
|
-
|
101
|
-
#
|
124
|
+
# Gets the current service class. This will use @service_class if set, otherwise will fallback to
|
125
|
+
# self.class.internal_service_class.
|
126
|
+
# @return [Class] new_service_class The new service class.
|
102
127
|
def service_class
|
103
|
-
@service_class ||= self.class.
|
128
|
+
@service_class ||= self.class.internal_service_class
|
104
129
|
end
|
105
130
|
|
106
|
-
|
107
|
-
|
131
|
+
# Sets the params variable (@params) on the service.
|
132
|
+
# @param [Hash] new_params The new params Hash.
|
133
|
+
def set_params(new_params)
|
134
|
+
raise BBServices::ServiceHashTypeError unless new_params.is_a?(Hash)
|
135
|
+
|
136
|
+
@params = new_params
|
108
137
|
end
|
109
138
|
|
110
|
-
|
111
|
-
|
139
|
+
# Sets the params variable (@params) on the service.
|
140
|
+
# @param [Hash] new_params The new params Hash.
|
141
|
+
def params=(new_params)
|
142
|
+
set_params(new_params)
|
112
143
|
end
|
113
144
|
|
145
|
+
# Gets a single param using a key
|
146
|
+
# @param [String/Symbol] key The key which is used to find the param
|
147
|
+
# @return [Hash] The param found using the key
|
114
148
|
def param_for(key)
|
115
149
|
param(key)
|
116
150
|
end
|
117
151
|
|
152
|
+
# Gets a single param using a key
|
153
|
+
# @param [String/Symbol] key The key which is used to find the param
|
154
|
+
# @return [Hash] The param found using the key
|
118
155
|
def param(key)
|
119
156
|
@params[key] if @params
|
120
157
|
end
|
121
158
|
|
159
|
+
# Gets the number of params
|
160
|
+
# @return [Number] The number of params
|
122
161
|
def number_of_params
|
123
162
|
@params ? @params.length : 0
|
124
163
|
end
|
125
164
|
|
165
|
+
# Returns true/false on if the service has been ran
|
166
|
+
# @return [Boolean] True/False value on if the service has been ran
|
126
167
|
def ran?
|
127
168
|
@ran
|
128
169
|
end
|
129
170
|
|
171
|
+
# Returns true / false if the service has any params
|
172
|
+
# @return [Boolean] true/false if the service has any params
|
173
|
+
def params?
|
174
|
+
!!(@params && @params.length)
|
175
|
+
end
|
176
|
+
|
177
|
+
# An alias to {BBServices::Service}'s ran? method
|
178
|
+
alias run? ran?
|
179
|
+
|
180
|
+
# Returns true/false on if the service did succeed.
|
181
|
+
# @return [Boolean] true/false on if the service did succeed.
|
130
182
|
def succeeded?
|
131
183
|
successful?
|
132
184
|
end
|
133
185
|
|
186
|
+
# Returns true/false on if the service was successful.
|
187
|
+
# @return [Boolean] true/false on if the service was successful.
|
134
188
|
def successful?
|
135
189
|
@successful
|
136
190
|
end
|
137
191
|
|
192
|
+
# Returns true/false on if the service was unsuccessful. This will always be the inverse of successful?
|
193
|
+
# @return [Boolean] true/false on if the service failed.
|
138
194
|
def failed?
|
139
195
|
!succeeded?
|
140
196
|
end
|
141
197
|
|
142
|
-
|
143
|
-
|
198
|
+
# Calls the given block if the service was successful
|
199
|
+
def success
|
200
|
+
yield(self) if succeeded?
|
144
201
|
end
|
145
202
|
|
146
|
-
|
147
|
-
|
203
|
+
# Calls the given block if the service failed
|
204
|
+
def failure
|
205
|
+
yield(self) if failed?
|
148
206
|
end
|
149
207
|
|
150
|
-
|
151
|
-
|
208
|
+
# Calls success on success?, failure on !success?
|
209
|
+
# @param [Proc] success The proc to be called upon a successful service
|
210
|
+
# @param [Proc] failure
|
211
|
+
# @return [Boolean] true/false if the service has any params
|
212
|
+
def on(success: proc {}, failure: proc {})
|
213
|
+
if successful?
|
214
|
+
success.call
|
215
|
+
else
|
216
|
+
failure.call
|
217
|
+
end
|
152
218
|
end
|
153
219
|
|
154
|
-
|
155
|
-
|
220
|
+
# Returns true / false if the service threw an error
|
221
|
+
# @return [Boolean] true/false on if an error has occurred
|
222
|
+
def error?
|
223
|
+
!!@error
|
156
224
|
end
|
157
225
|
|
158
226
|
protected
|
159
227
|
|
228
|
+
# Called upon run / run!, should be overridden in order to setup any variable
|
229
|
+
# initalization
|
160
230
|
def initialize_service() end
|
161
231
|
|
232
|
+
# Called upon run, should be overridden in order to setup any variable
|
233
|
+
# initalization
|
162
234
|
def run_service
|
163
235
|
set_successful
|
164
236
|
set_object(nil)
|
165
237
|
end
|
166
238
|
|
239
|
+
# Called upon run, should be overridden in order to setup any variable
|
240
|
+
# initalization
|
167
241
|
def run_service!
|
168
242
|
set_successful
|
169
243
|
set_object(nil)
|
170
244
|
end
|
171
245
|
|
246
|
+
# Sets the @object instance variable, the object will be accessible via the .object property outside of the service
|
247
|
+
# @param obj The object which will be assigned to the @object instance variable
|
172
248
|
def set_object(obj)
|
173
249
|
@object = obj
|
174
250
|
end
|
175
251
|
|
176
|
-
|
177
|
-
|
252
|
+
private
|
253
|
+
|
254
|
+
# Sets the internal @ran instance variable
|
255
|
+
# @param [Boolean] ran True / False if the service has been ran
|
256
|
+
def set_ran(ran = true)
|
257
|
+
@ran = ran
|
178
258
|
end
|
179
259
|
|
260
|
+
# Sets the internal @successful instance variable
|
261
|
+
# @param [Boolean] successful True / False if the service has been successful
|
180
262
|
def set_successful(successful = true)
|
181
263
|
@successful = successful
|
182
264
|
end
|
183
265
|
|
184
|
-
|
185
|
-
|
266
|
+
# Sets the internal @error instance variable
|
267
|
+
# @param [Error] error The error to be assigned
|
268
|
+
def set_error(error)
|
269
|
+
@error = error
|
186
270
|
end
|
187
271
|
|
188
|
-
|
189
|
-
|
272
|
+
# Calls the block which has been passed
|
190
273
|
def call_block
|
191
274
|
yield(self) if block_given?
|
192
275
|
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# The BBServices namespace.
|
4
|
+
module BBServices
|
5
|
+
# Container for chained services.
|
6
|
+
class ServiceChain
|
7
|
+
|
8
|
+
attr_reader :services
|
9
|
+
|
10
|
+
# Initializes the ServiceChain
|
11
|
+
def initialize
|
12
|
+
@services = []
|
13
|
+
@successful = true
|
14
|
+
end
|
15
|
+
|
16
|
+
def chain(params = {})
|
17
|
+
tap do |_service_chain|
|
18
|
+
if @successful
|
19
|
+
service = yield(params, self, previous_service)
|
20
|
+
process_service(service)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def previous_service
|
26
|
+
return nil unless @services.length
|
27
|
+
|
28
|
+
@services.last
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns true/false on if the chain did succeed.
|
32
|
+
# @return [Boolean] true/false on if the chain did succeed.
|
33
|
+
def succeeded?
|
34
|
+
successful?
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns true/false on if the chain was successful.
|
38
|
+
# @return [Boolean] true/false on if the chain was successful.
|
39
|
+
def successful?
|
40
|
+
@successful
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns true/false on if the chain was unsuccessful. This will always be the inverse of successful?
|
44
|
+
# @return [Boolean] true/false on if the chain failed.
|
45
|
+
def failed?
|
46
|
+
!succeeded?
|
47
|
+
end
|
48
|
+
|
49
|
+
# Calls the given block if the chain was successful
|
50
|
+
def success
|
51
|
+
yield(self) if succeeded?
|
52
|
+
end
|
53
|
+
|
54
|
+
# Calls the given block if the chain failed
|
55
|
+
def failure
|
56
|
+
yield(self) if failed?
|
57
|
+
end
|
58
|
+
|
59
|
+
# Calls success on success?, failure on !success?
|
60
|
+
# @param [Proc] success The proc to be called upon a successful chain
|
61
|
+
# @param [Proc] failure
|
62
|
+
# @return [Boolean] true/false if the chain has any params
|
63
|
+
def on(success: proc {}, failure: proc {})
|
64
|
+
if successful?
|
65
|
+
success.call
|
66
|
+
else
|
67
|
+
failure.call
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Returns true / false if the chain threw an error
|
72
|
+
# @return [Boolean] true/false on if an error has occurred
|
73
|
+
def error?
|
74
|
+
previous_service ? previous_service.error? : false
|
75
|
+
end
|
76
|
+
|
77
|
+
def error
|
78
|
+
previous_service ? previous_service.error : nil
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def process_service(service)
|
84
|
+
if service.is_a?(BBServices::Service)
|
85
|
+
@services << service
|
86
|
+
@successful = service.successful?
|
87
|
+
else
|
88
|
+
@successful = !!service
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -1,36 +1,63 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# The BBServices namespace.
|
3
4
|
module BBServices
|
4
|
-
|
5
|
-
#
|
5
|
+
# Module to allow external classes (Namely controllers) to interact with underlying service objects
|
6
6
|
module ServiceProvider
|
7
7
|
def self.included(base)
|
8
8
|
base.class_eval do
|
9
|
-
|
10
|
-
#
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
# Creates a service with a given type and params, the service instance will not be ran.
|
10
|
+
# Sets the @service instance on the object which includes this provider.
|
11
|
+
# @param [Class] service_type The class which should be instanciated
|
12
|
+
# @param [Hash] service_params The params which will be passed to the service
|
13
|
+
# @return [{BBServices::Service}] The service type instance
|
14
|
+
def create_service(service_type, service_params = {})
|
15
|
+
@service = service_type.new.tap do |new_service|
|
16
|
+
new_service.set_params(service_params)
|
14
17
|
end
|
15
18
|
end
|
16
19
|
|
17
|
-
|
18
|
-
#
|
20
|
+
# Creates a service with a given type and params, the service instance will be ran using the run method.
|
21
|
+
# Sets the @service instance on the object which includes this provider.
|
22
|
+
# @param [Class] service_type The class which should be instanciated
|
23
|
+
# @param [Hash] service_params The params which will be passed to the service
|
24
|
+
# @param [Block] block The block to call upon running of the service is complete
|
25
|
+
# @return [{BBServices::Service}] The service type instance
|
19
26
|
def run_service(service_type, service_params = {}, &block)
|
20
|
-
|
27
|
+
create_service(service_type, service_params).tap do |service|
|
21
28
|
service.service_class = service_type
|
22
29
|
service.run(&block)
|
23
30
|
end
|
24
31
|
end
|
25
32
|
|
26
|
-
|
27
|
-
#
|
33
|
+
# Creates a service with a given type and params, the service instance will be ran using the run! method.
|
34
|
+
# Sets the @service instance on the object which includes this provider.
|
35
|
+
# @param [Class] service_type The class which should be instanciated
|
36
|
+
# @param [Hash] service_params The params which will be passed to the service
|
37
|
+
# @param [Block] block The block to call upon running of the service is complete
|
38
|
+
# @return [{BBServices::Service}] The service type instance
|
28
39
|
def run_service!(service_type, service_params = {}, &block)
|
29
|
-
|
40
|
+
create_service(service_type, service_params).tap do |service|
|
30
41
|
service.service_class = service_type
|
31
42
|
service.run!(&block)
|
32
43
|
end
|
33
44
|
end
|
45
|
+
|
46
|
+
def chain_services(params = {}, &block)
|
47
|
+
@service_chain = BBServices.chain(params, &block)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Returns the {BBService::Service} instance currently stored within @service
|
51
|
+
# @return [{BBService::Service}] The current service
|
52
|
+
def service
|
53
|
+
@service
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns the {BBServices::ServiceChain} instance currently stored within @service_chain
|
57
|
+
# @return [{BBServices::ServiceChain}] The current service
|
58
|
+
def service_chain
|
59
|
+
@service_chain
|
60
|
+
end
|
34
61
|
end
|
35
62
|
end
|
36
63
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bbservices
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stuart Farnaby, Big Bear Studios
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-04
|
11
|
+
date: 2021-06-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: guard-rspec
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - '='
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 0.18.5
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: yard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.8.7.6
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.8.7.6
|
69
83
|
description:
|
70
84
|
email:
|
71
85
|
executables: []
|
@@ -74,6 +88,7 @@ extra_rdoc_files: []
|
|
74
88
|
files:
|
75
89
|
- lib/bbservices.rb
|
76
90
|
- lib/bbservices/service.rb
|
91
|
+
- lib/bbservices/service_chain.rb
|
77
92
|
- lib/bbservices/service_provider.rb
|
78
93
|
homepage: https://gitlab.com/big-bear-studios-open-source/bbservices
|
79
94
|
licenses:
|