functional-light-service 6.2.0 → 6.3.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/functional-light-service.rb +6 -36
- metadata +14 -307
- data/.github/workflows/project-build.yml +0 -71
- data/.gitignore +0 -24
- data/.rspec +0 -3
- data/.rubocop.yml +0 -101
- data/.solargraph.yml +0 -11
- data/AUDIT-functional-light-service.md +0 -352
- data/Appraisals +0 -7
- data/CHANGELOG.md +0 -292
- data/CODE_OF_CONDUCT.md +0 -22
- data/Gemfile +0 -4
- data/LICENSE +0 -22
- data/README.md +0 -1663
- data/Rakefile +0 -12
- data/VERSION +0 -1
- data/audit/bench.rb +0 -99
- data/audit/verify_findings.rb +0 -172
- data/functional-light-service.gemspec +0 -35
- data/gemfiles/dry_inflector_0_2_1.gemfile +0 -5
- data/gemfiles/i18n_1_8_11.gemfile +0 -5
- data/lib/functional-light-service/action.rb +0 -145
- data/lib/functional-light-service/configuration.rb +0 -36
- data/lib/functional-light-service/context/key_verifier.rb +0 -124
- data/lib/functional-light-service/context.rb +0 -208
- data/lib/functional-light-service/deprecations.rb +0 -26
- data/lib/functional-light-service/errors.rb +0 -9
- data/lib/functional-light-service/functional/enum.rb +0 -286
- data/lib/functional-light-service/functional/maybe.rb +0 -21
- data/lib/functional-light-service/functional/monad.rb +0 -77
- data/lib/functional-light-service/functional/null.rb +0 -88
- data/lib/functional-light-service/functional/option.rb +0 -100
- data/lib/functional-light-service/functional/result.rb +0 -129
- data/lib/functional-light-service/functional/sequencer.rb +0 -144
- data/lib/functional-light-service/i18n/localization_adapter.rb +0 -50
- data/lib/functional-light-service/localization_adapter.rb +0 -39
- data/lib/functional-light-service/localization_map.rb +0 -9
- data/lib/functional-light-service/organizer/execute.rb +0 -16
- data/lib/functional-light-service/organizer/iterate.rb +0 -30
- data/lib/functional-light-service/organizer/reduce_case.rb +0 -50
- data/lib/functional-light-service/organizer/reduce_if.rb +0 -19
- data/lib/functional-light-service/organizer/reduce_if_else.rb +0 -23
- data/lib/functional-light-service/organizer/reduce_until.rb +0 -22
- data/lib/functional-light-service/organizer/reduce_while.rb +0 -31
- data/lib/functional-light-service/organizer/scoped_reducable.rb +0 -15
- data/lib/functional-light-service/organizer/with_callback.rb +0 -28
- data/lib/functional-light-service/organizer/with_reducer.rb +0 -81
- data/lib/functional-light-service/organizer/with_reducer_factory.rb +0 -20
- data/lib/functional-light-service/organizer/with_reducer_log_decorator.rb +0 -111
- data/lib/functional-light-service/organizer.rb +0 -129
- data/lib/functional-light-service/testing/context_factory.rb +0 -48
- data/lib/functional-light-service/testing.rb +0 -3
- data/lib/functional-light-service/version.rb +0 -5
- data/resources/fail_actions.png +0 -0
- data/resources/light-service.png +0 -0
- data/resources/organizer_and_actions.png +0 -0
- data/resources/skip_actions.png +0 -0
- data/spec/acceptance/add_numbers_spec.rb +0 -11
- data/spec/acceptance/after_actions_spec.rb +0 -87
- data/spec/acceptance/around_each_spec.rb +0 -19
- data/spec/acceptance/before_actions_spec.rb +0 -115
- data/spec/acceptance/custom_log_from_organizer_spec.rb +0 -61
- data/spec/acceptance/deprecation_warnings_spec.rb +0 -82
- data/spec/acceptance/fail_spec.rb +0 -52
- data/spec/acceptance/log_from_organizer_spec.rb +0 -154
- data/spec/acceptance/message_localization_spec.rb +0 -119
- data/spec/acceptance/organizer/add_aliases_spec.rb +0 -28
- data/spec/acceptance/organizer/add_to_context_spec.rb +0 -54
- data/spec/acceptance/organizer/around_each_with_reduce_if_spec.rb +0 -42
- data/spec/acceptance/organizer/context_failure_and_skipping_spec.rb +0 -90
- data/spec/acceptance/organizer/execute_spec.rb +0 -67
- data/spec/acceptance/organizer/iterate_spec.rb +0 -44
- data/spec/acceptance/organizer/reduce_case_spec.rb +0 -65
- data/spec/acceptance/organizer/reduce_if_else_spec.rb +0 -60
- data/spec/acceptance/organizer/reduce_if_spec.rb +0 -89
- data/spec/acceptance/organizer/reduce_until_spec.rb +0 -49
- data/spec/acceptance/organizer/reduce_while_spec.rb +0 -96
- data/spec/acceptance/organizer/with_callback_spec.rb +0 -113
- data/spec/acceptance/organizer_entry_point_spec.rb +0 -35
- data/spec/acceptance/rollback_spec.rb +0 -183
- data/spec/acceptance/skip_all_remaining_spec.rb +0 -139
- data/spec/acceptance/testing/context_factory_spec.rb +0 -54
- data/spec/action_expected_keys_spec.rb +0 -63
- data/spec/action_expects_and_promises_spec.rb +0 -97
- data/spec/action_optional_expected_keys_spec.rb +0 -107
- data/spec/action_promised_keys_spec.rb +0 -126
- data/spec/action_spec.rb +0 -97
- data/spec/context/inspect_spec.rb +0 -52
- data/spec/context_spec.rb +0 -301
- data/spec/examples/amount_spec.rb +0 -77
- data/spec/examples/controller_spec.rb +0 -63
- data/spec/examples/validate_address_spec.rb +0 -38
- data/spec/i18n_localization_adapter_spec.rb +0 -83
- data/spec/lib/deterministic/class_mixin_spec.rb +0 -24
- data/spec/lib/deterministic/currify_spec.rb +0 -90
- data/spec/lib/deterministic/monad_axioms.rb +0 -46
- data/spec/lib/deterministic/monad_spec.rb +0 -47
- data/spec/lib/deterministic/null_spec.rb +0 -65
- data/spec/lib/deterministic/option_spec.rb +0 -140
- data/spec/lib/deterministic/result/failure_spec.rb +0 -65
- data/spec/lib/deterministic/result/result_map_spec.rb +0 -155
- data/spec/lib/deterministic/result/result_shared.rb +0 -25
- data/spec/lib/deterministic/result/success_spec.rb +0 -41
- data/spec/lib/deterministic/result_spec.rb +0 -63
- data/spec/lib/deterministic/sequencer_spec.rb +0 -506
- data/spec/lib/edge_cases_spec.rb +0 -156
- data/spec/lib/enum_spec.rb +0 -114
- data/spec/lib/native_pattern_matching_spec.rb +0 -74
- data/spec/localization_adapter_spec.rb +0 -66
- data/spec/organizer/with_reducer_spec.rb +0 -56
- data/spec/organizer_key_aliases_spec.rb +0 -29
- data/spec/organizer_spec.rb +0 -115
- data/spec/readme_spec.rb +0 -45
- data/spec/sample/calculates_order_tax_action_spec.rb +0 -16
- data/spec/sample/calculates_tax_spec.rb +0 -30
- data/spec/sample/looks_up_tax_percentage_action_spec.rb +0 -55
- data/spec/sample/provides_free_shipping_action_spec.rb +0 -25
- data/spec/sample/tax/calculates_order_tax_action.rb +0 -10
- data/spec/sample/tax/calculates_tax.rb +0 -11
- data/spec/sample/tax/looks_up_tax_percentage_action.rb +0 -28
- data/spec/sample/tax/provides_free_shipping_action.rb +0 -11
- data/spec/spec_helper.rb +0 -32
- data/spec/support.rb +0 -1
- data/spec/test_doubles.rb +0 -656
- data/spec/testing/context_factory/iterate_spec.rb +0 -39
- data/spec/testing/context_factory/reduce_if_spec.rb +0 -40
- data/spec/testing/context_factory/reduce_until_spec.rb +0 -40
- data/spec/testing/context_factory/with_callback_spec.rb +0 -38
- data/spec/testing/context_factory_spec.rb +0 -76
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require 'test_doubles'
|
|
3
|
-
|
|
4
|
-
RSpec.describe FunctionalLightService::Organizer do
|
|
5
|
-
class TestExecute
|
|
6
|
-
extend FunctionalLightService::Organizer
|
|
7
|
-
|
|
8
|
-
def self.call(context)
|
|
9
|
-
with(context).reduce(steps)
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def self.steps
|
|
13
|
-
[
|
|
14
|
-
TestDoubles::AddsOneAction,
|
|
15
|
-
execute(->(ctx) { ctx.number += 1 }),
|
|
16
|
-
execute(->(ctx) { ctx[:something] = 'hello' }),
|
|
17
|
-
TestDoubles::AddsOne.actions
|
|
18
|
-
]
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
let(:empty_context) { FunctionalLightService::Context.make }
|
|
23
|
-
|
|
24
|
-
it 'calls the lambda in the execute block using the context' do
|
|
25
|
-
result = TestExecute.call(:number => 0)
|
|
26
|
-
|
|
27
|
-
expect(result).to be_success
|
|
28
|
-
expect(result.number).to eq(3)
|
|
29
|
-
expect(result[:something]).to eq('hello')
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
it 'will not execute a failed context' do
|
|
33
|
-
empty_context.fail!('Something bad happened')
|
|
34
|
-
|
|
35
|
-
result = TestExecute.call(empty_context)
|
|
36
|
-
|
|
37
|
-
expect(result).to be_failure
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
it 'does not execute over a skipped context' do
|
|
41
|
-
empty_context.skip_remaining!('No more needed')
|
|
42
|
-
|
|
43
|
-
result = TestExecute.call(empty_context)
|
|
44
|
-
expect(result).to be_success
|
|
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
|
|
67
|
-
end
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require 'test_doubles'
|
|
3
|
-
|
|
4
|
-
RSpec.describe FunctionalLightService::Organizer do
|
|
5
|
-
let(:empty_context) { FunctionalLightService::Context.make }
|
|
6
|
-
|
|
7
|
-
it 'reduces each item of a collection and singularizes the collection key' do
|
|
8
|
-
result = TestDoubles::TestIterate.call(:number => 1,
|
|
9
|
-
:counters => [1, 2, 3, 4])
|
|
10
|
-
|
|
11
|
-
expect(result).to be_success
|
|
12
|
-
expect(result.number).to eq(5)
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
it 'accepts a single action or organizer' do
|
|
16
|
-
result = TestDoubles::TestIterate.call_single(:number => 1,
|
|
17
|
-
:counters => [1, 2, 3, 4])
|
|
18
|
-
|
|
19
|
-
expect(result).to be_success
|
|
20
|
-
expect(result.number).to eq(5)
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
it "knows that it's being iterated from within an organizer" do
|
|
24
|
-
result = TestDoubles::TestIterate.call(:number => 1,
|
|
25
|
-
:counters => [1, 2, 3, 4])
|
|
26
|
-
|
|
27
|
-
expect(result.organized_by).to eq TestDoubles::TestIterate
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
it 'will not iterate over a failed context' do
|
|
31
|
-
empty_context.fail!('Something bad happened')
|
|
32
|
-
|
|
33
|
-
result = TestDoubles::TestIterate.call(empty_context)
|
|
34
|
-
|
|
35
|
-
expect(result).to be_failure
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
it 'does not iterate over a skipped context' do
|
|
39
|
-
empty_context.skip_remaining!('No more needed')
|
|
40
|
-
|
|
41
|
-
result = TestDoubles::TestIterate.call(empty_context)
|
|
42
|
-
expect(result).to be_success
|
|
43
|
-
end
|
|
44
|
-
end
|
|
@@ -1,65 +0,0 @@
|
|
|
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
|
|
@@ -1,60 +0,0 @@
|
|
|
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
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require 'test_doubles'
|
|
3
|
-
|
|
4
|
-
RSpec.describe FunctionalLightService::Organizer do
|
|
5
|
-
class TestReduceIf
|
|
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(->(ctx) { ctx.number == 1 },
|
|
16
|
-
TestDoubles::AddsOneAction)
|
|
17
|
-
]
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
let(:empty_context) { FunctionalLightService::Context.make }
|
|
22
|
-
|
|
23
|
-
it 'reduces if the block evaluates to true' do
|
|
24
|
-
result = TestReduceIf.call(:number => 0)
|
|
25
|
-
|
|
26
|
-
expect(result).to be_success
|
|
27
|
-
expect(result[:number]).to eq(2)
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
it 'does not reduce if the block evaluates to false' do
|
|
31
|
-
result = TestReduceIf.call(:number => 2)
|
|
32
|
-
|
|
33
|
-
expect(result).to be_success
|
|
34
|
-
expect(result[:number]).to eq(3)
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
it 'will not reduce over a failed context' do
|
|
38
|
-
empty_context.fail!('Something bad happened')
|
|
39
|
-
|
|
40
|
-
result = TestReduceIf.call(empty_context)
|
|
41
|
-
|
|
42
|
-
expect(result).to be_failure
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
it 'does not reduce over a skipped context' do
|
|
46
|
-
empty_context.skip_remaining!('No more needed')
|
|
47
|
-
|
|
48
|
-
result = TestReduceIf.call(empty_context)
|
|
49
|
-
expect(result).to be_success
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
it "knows that it's being conditionally reduced from within an organizer" do
|
|
53
|
-
result = TestReduceIf.call(:number => 2)
|
|
54
|
-
|
|
55
|
-
expect(result.organized_by).to eq TestReduceIf
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
it 'skips actions within in its own scope' do
|
|
59
|
-
org = Class.new do
|
|
60
|
-
extend FunctionalLightService::Organizer
|
|
61
|
-
|
|
62
|
-
def self.call
|
|
63
|
-
reduce(actions)
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
def self.actions
|
|
67
|
-
[
|
|
68
|
-
reduce_if(
|
|
69
|
-
->(c) { !c.nil? },
|
|
70
|
-
[
|
|
71
|
-
execute(->(c) { c[:first_reduce_if] = true }),
|
|
72
|
-
execute(->(c) { c.skip_remaining! }), # rubocop:disable Style/SymbolProc
|
|
73
|
-
execute(->(c) { c[:second_reduce_if] = true })
|
|
74
|
-
]
|
|
75
|
-
),
|
|
76
|
-
execute(->(c) { c[:last_outside] = true })
|
|
77
|
-
]
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
result = org.call
|
|
82
|
-
|
|
83
|
-
aggregate_failures do
|
|
84
|
-
expect(result[:first_reduce_if]).to be true
|
|
85
|
-
expect(result[:second_reduce_if]).to be_nil
|
|
86
|
-
expect(result[:last_outside]).to be true
|
|
87
|
-
end
|
|
88
|
-
end
|
|
89
|
-
end
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require 'test_doubles'
|
|
3
|
-
|
|
4
|
-
RSpec.describe FunctionalLightService::Organizer do
|
|
5
|
-
class TestReduceUntil
|
|
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_until(->(ctx) { ctx.number == 3 },
|
|
15
|
-
TestDoubles::AddsOneAction)
|
|
16
|
-
]
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
let(:empty_context) { FunctionalLightService::Context.make }
|
|
21
|
-
|
|
22
|
-
it 'reduces until the block evaluates to true' do
|
|
23
|
-
context = { :number => 1 }
|
|
24
|
-
result = TestReduceUntil.call(context)
|
|
25
|
-
|
|
26
|
-
expect(result).to be_success
|
|
27
|
-
expect(result.number).to eq(3)
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
it 'does not execute on failed context' do
|
|
31
|
-
empty_context.fail!('Something bad happened')
|
|
32
|
-
|
|
33
|
-
result = TestReduceUntil.call(empty_context)
|
|
34
|
-
expect(result).to be_failure
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
it 'does not execute a skipped context' do
|
|
38
|
-
empty_context.skip_remaining!('No more needed')
|
|
39
|
-
|
|
40
|
-
result = TestReduceUntil.call(empty_context)
|
|
41
|
-
expect(result).to be_success
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
it "is expected to know its organizer when reducing until a condition" do
|
|
45
|
-
result = TestReduceUntil.call(:number => 1)
|
|
46
|
-
|
|
47
|
-
expect(result.organized_by).to eq TestReduceUntil
|
|
48
|
-
end
|
|
49
|
-
end
|
|
@@ -1,96 +0,0 @@
|
|
|
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
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require 'test_doubles'
|
|
3
|
-
|
|
4
|
-
RSpec.describe FunctionalLightService::Organizer do
|
|
5
|
-
describe 'a simple case with a single callback' do
|
|
6
|
-
it 'calls the actions defined with callback' do
|
|
7
|
-
result = TestDoubles::TestWithCallback.call
|
|
8
|
-
|
|
9
|
-
expect(result.counter).to eq(3)
|
|
10
|
-
expect(result.total).to eq(6)
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
describe 'a more complex example with nested callbacks' do
|
|
15
|
-
class TestWithNestedCallback
|
|
16
|
-
extend FunctionalLightService::Organizer
|
|
17
|
-
|
|
18
|
-
def self.call(context = {})
|
|
19
|
-
with(context).reduce(actions)
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
def self.actions
|
|
23
|
-
[
|
|
24
|
-
SetUpNestedContextAction,
|
|
25
|
-
with_callback(IterateOuterCollectionAction,
|
|
26
|
-
[IncrementOuterCountAction,
|
|
27
|
-
with_callback(TestDoubles::IterateCollectionAction,
|
|
28
|
-
[TestDoubles::IncrementCountAction,
|
|
29
|
-
TestDoubles::AddToTotalAction])])
|
|
30
|
-
]
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
class SetUpNestedContextAction
|
|
35
|
-
extend FunctionalLightService::Action
|
|
36
|
-
|
|
37
|
-
promises :outer_numbers, :outer_counter,
|
|
38
|
-
:numbers, :counter, :total
|
|
39
|
-
|
|
40
|
-
executed do |ctx|
|
|
41
|
-
ctx.outer_numbers = [12, 17]
|
|
42
|
-
ctx.outer_counter = 0
|
|
43
|
-
ctx.numbers = [1, 2, 3]
|
|
44
|
-
ctx.counter = 0
|
|
45
|
-
ctx.total = 0
|
|
46
|
-
end
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
class IterateOuterCollectionAction
|
|
50
|
-
extend FunctionalLightService::Action
|
|
51
|
-
|
|
52
|
-
expects :outer_numbers, :callback
|
|
53
|
-
promises :outer_number
|
|
54
|
-
|
|
55
|
-
executed do |ctx|
|
|
56
|
-
ctx.outer_numbers.each do |outer_number|
|
|
57
|
-
ctx.outer_number = outer_number
|
|
58
|
-
ctx.callback.call(ctx)
|
|
59
|
-
end
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
class IncrementOuterCountAction
|
|
64
|
-
extend FunctionalLightService::Action
|
|
65
|
-
|
|
66
|
-
expects :outer_counter
|
|
67
|
-
|
|
68
|
-
executed do |ctx|
|
|
69
|
-
ctx.outer_counter = ctx.outer_counter + 1
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
it 'calls both the action and the nested callbacks' do
|
|
74
|
-
result = TestWithNestedCallback.call
|
|
75
|
-
|
|
76
|
-
expect(result.outer_counter).to eq(2)
|
|
77
|
-
# Counts and total are the duplicates of
|
|
78
|
-
# what you'll see in the simple spec,
|
|
79
|
-
# as the internal callback logic is called
|
|
80
|
-
# twice due to 2 items in the outer_numbers
|
|
81
|
-
# collection.
|
|
82
|
-
expect(result.counter).to eq(6)
|
|
83
|
-
expect(result.total).to eq(12)
|
|
84
|
-
end
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
describe 'with failed or skipped context' do
|
|
88
|
-
class TestWithFailureCallback
|
|
89
|
-
extend FunctionalLightService::Organizer
|
|
90
|
-
|
|
91
|
-
def self.call(context = {})
|
|
92
|
-
with(context).reduce(actions)
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
def self.actions
|
|
96
|
-
[
|
|
97
|
-
TestDoubles::SetUpContextAction,
|
|
98
|
-
with_callback(TestDoubles::IterateCollectionAction,
|
|
99
|
-
[TestDoubles::IncrementCountAction,
|
|
100
|
-
TestDoubles::FailureAction])
|
|
101
|
-
]
|
|
102
|
-
end
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
it 'will not process the routine' do
|
|
106
|
-
result = TestWithFailureCallback.call
|
|
107
|
-
|
|
108
|
-
expect(result).to be_failure
|
|
109
|
-
expect(result.counter).to eq(1)
|
|
110
|
-
expect(result.total).to eq(0)
|
|
111
|
-
end
|
|
112
|
-
end
|
|
113
|
-
end
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require 'test_doubles'
|
|
3
|
-
|
|
4
|
-
describe "Organizer entry point" do
|
|
5
|
-
context "when the organizer entry method is not named `call`" do
|
|
6
|
-
it "works without emitting any warning" do
|
|
7
|
-
class OrganizerWithoutCallMethod
|
|
8
|
-
extend FunctionalLightService::Organizer
|
|
9
|
-
|
|
10
|
-
def self.do_something
|
|
11
|
-
reduce([])
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
result = nil
|
|
16
|
-
expect { result = OrganizerWithoutCallMethod.do_something }
|
|
17
|
-
.not_to output.to_stdout
|
|
18
|
-
expect(result).to be_a_kind_of(FunctionalLightService::Context)
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
context "when the organizer has the `call` method" do
|
|
23
|
-
it "works without emitting any warning" do
|
|
24
|
-
class OrganizerWithCallMethod
|
|
25
|
-
extend FunctionalLightService::Organizer
|
|
26
|
-
|
|
27
|
-
def self.call
|
|
28
|
-
reduce([])
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
expect(OrganizerWithCallMethod.call).to be_a_kind_of(FunctionalLightService::Context)
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
end
|