light-service 0.11.0 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rubocop.yml +6 -0
- data/.travis.yml +1 -1
- data/README.md +26 -13
- data/RELEASES.md +4 -0
- data/lib/light-service.rb +1 -0
- data/lib/light-service/context.rb +4 -1
- data/lib/light-service/organizer.rb +8 -0
- data/lib/light-service/organizer/with_reducer.rb +1 -0
- data/lib/light-service/organizer/with_reducer_factory.rb +11 -7
- data/lib/light-service/organizer/with_reducer_log_decorator.rb +2 -2
- data/lib/light-service/version.rb +1 -1
- data/light-service.gemspec +4 -4
- data/spec/acceptance/custom_log_from_organizer_spec.rb +60 -0
- data/spec/acceptance/fail_spec.rb +42 -16
- data/spec/acceptance/organizer/execute_spec.rb +1 -1
- data/spec/spec_helper.rb +2 -1
- data/spec/test_doubles.rb +12 -0
- metadata +26 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d2a2ead51dc9f98f9b676a62b94377c2d0395b008afa88f702fefeebfb2e44f4
|
4
|
+
data.tar.gz: 0c9a28ca3cd84a084c836beb8466e708d17ac112a0506db01d58b7c2af243546
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 938421a4c805d816d0592871427c39b58f1b01f5fd106e81f2f6d885c158eafeefb83c7230df001655ff635cb83d8a68b01f78d9cfb0ddf2bad9ced7c4a6290b
|
7
|
+
data.tar.gz: 9f2c68f84c666f0d1d1604e0d0a46ff03d0a7c1bd3109f9098a84d14e3a721c7cb14730c6045b91cc3ed314ff2e25119726e638e9443d4797eb9c47ee97b49c2
|
data/.rubocop.yml
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
+
require: rubocop-performance
|
2
|
+
|
1
3
|
AllCops:
|
4
|
+
TargetRubyVersion: 2.3
|
2
5
|
Exclude:
|
3
6
|
- 'lib/light-service.rb'
|
4
7
|
- 'vendor/bundle/**/*'
|
@@ -46,3 +49,6 @@ Metrics/BlockLength:
|
|
46
49
|
|
47
50
|
Layout/TrailingBlankLines:
|
48
51
|
Enabled: false
|
52
|
+
|
53
|
+
Layout/EndOfLine:
|
54
|
+
EnforcedStyle: lf
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -294,14 +294,16 @@ Consider this code:
|
|
294
294
|
class SomeOrganizer
|
295
295
|
extend LightService::Organizer
|
296
296
|
|
297
|
-
def call(ctx)
|
297
|
+
def self.call(ctx)
|
298
298
|
with(ctx).reduce(actions)
|
299
299
|
end
|
300
300
|
|
301
|
-
def actions
|
302
|
-
|
303
|
-
|
304
|
-
|
301
|
+
def self.actions
|
302
|
+
[
|
303
|
+
OneAction,
|
304
|
+
TwoAction,
|
305
|
+
ThreeAction
|
306
|
+
]
|
305
307
|
end
|
306
308
|
end
|
307
309
|
|
@@ -345,14 +347,16 @@ class SomeOrganizer
|
|
345
347
|
end
|
346
348
|
end)
|
347
349
|
|
348
|
-
def call(ctx)
|
350
|
+
def self.call(ctx)
|
349
351
|
with(ctx).reduce(actions)
|
350
352
|
end
|
351
353
|
|
352
|
-
def actions
|
353
|
-
|
354
|
-
|
355
|
-
|
354
|
+
def self.actions
|
355
|
+
[
|
356
|
+
OneAction,
|
357
|
+
TwoAction,
|
358
|
+
ThreeAction
|
359
|
+
]
|
356
360
|
end
|
357
361
|
end
|
358
362
|
|
@@ -451,9 +455,9 @@ class AnOrganizer
|
|
451
455
|
|
452
456
|
def self.call(order)
|
453
457
|
with(:order => order).reduce(
|
454
|
-
|
455
|
-
|
456
|
-
|
458
|
+
AnAction,
|
459
|
+
AnotherAction,
|
460
|
+
)
|
457
461
|
end
|
458
462
|
end
|
459
463
|
|
@@ -534,6 +538,15 @@ I, [DATE] INFO -- : [LightService] - ;-) <TestDoubles::MakesLatteAction> has de
|
|
534
538
|
I, [DATE] INFO -- : [LightService] - context message: Can't make a latte with a fatty milk like that!
|
535
539
|
```
|
536
540
|
|
541
|
+
You can specify the logger on the organizer level, so the organizer does not use the global logger.
|
542
|
+
|
543
|
+
```ruby
|
544
|
+
class FooOrganizer
|
545
|
+
extend LightService::Organizer
|
546
|
+
log_with Logger.new("/my/special.log")
|
547
|
+
end
|
548
|
+
```
|
549
|
+
|
537
550
|
## Error Codes
|
538
551
|
You can add some more structure to your error handling by taking advantage of error codes in the context.
|
539
552
|
Normally, when something goes wrong in your actions, you fail the process by setting the context to failure:
|
data/RELEASES.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
A brief list of new features and changes introduced with the specified version.
|
2
2
|
|
3
|
+
### 0.12.0
|
4
|
+
* [Per organizer logger](https://github.com/adomokos/light-service/pull/162)
|
5
|
+
* [Fix 'fail_and_return!' not accepting 'error_code' option](https://github.com/adomokos/light-service/pull/168)
|
6
|
+
|
3
7
|
### 0.11.0
|
4
8
|
* [Switch to 'each_with_object' in WithReducer](https://github.com/adomokos/light-service/pull/149).
|
5
9
|
|
data/lib/light-service.rb
CHANGED
@@ -88,7 +88,7 @@ module LightService
|
|
88
88
|
|
89
89
|
def fail_and_return!(*args)
|
90
90
|
fail!(*args)
|
91
|
-
throw(:jump_when_failed
|
91
|
+
throw(:jump_when_failed)
|
92
92
|
end
|
93
93
|
|
94
94
|
def fail_with_rollback!(message = nil, error_code = nil)
|
@@ -115,8 +115,10 @@ module LightService
|
|
115
115
|
|
116
116
|
def define_accessor_methods_for_keys(keys)
|
117
117
|
return if keys.nil?
|
118
|
+
|
118
119
|
keys.each do |key|
|
119
120
|
next if respond_to?(key.to_sym)
|
121
|
+
|
120
122
|
define_singleton_method(key.to_s) { fetch(key) }
|
121
123
|
define_singleton_method("#{key}=") { |value| self[key] = value }
|
122
124
|
end
|
@@ -161,6 +163,7 @@ module LightService
|
|
161
163
|
|
162
164
|
def check_nil(value)
|
163
165
|
return 'nil' unless value
|
166
|
+
|
164
167
|
"'#{value}'"
|
165
168
|
end
|
166
169
|
end
|
@@ -2,13 +2,17 @@ module LightService
|
|
2
2
|
module Organizer
|
3
3
|
class WithReducerFactory
|
4
4
|
def self.make(monitored_organizer)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
5
|
+
logger = monitored_organizer.logger ||
|
6
|
+
LightService::Configuration.logger
|
7
|
+
decorated = WithReducer.new
|
8
|
+
|
9
|
+
return decorated if logger.nil?
|
10
|
+
|
11
|
+
WithReducerLogDecorator.new(
|
12
|
+
monitored_organizer,
|
13
|
+
:decorated => decorated,
|
14
|
+
:logger => logger
|
15
|
+
)
|
12
16
|
end
|
13
17
|
end
|
14
18
|
end
|
@@ -5,10 +5,10 @@ module LightService
|
|
5
5
|
|
6
6
|
alias logged? logged
|
7
7
|
|
8
|
-
def initialize(organizer, decorated
|
8
|
+
def initialize(organizer, decorated: WithReducer.new, logger:)
|
9
9
|
@decorated = decorated
|
10
10
|
@organizer = organizer
|
11
|
-
@logger =
|
11
|
+
@logger = logger
|
12
12
|
@logged = false
|
13
13
|
end
|
14
14
|
|
data/light-service.gemspec
CHANGED
@@ -16,10 +16,10 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.require_paths = ["lib"]
|
17
17
|
gem.version = LightService::VERSION
|
18
18
|
|
19
|
-
gem.
|
20
|
-
|
19
|
+
gem.add_development_dependency("activesupport", ">= 5.2.2")
|
21
20
|
gem.add_development_dependency("rspec", "~> 3.0")
|
22
21
|
gem.add_development_dependency("simplecov", "~> 0.16.1")
|
23
|
-
gem.add_development_dependency("rubocop", "~> 0.
|
24
|
-
gem.add_development_dependency("
|
22
|
+
gem.add_development_dependency("rubocop", "~> 0.68.0")
|
23
|
+
gem.add_development_dependency("rubocop-performance", "~> 1.2.0")
|
24
|
+
gem.add_development_dependency("pry", "~> 0.12.2")
|
25
25
|
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Log from an organizer with a custom logger" do
|
4
|
+
context "when overriding the global LightService organizer" do
|
5
|
+
let(:global_logger_organizer) do
|
6
|
+
Class.new do
|
7
|
+
extend LightService::Organizer
|
8
|
+
|
9
|
+
def self.call(number)
|
10
|
+
with(:number => number).reduce(actions)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.actions
|
14
|
+
[
|
15
|
+
TestDoubles::AddsOneAction,
|
16
|
+
TestDoubles::AddsTwoAction,
|
17
|
+
TestDoubles::AddsThreeAction
|
18
|
+
]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
let(:global_logger_string) { StringIO.new }
|
24
|
+
|
25
|
+
let(:custom_logger_string) { StringIO.new }
|
26
|
+
let(:custom_logger_organizer) do
|
27
|
+
custom_logger = Logger.new(custom_logger_string)
|
28
|
+
|
29
|
+
Class.new do
|
30
|
+
extend LightService::Organizer
|
31
|
+
log_with custom_logger
|
32
|
+
|
33
|
+
def self.call(coffee, this_hot = :very_hot)
|
34
|
+
with(:milk => this_hot, :coffee => coffee)
|
35
|
+
.reduce(TestDoubles::MakesLatteAction,
|
36
|
+
TestDoubles::AddsTwoActionWithFetch)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
before do
|
42
|
+
@original_global_logger = LightService::Configuration.logger
|
43
|
+
LightService::Configuration.logger = Logger.new(global_logger_string)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "logs in own logger" do
|
47
|
+
global_logger_organizer.call(1)
|
48
|
+
custom_logger_organizer.call(:coffee => "Cappucino")
|
49
|
+
|
50
|
+
expect(custom_logger_string.string).to include("MakesLatteAction")
|
51
|
+
expect(custom_logger_string.string).to_not include("AddsOneAction")
|
52
|
+
expect(global_logger_string.string).to include("AddsOneAction")
|
53
|
+
expect(global_logger_string.string).to_not include("MakesLatteAction")
|
54
|
+
end
|
55
|
+
|
56
|
+
after do
|
57
|
+
LightService::Configuration.logger = @original_global_logger
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -1,24 +1,50 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
RSpec.describe "
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
3
|
+
RSpec.describe "fail_and_return!" do
|
4
|
+
describe "returns immediately from executed block" do
|
5
|
+
class FailAndReturnAction
|
6
|
+
extend LightService::Action
|
7
|
+
promises :one, :two
|
8
|
+
|
9
|
+
executed do |ctx|
|
10
|
+
ctx.one = 1
|
11
|
+
# Have to set it in Context
|
12
|
+
ctx.two = nil
|
13
|
+
|
14
|
+
ctx.fail_and_return!('Something went wrong')
|
15
|
+
ctx.two = 2
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns immediately from executed block" do
|
20
|
+
result = FailAndReturnAction.execute
|
21
|
+
|
22
|
+
expect(result).to be_failure
|
23
|
+
expect(result.two).to be_nil
|
15
24
|
end
|
16
25
|
end
|
17
26
|
|
18
|
-
|
19
|
-
|
27
|
+
describe "accepts error_code option" do
|
28
|
+
class FailAndReturnWithErrorCodeAction
|
29
|
+
extend LightService::Action
|
30
|
+
promises :one, :two
|
20
31
|
|
21
|
-
|
22
|
-
|
32
|
+
executed do |ctx|
|
33
|
+
ctx.one = 1
|
34
|
+
# Have to set it in Context
|
35
|
+
ctx.two = nil
|
36
|
+
|
37
|
+
ctx.fail_and_return!('Something went wrong', :error_code => 401)
|
38
|
+
ctx.two = 2
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it "returned context contains the error_code" do
|
43
|
+
result = FailAndReturnWithErrorCodeAction.execute
|
44
|
+
|
45
|
+
expect(result).to be_failure
|
46
|
+
expect(result.error_code).to eq 401
|
47
|
+
expect(result.two).to be_nil
|
48
|
+
end
|
23
49
|
end
|
24
50
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -15,8 +15,9 @@ end
|
|
15
15
|
require 'light-service'
|
16
16
|
require 'light-service/testing'
|
17
17
|
require 'ostruct'
|
18
|
-
require 'active_support/core_ext/string'
|
19
18
|
require 'pry'
|
20
19
|
require 'support'
|
20
|
+
require 'test_doubles'
|
21
|
+
require 'stringio'
|
21
22
|
|
22
23
|
I18n.enforce_available_locales = true
|
data/spec/test_doubles.rb
CHANGED
@@ -269,6 +269,18 @@ module TestDoubles
|
|
269
269
|
end
|
270
270
|
end
|
271
271
|
|
272
|
+
class AddsOne
|
273
|
+
extend LightService::Organizer
|
274
|
+
|
275
|
+
def call(ctx)
|
276
|
+
with(ctx).reduce(actions)
|
277
|
+
end
|
278
|
+
|
279
|
+
def self.actions
|
280
|
+
[AddsOneAction]
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
272
284
|
class AddsOneAction
|
273
285
|
extend LightService::Action
|
274
286
|
expects :number
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: light-service
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Attila Domokos
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-05-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
20
|
-
type: :
|
19
|
+
version: 5.2.2
|
20
|
+
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 5.2.2
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,28 +58,42 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 0.68.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.68.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop-performance
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.2.0
|
62
76
|
type: :development
|
63
77
|
prerelease: false
|
64
78
|
version_requirements: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
80
|
- - "~>"
|
67
81
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
82
|
+
version: 1.2.0
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: pry
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
87
|
- - "~>"
|
74
88
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
89
|
+
version: 0.12.2
|
76
90
|
type: :development
|
77
91
|
prerelease: false
|
78
92
|
version_requirements: !ruby/object:Gem::Requirement
|
79
93
|
requirements:
|
80
94
|
- - "~>"
|
81
95
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
96
|
+
version: 0.12.2
|
83
97
|
description: A service skeleton with an emphasis on simplicity
|
84
98
|
email:
|
85
99
|
- adomokos@gmail.com
|
@@ -136,6 +150,7 @@ files:
|
|
136
150
|
- spec/acceptance/after_actions_spec.rb
|
137
151
|
- spec/acceptance/around_each_spec.rb
|
138
152
|
- spec/acceptance/before_actions_spec.rb
|
153
|
+
- spec/acceptance/custom_log_from_organizer_spec.rb
|
139
154
|
- spec/acceptance/fail_spec.rb
|
140
155
|
- spec/acceptance/include_warning_spec.rb
|
141
156
|
- spec/acceptance/log_from_organizer_spec.rb
|
@@ -204,7 +219,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
204
219
|
version: '0'
|
205
220
|
requirements: []
|
206
221
|
rubyforge_project:
|
207
|
-
rubygems_version: 2.6
|
222
|
+
rubygems_version: 2.7.6
|
208
223
|
signing_key:
|
209
224
|
specification_version: 4
|
210
225
|
summary: A service skeleton with an emphasis on simplicity
|
@@ -213,6 +228,7 @@ test_files:
|
|
213
228
|
- spec/acceptance/after_actions_spec.rb
|
214
229
|
- spec/acceptance/around_each_spec.rb
|
215
230
|
- spec/acceptance/before_actions_spec.rb
|
231
|
+
- spec/acceptance/custom_log_from_organizer_spec.rb
|
216
232
|
- spec/acceptance/fail_spec.rb
|
217
233
|
- spec/acceptance/include_warning_spec.rb
|
218
234
|
- spec/acceptance/log_from_organizer_spec.rb
|