light-service 0.10.2 → 0.10.3

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
  SHA1:
3
- metadata.gz: 839bc255742513ba7c0cabc80bd8f72e35d4a442
4
- data.tar.gz: 5957a2d95ac83d37cb9ba40629446ba4952ab835
3
+ metadata.gz: 35d148e9bdcc089cd30046a2a99eff468ec77fde
4
+ data.tar.gz: 95b6d152ab5eca62471cfb245be0f60c2d4bfc73
5
5
  SHA512:
6
- metadata.gz: 2c992973e9289bbf41dd73f77f9ee2e12d1e6bc2f55ad0d6af293e026a785931ee52dabe6ce2b5d85cf00afb86ffdf1218087a3cb1bdb6b525fa102ddd800a8c
7
- data.tar.gz: 7663059f0fad0ee5235306928110b6f7c5d77a057e0c34055fa9ac5eaae5756308855532023348c1a968120d8ed6b8a97649e8db5a7a3e973f3b69cc98dc1f7a
6
+ metadata.gz: 20fe035e150978da67f5cc624c9e142fc5299c7b20cc1f71d89c90a665577daa85ec73e32f695950f7d5d52ce0a4604159b08d367b195d27191819c18f497b38
7
+ data.tar.gz: 829ccc813a76c16da7ceb7aaa02262d30c555b5486224212b016588a45c2f172e050e6a2c6adc0c2ebef2c28ce5e9f3968a6c791fbc30c2cf4bee2f2a336824d
data/README.md CHANGED
@@ -257,12 +257,12 @@ Check out this example:
257
257
 
258
258
  ```ruby
259
259
  class LogDuration
260
- def self.call(action, context)
260
+ def self.call(context)
261
261
  start_time = Time.now
262
262
  result = yield
263
263
  duration = Time.now - start_time
264
264
  LightService::Configuration.logger.info(
265
- :action => action,
265
+ :action => context.current_action,
266
266
  :duration => duration
267
267
  )
268
268
 
@@ -1,5 +1,8 @@
1
1
  A brief list of new features and changes introduced with the specified version.
2
2
 
3
+ ### 0.10.2
4
+ * [Adding ContextFactory](https://github.com/adomokos/light-service/pull/147).
5
+
3
6
  ### 0.10.2
4
7
  * [Revert 0.10.1](https://github.com/adomokos/light-service/pull/146), it breaks tests in our apps :-(.
5
8
 
@@ -63,6 +63,8 @@ module LightService
63
63
  @aliases = key_hash
64
64
  end
65
65
 
66
+ # This looks like an accessor,
67
+ # but it's used as a macro in the Organizer
66
68
  def before_actions(*logic)
67
69
  self.before_actions = logic
68
70
  end
@@ -71,6 +73,13 @@ module LightService
71
73
  @before_actions = [logic].flatten
72
74
  end
73
75
 
76
+ def append_before_actions(action)
77
+ @before_actions ||= []
78
+ @before_actions.push(action)
79
+ end
80
+
81
+ # This looks like an accessor,
82
+ # but it's used as a macro in the Organizer
74
83
  def after_actions(*logic)
75
84
  self.after_actions = logic
76
85
  end
@@ -78,6 +87,11 @@ module LightService
78
87
  def after_actions=(logic)
79
88
  @after_actions = [logic].flatten
80
89
  end
90
+
91
+ def append_after_actions(action)
92
+ @after_actions ||= []
93
+ @after_actions.push(action)
94
+ end
81
95
  end
82
96
  end
83
97
  end
@@ -1,17 +1,6 @@
1
1
  module LightService
2
2
  module Testing
3
3
  class ContextFactory
4
- class ContextFactoryOrganizer
5
- extend LightService::Organizer
6
- class << self
7
- attr_accessor :actions
8
- end
9
-
10
- def self.call(ctx)
11
- with(ctx).reduce(actions)
12
- end
13
- end
14
-
15
4
  attr_reader :organizer
16
5
 
17
6
  def self.make_from(organizer)
@@ -19,25 +8,33 @@ module LightService
19
8
  end
20
9
 
21
10
  def for(action)
22
- ContextFactoryOrganizer.actions = find_up_to(action)
11
+ @organizer.append_before_actions(
12
+ lambda do |ctx|
13
+ if ctx.current_action == action
14
+ # The last `:_before_actions` hook is for
15
+ # ContextFactory, remove it, so it won't
16
+ # be invoked again
17
+ ctx[:_before_actions].pop
18
+
19
+ throw(:return_ctx_from_execution, ctx)
20
+ end
21
+ end
22
+ )
23
+
23
24
  self
24
25
  end
25
26
 
26
- def with(ctx)
27
- ContextFactoryOrganizer.call(ctx)
27
+ # More than one arguments can be passed to the
28
+ # Organizer's #call method
29
+ def with(*args, &block)
30
+ catch(:return_ctx_from_execution) do
31
+ @organizer.call(*args, &block)
32
+ end
28
33
  end
29
34
 
30
35
  def initialize(organizer)
31
36
  @organizer = organizer
32
37
  end
33
-
34
- def find_up_to(action)
35
- original_actions = organizer.actions
36
-
37
- original_actions.take_while do |current_action|
38
- current_action != action
39
- end
40
- end
41
38
  end
42
39
  end
43
40
  end
@@ -1,3 +1,3 @@
1
1
  module LightService
2
- VERSION = "0.10.2".freeze
2
+ VERSION = "0.10.3".freeze
3
3
  end
@@ -64,4 +64,17 @@ RSpec.describe 'Action after_actions' do
64
64
  expect(result.fetch(:number)).to eq(1)
65
65
  end
66
66
  end
67
+
68
+ describe 'after_actions can be appended' do
69
+ it 'adds to the :_after_actions collection' do
70
+ TestDoubles::AdditionOrganizer.append_after_actions(
71
+ lambda do |ctx|
72
+ ctx.number -= 3 if ctx.current_action == TestDoubles::AddsThreeAction
73
+ end
74
+ )
75
+
76
+ result = TestDoubles::AdditionOrganizer.call(0)
77
+ expect(result.fetch(:number)).to eq(3)
78
+ end
79
+ end
67
80
  end
@@ -8,12 +8,12 @@ class AdditionOrganizerContextFactory
8
8
  LightService::Testing::ContextFactory
9
9
  .make_from(TestDoubles::AdditionOrganizer)
10
10
  .for(action)
11
- .with(:number => number)
11
+ .with(number)
12
12
  end
13
13
  end
14
14
 
15
15
  RSpec.describe TestDoubles::AddsThreeAction do
16
- it "creates a context for the action with ContextFactory wrapper" do
16
+ it 'creates a context for the action with ContextFactory wrapper' do
17
17
  context =
18
18
  AdditionOrganizerContextFactory
19
19
  .make_for(TestDoubles::AddsThreeAction, 1)
@@ -21,13 +21,34 @@ RSpec.describe TestDoubles::AddsThreeAction do
21
21
  expect(context.number).to eq(7)
22
22
  end
23
23
 
24
- it "creates a context for the action using the ContextFactory" do
24
+ it 'creates a context for the action using the ContextFactory' do
25
25
  context =
26
26
  LightService::Testing::ContextFactory
27
27
  .make_from(TestDoubles::AdditionOrganizer)
28
28
  .for(TestDoubles::AddsThreeAction)
29
- .with(:number => 4) # Context is a "glorified" hash
29
+ .with(4) # Context is a "glorified" hash
30
30
 
31
31
  expect(context.number).to eq(7)
32
32
  end
33
+
34
+ it "works with multiple arguments passed to Organizer's call method" do
35
+ context = LightService::Testing::ContextFactory
36
+ .make_from(TestDoubles::ExtraArgumentAdditionOrganizer)
37
+ .for(described_class)
38
+ .with(4, 2)
39
+
40
+ expect(context.number).to eq(9)
41
+ end
42
+ end
43
+
44
+ RSpec.describe TestDoubles::AddsTwoAction do
45
+ it 'does not execute a callback entirely from a ContextFactory' do
46
+ context = LightService::Testing::ContextFactory
47
+ .make_from(TestDoubles::CallbackOrganizer)
48
+ .for(described_class)
49
+ .with(:number => 0)
50
+
51
+ # add 1, add 10, then stop before executing first add 2
52
+ expect(context.number).to eq(11)
53
+ end
33
54
  end
@@ -223,6 +223,22 @@ module TestDoubles
223
223
  end
224
224
  end
225
225
 
226
+ class ExtraArgumentAdditionOrganizer
227
+ extend LightService::Organizer
228
+
229
+ def self.call(number, another_number)
230
+ with(:number => number + another_number).reduce(actions)
231
+ end
232
+
233
+ def self.actions
234
+ [
235
+ AddsOneAction,
236
+ AddsTwoAction,
237
+ AddsThreeAction
238
+ ]
239
+ end
240
+ end
241
+
226
242
  class AddsOneAction
227
243
  extend LightService::Action
228
244
  expects :number
@@ -251,6 +267,99 @@ module TestDoubles
251
267
  end
252
268
  end
253
269
 
270
+ class IterateOrganizer
271
+ extend LightService::Organizer
272
+
273
+ def self.call(ctx)
274
+ with(ctx).reduce(actions)
275
+ end
276
+
277
+ def self.actions
278
+ [
279
+ AddsOneIteratesAction,
280
+ iterate(:numbers, [
281
+ AddsTwoAction,
282
+ AddsThreeAction
283
+ ])
284
+ ]
285
+ end
286
+ end
287
+
288
+ class AddsOneIteratesAction
289
+ extend LightService::Action
290
+ expects :numbers
291
+ promises :numbers
292
+
293
+ executed do |context|
294
+ context.numbers = context.numbers.map { |n| n + 1 }
295
+ end
296
+ end
297
+
298
+ class CallbackOrganizer
299
+ extend LightService::Organizer
300
+
301
+ def self.call(ctx)
302
+ with(ctx).reduce(actions)
303
+ end
304
+
305
+ def self.actions
306
+ [
307
+ AddsOneAction,
308
+ with_callback(AddTenCallbackAction, [
309
+ AddsTwoAction,
310
+ AddsThreeAction
311
+ ])
312
+ ]
313
+ end
314
+ end
315
+
316
+ class AddTenCallbackAction
317
+ extend LightService::Action
318
+ expects :number, :callback
319
+
320
+ executed do |context|
321
+ context.number += 10
322
+ context.number =
323
+ context.callback.call(context).fetch(:number)
324
+ end
325
+ end
326
+
327
+ class ReduceUntilOrganizer
328
+ extend LightService::Organizer
329
+
330
+ def self.call(ctx)
331
+ with(ctx).reduce(actions)
332
+ end
333
+
334
+ def self.actions
335
+ [
336
+ AddsOneAction,
337
+ reduce_until(->(ctx) { ctx.number > 3 }, [
338
+ AddsTwoAction,
339
+ AddsThreeAction
340
+ ])
341
+ ]
342
+ end
343
+ end
344
+
345
+ class ReduceIfOrganizer
346
+ extend LightService::Organizer
347
+
348
+ def self.call(ctx)
349
+ with(ctx).reduce(actions)
350
+ end
351
+
352
+ def self.actions
353
+ [
354
+ AddsOneAction,
355
+ reduce_if(->(ctx) { ctx.number > 1 }, [
356
+ AddsTwoAction,
357
+ AddsThreeAction
358
+ ])
359
+ ]
360
+ end
361
+ end
362
+
254
363
  class MakesTeaExpectingReservedKey
255
364
  extend LightService::Action
256
365
  expects :tea, :message
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+ require 'test_doubles'
3
+
4
+ RSpec.describe 'ContextFactory - used with IterateOrganizer' do
5
+ let(:organizer) { TestDoubles::IterateOrganizer }
6
+
7
+ context 'when called with outside iterate steps' do
8
+ it 'creates a context up-to the action defined before the iteration' do
9
+ ctx =
10
+ LightService::Testing::ContextFactory
11
+ .make_from(organizer)
12
+ .for(TestDoubles::AddsOneIteratesAction)
13
+ .with(:numbers => [1, 2])
14
+
15
+ expect(ctx[:numbers]).to eq([1, 2])
16
+ end
17
+
18
+ it 'creates a context up-to iteration with empty context steps' do
19
+ ctx =
20
+ LightService::Testing::ContextFactory
21
+ .make_from(organizer)
22
+ .for(TestDoubles::AddsTwoAction)
23
+ .with(:numbers => [1, 2])
24
+
25
+ expect(ctx.numbers).to eq([2, 3])
26
+ end
27
+
28
+ it 'creates a context only to the first step of the iteration' do
29
+ ctx =
30
+ LightService::Testing::ContextFactory
31
+ .make_from(organizer)
32
+ .for(TestDoubles::AddsThreeAction)
33
+ .with(:numbers => [1, 2])
34
+
35
+ expect(ctx.numbers).to eq([2, 3])
36
+ expect(ctx.number).to eq(4)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+ require 'test_doubles'
3
+
4
+ RSpec.describe 'ContextFactory - used with ReduceIfOrganizer' do
5
+ let(:organizer) { TestDoubles::ReduceIfOrganizer }
6
+
7
+ context 'when called with a truthy argument action' do
8
+ it 'executes a context up-to the callback action' do
9
+ ctx =
10
+ LightService::Testing::ContextFactory
11
+ .make_from(organizer)
12
+ .for(TestDoubles::AddsThreeAction)
13
+ .with(:number => 1)
14
+
15
+ expect(ctx.number).to eq(4)
16
+ end
17
+
18
+ it 'creates a context up-to action with empty context steps' do
19
+ ctx =
20
+ LightService::Testing::ContextFactory
21
+ .make_from(organizer)
22
+ .for(TestDoubles::AddsTwoAction)
23
+ .with(:number => 1)
24
+
25
+ expect(ctx.number).to eq(2)
26
+ end
27
+ end
28
+
29
+ context 'when called with a false argument action' do
30
+ it 'does not execute the steps' do
31
+ ctx =
32
+ LightService::Testing::ContextFactory
33
+ .make_from(organizer)
34
+ .for(TestDoubles::AddsThreeAction)
35
+ .with(:number => 0)
36
+
37
+ expect(ctx.number).to eq(1)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+ require 'test_doubles'
3
+
4
+ RSpec.describe 'ContextFactory - used with ReduceUntilOrganizer' do
5
+ let(:organizer) { TestDoubles::ReduceUntilOrganizer }
6
+
7
+ context 'when called with truthy block' do
8
+ it 'creates a context up-to the action defined before the iteration' do
9
+ ctx =
10
+ LightService::Testing::ContextFactory
11
+ .make_from(organizer)
12
+ .for(TestDoubles::AddsTwoAction)
13
+ .with(:number => 1)
14
+
15
+ expect(ctx[:number]).to eq(2)
16
+ end
17
+
18
+ it 'creates a context only to the first step of the loop' do
19
+ ctx =
20
+ LightService::Testing::ContextFactory
21
+ .make_from(organizer)
22
+ .for(TestDoubles::AddsThreeAction)
23
+ .with(:number => 1)
24
+
25
+ expect(ctx.number).to eq(4)
26
+ end
27
+ end
28
+
29
+ context 'when called with falsey block' do
30
+ it 'creates a context up-to the first step' do
31
+ ctx =
32
+ LightService::Testing::ContextFactory
33
+ .make_from(organizer)
34
+ .for(TestDoubles::AddsThreeAction)
35
+ .with(:number => 7)
36
+
37
+ expect(ctx[:number]).to eq(10)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require 'test_doubles'
3
+
4
+ RSpec.describe 'ContextFactory - used with CallbackOrganizer' do
5
+ let(:organizer) { TestDoubles::CallbackOrganizer }
6
+
7
+ context 'when called with the callback action' do
8
+ it 'creates a context up-to the callback action' do
9
+ ctx =
10
+ LightService::Testing::ContextFactory
11
+ .make_from(organizer)
12
+ .for(TestDoubles::AddTenCallbackAction)
13
+ .with(:number => 1)
14
+
15
+ expect(ctx.number).to eq(2)
16
+ end
17
+
18
+ it 'creates a context up-to callback action with empty context steps' do
19
+ ctx =
20
+ LightService::Testing::ContextFactory
21
+ .make_from(organizer)
22
+ .for(TestDoubles::AddsTwoAction)
23
+ .with(:number => 1)
24
+
25
+ expect(ctx.number).to eq(12)
26
+ end
27
+
28
+ it 'creates a context up-to the action defined in context steps' do
29
+ ctx =
30
+ LightService::Testing::ContextFactory
31
+ .make_from(organizer)
32
+ .for(TestDoubles::AddsThreeAction)
33
+ .with(:number => 1)
34
+
35
+ expect(ctx.number).to eq(14)
36
+ end
37
+ end
38
+ end
@@ -2,13 +2,15 @@ require 'spec_helper'
2
2
  require 'test_doubles'
3
3
 
4
4
  describe 'ContextFactory - used with AdditionOrganizer' do
5
+ let(:organizer) { TestDoubles::AdditionOrganizer }
6
+
5
7
  context 'when called with the first action' do
6
8
  it 'does not alter the context' do
7
9
  ctx =
8
10
  LightService::Testing::ContextFactory
9
- .make_from(TestDoubles::AdditionOrganizer)
11
+ .make_from(organizer)
10
12
  .for(TestDoubles::AddsOneAction)
11
- .with(:number => 1)
13
+ .with(1)
12
14
 
13
15
  expect(ctx[:number]).to eq(1)
14
16
  end
@@ -18,9 +20,9 @@ describe 'ContextFactory - used with AdditionOrganizer' do
18
20
  it 'adds one to the number provided' do
19
21
  ctx =
20
22
  LightService::Testing::ContextFactory
21
- .make_from(TestDoubles::AdditionOrganizer)
23
+ .make_from(organizer)
22
24
  .for(TestDoubles::AddsTwoAction)
23
- .with(:number => 1)
25
+ .with(1)
24
26
 
25
27
  expect(ctx.number).to eq(2)
26
28
  end
@@ -30,11 +32,31 @@ describe 'ContextFactory - used with AdditionOrganizer' do
30
32
  it 'creates a context up-to the action defined' do
31
33
  ctx =
32
34
  LightService::Testing::ContextFactory
33
- .make_from(TestDoubles::AdditionOrganizer)
35
+ .make_from(organizer)
34
36
  .for(TestDoubles::AddsThreeAction)
35
- .with(:number => 1)
37
+ .with(1)
36
38
 
37
39
  expect(ctx.number).to eq(4)
38
40
  end
39
41
  end
42
+
43
+ context 'when there are already before_actions' do
44
+ it 'only appends before_actions' do
45
+ TestDoubles::AdditionOrganizer.before_actions = [
46
+ lambda do |ctx|
47
+ ctx[:number] += 1 \
48
+ if ctx.current_action == TestDoubles::AddsTwoAction
49
+ end
50
+ ]
51
+
52
+ context =
53
+ LightService::Testing::ContextFactory
54
+ .make_from(TestDoubles::AdditionOrganizer)
55
+ .for(TestDoubles::AddsThreeAction)
56
+ .with(4) # Context is a "glorified" hash
57
+
58
+ expect(context.number).to eq(8)
59
+ expect(context[:_before_actions].length).to eq(1)
60
+ end
61
+ end
40
62
  end
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.10.2
4
+ version: 0.10.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Attila Domokos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-30 00:00:00.000000000 Z
11
+ date: 2018-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -179,6 +179,10 @@ files:
179
179
  - spec/spec_helper.rb
180
180
  - spec/support.rb
181
181
  - spec/test_doubles.rb
182
+ - spec/testing/context_factory/iterate_spec.rb
183
+ - spec/testing/context_factory/reduce_if_spec.rb
184
+ - spec/testing/context_factory/reduce_until_spec.rb
185
+ - spec/testing/context_factory/with_callback_spec.rb
182
186
  - spec/testing/context_factory_spec.rb
183
187
  homepage: https://github.com/adomokos/light-service
184
188
  licenses:
@@ -252,4 +256,8 @@ test_files:
252
256
  - spec/spec_helper.rb
253
257
  - spec/support.rb
254
258
  - spec/test_doubles.rb
259
+ - spec/testing/context_factory/iterate_spec.rb
260
+ - spec/testing/context_factory/reduce_if_spec.rb
261
+ - spec/testing/context_factory/reduce_until_spec.rb
262
+ - spec/testing/context_factory/with_callback_spec.rb
255
263
  - spec/testing/context_factory_spec.rb