functional-light-service 6.1.0 → 6.2.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/.gitignore +2 -0
- data/CHANGELOG.md +19 -0
- data/README.md +96 -11
- data/functional-light-service.gemspec +3 -1
- data/lib/functional-light-service/action.rb +48 -0
- data/lib/functional-light-service/configuration.rb +12 -2
- data/lib/functional-light-service/context/key_verifier.rb +1 -1
- data/lib/functional-light-service/context.rb +15 -2
- data/lib/functional-light-service/errors.rb +1 -0
- data/lib/functional-light-service/i18n/localization_adapter.rb +50 -0
- data/lib/functional-light-service/localization_adapter.rb +19 -28
- data/lib/functional-light-service/localization_map.rb +9 -0
- data/lib/functional-light-service/organizer/reduce_case.rb +50 -0
- data/lib/functional-light-service/organizer/reduce_if_else.rb +23 -0
- data/lib/functional-light-service/organizer/reduce_until.rb +1 -1
- data/lib/functional-light-service/organizer/reduce_while.rb +31 -0
- data/lib/functional-light-service/organizer/scoped_reducable.rb +2 -2
- data/lib/functional-light-service/organizer/with_reducer_log_decorator.rb +2 -1
- data/lib/functional-light-service/organizer.rb +18 -3
- data/lib/functional-light-service/version.rb +1 -1
- data/lib/functional-light-service.rb +5 -0
- data/spec/acceptance/organizer/add_to_context_spec.rb +24 -0
- data/spec/acceptance/organizer/context_failure_and_skipping_spec.rb +22 -0
- data/spec/acceptance/organizer/execute_spec.rb +21 -0
- data/spec/acceptance/organizer/reduce_case_spec.rb +65 -0
- data/spec/acceptance/organizer/reduce_if_else_spec.rb +60 -0
- data/spec/acceptance/organizer/reduce_while_spec.rb +96 -0
- data/spec/acceptance/skip_all_remaining_spec.rb +139 -0
- data/spec/action_optional_expected_keys_spec.rb +107 -0
- data/spec/context/inspect_spec.rb +13 -3
- data/spec/context_spec.rb +12 -0
- data/spec/i18n_localization_adapter_spec.rb +83 -0
- data/spec/localization_adapter_spec.rb +66 -83
- data/spec/spec_helper.rb +3 -0
- data/spec/test_doubles.rb +28 -0
- metadata +26 -15
|
@@ -36,16 +36,28 @@ module FunctionalLightService
|
|
|
36
36
|
ReduceIf.run(self, condition_block, steps)
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
+
def reduce_if_else(condition_block, if_steps, else_steps)
|
|
40
|
+
ReduceIfElse.run(self, condition_block, if_steps, else_steps)
|
|
41
|
+
end
|
|
42
|
+
|
|
39
43
|
def reduce_until(condition_block, steps)
|
|
40
44
|
ReduceUntil.run(self, condition_block, steps)
|
|
41
45
|
end
|
|
42
46
|
|
|
47
|
+
def reduce_case(**args)
|
|
48
|
+
ReduceCase.run(self, **args)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def reduce_while(condition_block, steps)
|
|
52
|
+
ReduceWhile.run(self, condition_block, steps)
|
|
53
|
+
end
|
|
54
|
+
|
|
43
55
|
def iterate(collection_key, steps)
|
|
44
56
|
Iterate.run(self, collection_key, steps)
|
|
45
57
|
end
|
|
46
58
|
|
|
47
|
-
def execute(code_block)
|
|
48
|
-
Execute.run(code_block)
|
|
59
|
+
def execute(code_block = nil, &block)
|
|
60
|
+
Execute.run(code_block || block)
|
|
49
61
|
end
|
|
50
62
|
|
|
51
63
|
def with_callback(action, steps)
|
|
@@ -62,7 +74,10 @@ module FunctionalLightService
|
|
|
62
74
|
|
|
63
75
|
def add_to_context(**args)
|
|
64
76
|
args.map do |key, value|
|
|
65
|
-
execute(->(ctx)
|
|
77
|
+
execute(->(ctx) do
|
|
78
|
+
ctx[key.to_sym] = value
|
|
79
|
+
ctx.define_accessor_methods_for_keys([key])
|
|
80
|
+
end)
|
|
66
81
|
end
|
|
67
82
|
end
|
|
68
83
|
|
|
@@ -15,7 +15,9 @@ require 'functional-light-service/functional/null'
|
|
|
15
15
|
require 'functional-light-service/functional/sequencer'
|
|
16
16
|
require 'functional-light-service/errors'
|
|
17
17
|
require 'functional-light-service/configuration'
|
|
18
|
+
require 'functional-light-service/i18n/localization_adapter'
|
|
18
19
|
require 'functional-light-service/localization_adapter'
|
|
20
|
+
require 'functional-light-service/localization_map'
|
|
19
21
|
require 'functional-light-service/context'
|
|
20
22
|
require 'functional-light-service/context/key_verifier'
|
|
21
23
|
require 'functional-light-service/organizer/scoped_reducable'
|
|
@@ -23,7 +25,10 @@ require 'functional-light-service/organizer/with_reducer'
|
|
|
23
25
|
require 'functional-light-service/organizer/with_reducer_log_decorator'
|
|
24
26
|
require 'functional-light-service/organizer/with_reducer_factory'
|
|
25
27
|
require 'functional-light-service/organizer/reduce_if'
|
|
28
|
+
require 'functional-light-service/organizer/reduce_if_else'
|
|
29
|
+
require 'functional-light-service/organizer/reduce_case'
|
|
26
30
|
require 'functional-light-service/organizer/reduce_until'
|
|
31
|
+
require 'functional-light-service/organizer/reduce_while'
|
|
27
32
|
require 'functional-light-service/organizer/iterate'
|
|
28
33
|
require 'functional-light-service/organizer/execute'
|
|
29
34
|
require 'functional-light-service/organizer/with_callback'
|
|
@@ -27,4 +27,28 @@ RSpec.describe FunctionalLightService::Organizer do
|
|
|
27
27
|
expect(result.number).to eq(1)
|
|
28
28
|
expect(result[:something]).to eq("hello")
|
|
29
29
|
end
|
|
30
|
+
|
|
31
|
+
it "defines accessors for the added keys" do
|
|
32
|
+
result = TestAddToContext.call
|
|
33
|
+
|
|
34
|
+
expect(result.something).to eq("hello")
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "raises when the added key conflicts with a Context method" do
|
|
38
|
+
organizer = Class.new do
|
|
39
|
+
extend FunctionalLightService::Organizer
|
|
40
|
+
|
|
41
|
+
def self.call
|
|
42
|
+
reduce(steps)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.steps
|
|
46
|
+
[add_to_context(:message => "boom")]
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
expect { organizer.call }
|
|
51
|
+
.to raise_error(FunctionalLightService::ReservedKeysInContextError,
|
|
52
|
+
/:message conflicts/)
|
|
53
|
+
end
|
|
30
54
|
end
|
|
@@ -31,6 +31,21 @@ RSpec.describe FunctionalLightService::Organizer do
|
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
+
class TestSkipAllFromNestedScope
|
|
35
|
+
extend FunctionalLightService::Organizer
|
|
36
|
+
|
|
37
|
+
def self.call
|
|
38
|
+
with(:number => 1)
|
|
39
|
+
.reduce([
|
|
40
|
+
reduce_if(->(_ctx) { true }, [
|
|
41
|
+
TestDoubles::AddsOneAction,
|
|
42
|
+
execute(lambda(&:skip_all_remaining!))
|
|
43
|
+
]),
|
|
44
|
+
TestDoubles::AddsOneAction
|
|
45
|
+
])
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
34
49
|
class TestContextFailure
|
|
35
50
|
extend FunctionalLightService::Organizer
|
|
36
51
|
|
|
@@ -59,6 +74,13 @@ RSpec.describe FunctionalLightService::Organizer do
|
|
|
59
74
|
expect(result[:number]).to eq(3)
|
|
60
75
|
end
|
|
61
76
|
|
|
77
|
+
it 'does not reset skip_all_remaining at the end of a nested scope' do
|
|
78
|
+
result = TestSkipAllFromNestedScope.call
|
|
79
|
+
|
|
80
|
+
expect(result).to be_success
|
|
81
|
+
expect(result[:number]).to eq(2)
|
|
82
|
+
end
|
|
83
|
+
|
|
62
84
|
it 'respects failure across all nestings' do
|
|
63
85
|
result = TestContextFailure.call
|
|
64
86
|
|
|
@@ -43,4 +43,25 @@ RSpec.describe FunctionalLightService::Organizer do
|
|
|
43
43
|
result = TestExecute.call(empty_context)
|
|
44
44
|
expect(result).to be_success
|
|
45
45
|
end
|
|
46
|
+
|
|
47
|
+
it 'accepts a block instead of a lambda' do
|
|
48
|
+
organizer = Class.new do
|
|
49
|
+
extend FunctionalLightService::Organizer
|
|
50
|
+
|
|
51
|
+
def self.call(context)
|
|
52
|
+
with(context).reduce(steps)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def self.steps
|
|
56
|
+
[
|
|
57
|
+
execute { |ctx| ctx[:number] += 1 }
|
|
58
|
+
]
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
result = organizer.call(:number => 1)
|
|
63
|
+
|
|
64
|
+
expect(result).to be_success
|
|
65
|
+
expect(result[:number]).to eq(2)
|
|
66
|
+
end
|
|
46
67
|
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'test_doubles'
|
|
3
|
+
|
|
4
|
+
RSpec.describe FunctionalLightService::Organizer do
|
|
5
|
+
class TestReduceCase
|
|
6
|
+
extend FunctionalLightService::Organizer
|
|
7
|
+
|
|
8
|
+
def self.call(context)
|
|
9
|
+
with(context).reduce(actions)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.actions
|
|
13
|
+
[
|
|
14
|
+
reduce_case(
|
|
15
|
+
:value => :incr_num,
|
|
16
|
+
:when => {
|
|
17
|
+
:one => [TestDoubles::AddsOneAction],
|
|
18
|
+
:two => [TestDoubles::AddsTwoAction],
|
|
19
|
+
:three => [TestDoubles::AddsThreeAction]
|
|
20
|
+
},
|
|
21
|
+
:else => [TestDoubles::FailureAction]
|
|
22
|
+
)
|
|
23
|
+
]
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it 'adds one if the incr_num is one' do
|
|
28
|
+
result = TestReduceCase.call(:number => 0, :incr_num => :one)
|
|
29
|
+
|
|
30
|
+
expect(result).to be_success
|
|
31
|
+
expect(result[:number]).to eq(1)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it 'adds two if the incr_num is two' do
|
|
35
|
+
result = TestReduceCase.call(:number => 0, :incr_num => :two)
|
|
36
|
+
|
|
37
|
+
expect(result).to be_success
|
|
38
|
+
expect(result[:number]).to eq(2)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it 'adds three if the incr_num is three' do
|
|
42
|
+
result = TestReduceCase.call(:number => 0, :incr_num => :three)
|
|
43
|
+
|
|
44
|
+
expect(result).to be_success
|
|
45
|
+
expect(result[:number]).to eq(3)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it 'will fail if the incr_num is neither one, two, or three' do
|
|
49
|
+
result = TestReduceCase.call(:number => 0, :incr_num => :four)
|
|
50
|
+
|
|
51
|
+
expect(result).to be_failure
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it 'raises ArgumentError when a mandatory keyword argument is missing' do
|
|
55
|
+
expect do
|
|
56
|
+
Class.new do
|
|
57
|
+
extend FunctionalLightService::Organizer
|
|
58
|
+
|
|
59
|
+
def self.actions
|
|
60
|
+
[reduce_case(:value => :incr_num, :when => {})]
|
|
61
|
+
end
|
|
62
|
+
end.actions
|
|
63
|
+
end.to raise_error(ArgumentError, /Expected keyword arguments/)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'test_doubles'
|
|
3
|
+
|
|
4
|
+
RSpec.describe FunctionalLightService::Organizer do
|
|
5
|
+
class TestReduceIfElse
|
|
6
|
+
extend FunctionalLightService::Organizer
|
|
7
|
+
|
|
8
|
+
def self.call(context)
|
|
9
|
+
with(context).reduce(actions)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.actions
|
|
13
|
+
[
|
|
14
|
+
TestDoubles::AddsOneAction,
|
|
15
|
+
reduce_if_else(
|
|
16
|
+
->(ctx) { ctx.number == 1 },
|
|
17
|
+
[TestDoubles::AddsOneAction],
|
|
18
|
+
[TestDoubles::AddsTwoAction]
|
|
19
|
+
)
|
|
20
|
+
]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
let(:empty_context) { FunctionalLightService::Context.make }
|
|
25
|
+
|
|
26
|
+
it 'reduces the if_steps if the condition is true' do
|
|
27
|
+
result = TestReduceIfElse.call(:number => 0)
|
|
28
|
+
|
|
29
|
+
expect(result).to be_success
|
|
30
|
+
expect(result[:number]).to eq(2)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it 'reduces the else_steps if the condition is false' do
|
|
34
|
+
result = TestReduceIfElse.call(:number => 2)
|
|
35
|
+
|
|
36
|
+
expect(result).to be_success
|
|
37
|
+
expect(result[:number]).to eq(5)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it 'will not reduce over a failed context' do
|
|
41
|
+
empty_context.fail!('Something bad happened')
|
|
42
|
+
|
|
43
|
+
result = TestReduceIfElse.call(empty_context)
|
|
44
|
+
|
|
45
|
+
expect(result).to be_failure
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it 'does not reduce over a skipped context' do
|
|
49
|
+
empty_context.skip_remaining!('No more needed')
|
|
50
|
+
|
|
51
|
+
result = TestReduceIfElse.call(empty_context)
|
|
52
|
+
expect(result).to be_success
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "knows that it's being conditionally reduced from within an organizer" do
|
|
56
|
+
result = TestReduceIfElse.call(:number => 2)
|
|
57
|
+
|
|
58
|
+
expect(result.organized_by).to eq TestReduceIfElse
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'test_doubles'
|
|
3
|
+
|
|
4
|
+
RSpec.describe FunctionalLightService::Organizer do
|
|
5
|
+
class TestReduceWhile
|
|
6
|
+
extend FunctionalLightService::Organizer
|
|
7
|
+
|
|
8
|
+
def self.call(context)
|
|
9
|
+
with(context).reduce(actions)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.actions
|
|
13
|
+
[
|
|
14
|
+
reduce_while(->(ctx) { ctx[:number] < 3 }, [
|
|
15
|
+
TestDoubles::AddsOneAction,
|
|
16
|
+
TestDoubles::AddsTwoAction
|
|
17
|
+
])
|
|
18
|
+
]
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
let(:empty_context) { FunctionalLightService::Context.make }
|
|
23
|
+
|
|
24
|
+
it 'reduces while the block evaluates to true' do
|
|
25
|
+
result = TestReduceWhile.call(:number => 0)
|
|
26
|
+
|
|
27
|
+
expect(result).to be_success
|
|
28
|
+
expect(result[:number]).to eq(3)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'checks the condition before each action' do
|
|
32
|
+
result = TestReduceWhile.call(:number => 2)
|
|
33
|
+
|
|
34
|
+
expect(result).to be_success
|
|
35
|
+
expect(result[:number]).to eq(3)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it 'does not execute any steps when the condition is false from the start' do
|
|
39
|
+
result = TestReduceWhile.call(:number => 5)
|
|
40
|
+
|
|
41
|
+
expect(result).to be_success
|
|
42
|
+
expect(result[:number]).to eq(5)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it 'does not execute on failed context' do
|
|
46
|
+
empty_context.fail!('Something bad happened')
|
|
47
|
+
|
|
48
|
+
result = TestReduceWhile.call(empty_context)
|
|
49
|
+
expect(result).to be_failure
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it 'does not execute a skipped context' do
|
|
53
|
+
empty_context.skip_remaining!('No more needed')
|
|
54
|
+
|
|
55
|
+
result = TestReduceWhile.call(empty_context)
|
|
56
|
+
expect(result).to be_success
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it "is expected to know its organizer when reducing while a condition" do
|
|
60
|
+
result = TestReduceWhile.call(:number => 0)
|
|
61
|
+
|
|
62
|
+
expect(result.organized_by).to eq TestReduceWhile
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it 'skips actions within its own scope' do
|
|
66
|
+
org = Class.new do
|
|
67
|
+
extend FunctionalLightService::Organizer
|
|
68
|
+
|
|
69
|
+
def self.call
|
|
70
|
+
reduce(actions)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def self.actions
|
|
74
|
+
[
|
|
75
|
+
reduce_while(
|
|
76
|
+
->(c) { !c.nil? },
|
|
77
|
+
[
|
|
78
|
+
execute(->(c) { c[:first_reduce_while] = true }),
|
|
79
|
+
execute(lambda(&:skip_remaining!)),
|
|
80
|
+
execute(->(c) { c[:second_reduce_while] = true })
|
|
81
|
+
]
|
|
82
|
+
),
|
|
83
|
+
execute(->(c) { c[:last_outside] = true })
|
|
84
|
+
]
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
result = org.call
|
|
89
|
+
|
|
90
|
+
aggregate_failures do
|
|
91
|
+
expect(result[:first_reduce_while]).to be true
|
|
92
|
+
expect(result[:second_reduce_while]).to be_nil
|
|
93
|
+
expect(result[:last_outside]).to be true
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe "skip_all_remaining!" do
|
|
4
|
+
context "with regular organizer" do
|
|
5
|
+
let(:organizer) do
|
|
6
|
+
Class.new do
|
|
7
|
+
extend FunctionalLightService::Organizer
|
|
8
|
+
|
|
9
|
+
def self.call(ctx)
|
|
10
|
+
with(ctx).reduce(actions)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.actions
|
|
14
|
+
[
|
|
15
|
+
execute(->(c) { c[:first] = true }),
|
|
16
|
+
execute(lambda(&:skip_all_remaining!)),
|
|
17
|
+
execute(->(c) { c[:second] = true })
|
|
18
|
+
]
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "skips all remaining actions" do
|
|
24
|
+
result = organizer.call(FunctionalLightService::Context.make)
|
|
25
|
+
|
|
26
|
+
expect(result[:first]).to be true
|
|
27
|
+
expect(result[:second]).to be_nil
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
context "with an organizer with a reducer" do
|
|
32
|
+
let(:organizer) do
|
|
33
|
+
Class.new do
|
|
34
|
+
extend FunctionalLightService::Organizer
|
|
35
|
+
|
|
36
|
+
def self.call(ctx)
|
|
37
|
+
with(ctx).reduce(actions)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.actions
|
|
41
|
+
[
|
|
42
|
+
reduce_if(
|
|
43
|
+
->(_) { true },
|
|
44
|
+
[
|
|
45
|
+
execute(->(c) { c[:first_inside] = true }),
|
|
46
|
+
execute(lambda(&:skip_all_remaining!)),
|
|
47
|
+
execute(->(c) { c[:second_inside] = true })
|
|
48
|
+
]
|
|
49
|
+
),
|
|
50
|
+
execute(->(c) { c[:outside] = true })
|
|
51
|
+
]
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it "skips all remaining actions inside and outside the reducer" do
|
|
57
|
+
result = organizer.call(FunctionalLightService::Context.make)
|
|
58
|
+
|
|
59
|
+
expect(result[:first_inside]).to be true
|
|
60
|
+
expect(result[:second_inside]).to be_nil
|
|
61
|
+
expect(result[:outside]).to be_nil
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
context "with a message" do
|
|
66
|
+
let(:organizer) do
|
|
67
|
+
Class.new do
|
|
68
|
+
extend FunctionalLightService::Organizer
|
|
69
|
+
|
|
70
|
+
def self.call(ctx)
|
|
71
|
+
with(ctx).reduce(actions)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def self.actions
|
|
75
|
+
[
|
|
76
|
+
reduce_if(
|
|
77
|
+
->(_) { true },
|
|
78
|
+
[
|
|
79
|
+
execute(->(c) { c.skip_all_remaining!("Skipping with message") })
|
|
80
|
+
]
|
|
81
|
+
),
|
|
82
|
+
execute(->(c) { c[:outside] = true })
|
|
83
|
+
]
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
it "preserves the message when exiting scoped reducers" do
|
|
89
|
+
result = organizer.call(FunctionalLightService::Context.make)
|
|
90
|
+
|
|
91
|
+
expect(result.message).to eq("Skipping with message")
|
|
92
|
+
expect(result[:outside]).to be_nil
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
context "with an organizer with nested reducers" do
|
|
97
|
+
let(:organizer) do
|
|
98
|
+
Class.new do
|
|
99
|
+
extend FunctionalLightService::Organizer
|
|
100
|
+
|
|
101
|
+
def self.call(ctx)
|
|
102
|
+
with(ctx).reduce(actions)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def self.actions # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
|
|
106
|
+
[
|
|
107
|
+
reduce_if(
|
|
108
|
+
->(_) { true },
|
|
109
|
+
[
|
|
110
|
+
iterate(:items, [
|
|
111
|
+
execute(->(c) {
|
|
112
|
+
c[:executed_items] ||= []
|
|
113
|
+
c[:executed_items] << c[:item]
|
|
114
|
+
}),
|
|
115
|
+
execute(->(c) { c.skip_all_remaining! if c[:item] == 2 }),
|
|
116
|
+
execute(->(c) {
|
|
117
|
+
c[:skipped_in_iterate] ||= []
|
|
118
|
+
c[:skipped_in_iterate] << c[:item]
|
|
119
|
+
})
|
|
120
|
+
]),
|
|
121
|
+
execute(->(c) { c[:after_iterate_inside_if] = true })
|
|
122
|
+
]
|
|
123
|
+
),
|
|
124
|
+
execute(->(c) { c[:outside] = true })
|
|
125
|
+
]
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
it "skips all remaining actions across all nested scopes" do
|
|
131
|
+
result = organizer.call(FunctionalLightService::Context.make(:items => [1, 2, 3]))
|
|
132
|
+
|
|
133
|
+
expect(result[:executed_items]).to eq([1, 2])
|
|
134
|
+
expect(result[:skipped_in_iterate]).to eq([1])
|
|
135
|
+
expect(result[:after_iterate_inside_if]).to be_nil
|
|
136
|
+
expect(result[:outside]).to be_nil
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'test_doubles'
|
|
3
|
+
|
|
4
|
+
describe ":expects macro using defaults" do
|
|
5
|
+
context "when all expected keys are supplied" do
|
|
6
|
+
it "is expected to ignore default values" do
|
|
7
|
+
outcome = TestDoubles::AddsNumbersWithOptionalDefaults.execute(
|
|
8
|
+
:first_number => 3,
|
|
9
|
+
:second_number => 5,
|
|
10
|
+
:third_number => 7
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
expect(outcome.total).to eq 15
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
context "when defaults are supplied" do
|
|
18
|
+
it "is expected to use static values" do
|
|
19
|
+
outcome = TestDoubles::AddsNumbersWithOptionalDefaults.execute(
|
|
20
|
+
:first_number => 3,
|
|
21
|
+
:second_number => 7
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
expect(outcome.total).to eq 20
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "is expected to use dynamic values" do
|
|
28
|
+
outcome = TestDoubles::AddsNumbersWithOptionalDefaults.execute(
|
|
29
|
+
:first_number => 3,
|
|
30
|
+
:third_number => 5
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
expect(outcome.total).to eq 18
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "is expected to process defaults in their defined order" do
|
|
37
|
+
outcome = TestDoubles::AddsNumbersWithOptionalDefaults.execute(
|
|
38
|
+
:third_number => 5,
|
|
39
|
+
:first_number => 3
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
expect(outcome.total).to eq 18
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "is expected to use all defaults if required" do
|
|
46
|
+
outcome = TestDoubles::AddsNumbersWithOptionalDefaults.execute(
|
|
47
|
+
:first_number => 3
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
expect(outcome.total).to eq 23
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
context "when used within an organizer" do
|
|
55
|
+
it "is expected to process required defaults" do
|
|
56
|
+
outcome = TestDoubles::OrganizerWithActionsUsingDefaults.call
|
|
57
|
+
|
|
58
|
+
expect(outcome.total).to eq 20
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
context "when the expected key is satisfied through an alias" do
|
|
63
|
+
it "is expected to not apply the default" do
|
|
64
|
+
action = Class.new do
|
|
65
|
+
extend FunctionalLightService::Action
|
|
66
|
+
|
|
67
|
+
expects :greeting, :default => "hello"
|
|
68
|
+
promises :greeted
|
|
69
|
+
|
|
70
|
+
executed do |ctx|
|
|
71
|
+
ctx.greeted = ctx.greeting
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
organizer = Class.new do
|
|
76
|
+
extend FunctionalLightService::Organizer
|
|
77
|
+
|
|
78
|
+
aliases :salutation => :greeting
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
outcome = organizer.with(:salutation => "ciao").reduce([action])
|
|
82
|
+
|
|
83
|
+
expect(outcome.greeted).to eq "ciao"
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
context "when defaults are misconfigured" do
|
|
88
|
+
it "is expected to raise an exception" do
|
|
89
|
+
expect do
|
|
90
|
+
# Needs to be specified in the block
|
|
91
|
+
# as error is raised at define time
|
|
92
|
+
class AddsNumbersWithIncorrectDefaults
|
|
93
|
+
extend FunctionalLightService::Action
|
|
94
|
+
|
|
95
|
+
expects :first, :default => 10 # This one is fine. Other two arent
|
|
96
|
+
expects :second, :defalut => ->(ctx) { ctx[:first] + 7 }
|
|
97
|
+
expects :third, :deafult => 10
|
|
98
|
+
promises :total
|
|
99
|
+
|
|
100
|
+
executed do |ctx|
|
|
101
|
+
ctx.total = ctx.first + ctx.second + ctx.third
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end.to raise_error(FunctionalLightService::UnusableExpectKeyDefaultError)
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
@@ -14,7 +14,7 @@ RSpec.describe FunctionalLightService::Context do
|
|
|
14
14
|
it 'inspects the hash with all the fields' do
|
|
15
15
|
inspected_context =
|
|
16
16
|
"FunctionalLightService::Context({}, success: true, message: '', error_code: nil, " \
|
|
17
|
-
"skip_remaining: false, aliases: {})"
|
|
17
|
+
"skip_remaining: false, skip_all_remaining: false, aliases: {})"
|
|
18
18
|
|
|
19
19
|
expect(context.inspect).to eq(inspected_context)
|
|
20
20
|
end
|
|
@@ -24,7 +24,7 @@ RSpec.describe FunctionalLightService::Context do
|
|
|
24
24
|
|
|
25
25
|
inspected_context =
|
|
26
26
|
"FunctionalLightService::Context({}, success: false, message: 'There was an error', " \
|
|
27
|
-
"error_code: nil, skip_remaining: false, aliases: {})"
|
|
27
|
+
"error_code: nil, skip_remaining: false, skip_all_remaining: false, aliases: {})"
|
|
28
28
|
|
|
29
29
|
expect(context.inspect).to eq(inspected_context)
|
|
30
30
|
end
|
|
@@ -34,7 +34,17 @@ RSpec.describe FunctionalLightService::Context do
|
|
|
34
34
|
|
|
35
35
|
inspected_context =
|
|
36
36
|
"FunctionalLightService::Context({}, success: true, message: 'No need to process', " \
|
|
37
|
-
"error_code: nil, skip_remaining: true, aliases: {})"
|
|
37
|
+
"error_code: nil, skip_remaining: true, skip_all_remaining: false, aliases: {})"
|
|
38
|
+
|
|
39
|
+
expect(context.inspect).to eq(inspected_context)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it 'prints skip_all_remaining' do
|
|
43
|
+
context.skip_all_remaining!('Nothing else to process')
|
|
44
|
+
|
|
45
|
+
inspected_context =
|
|
46
|
+
"FunctionalLightService::Context({}, success: true, message: 'Nothing else to process', " \
|
|
47
|
+
"error_code: nil, skip_remaining: false, skip_all_remaining: true, aliases: {})"
|
|
38
48
|
|
|
39
49
|
expect(context.inspect).to eq(inspected_context)
|
|
40
50
|
end
|
data/spec/context_spec.rb
CHANGED
|
@@ -198,6 +198,18 @@ RSpec.describe FunctionalLightService::Context do
|
|
|
198
198
|
expect { action.execute(:_before_actions => []) }
|
|
199
199
|
.to raise_error(FunctionalLightService::ReservedKeysInContextError)
|
|
200
200
|
end
|
|
201
|
+
|
|
202
|
+
it "rejects :organized_by in expects/promises" do
|
|
203
|
+
action = Class.new do
|
|
204
|
+
extend FunctionalLightService::Action
|
|
205
|
+
|
|
206
|
+
expects :organized_by
|
|
207
|
+
executed { |_ctx| } # rubocop:disable Lint/EmptyBlock
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
expect { action.execute(:organized_by => Object) }
|
|
211
|
+
.to raise_error(FunctionalLightService::ReservedKeysInContextError)
|
|
212
|
+
end
|
|
201
213
|
end
|
|
202
214
|
|
|
203
215
|
describe "#fail! does not mutate the caller's options hash" do
|