functional-light-service 6.1.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.
Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. data/lib/functional-light-service.rb +6 -31
  3. metadata +15 -297
  4. data/.github/workflows/project-build.yml +0 -71
  5. data/.gitignore +0 -22
  6. data/.rspec +0 -3
  7. data/.rubocop.yml +0 -101
  8. data/.solargraph.yml +0 -11
  9. data/AUDIT-functional-light-service.md +0 -352
  10. data/Appraisals +0 -7
  11. data/CHANGELOG.md +0 -273
  12. data/CODE_OF_CONDUCT.md +0 -22
  13. data/Gemfile +0 -4
  14. data/LICENSE +0 -22
  15. data/README.md +0 -1578
  16. data/Rakefile +0 -12
  17. data/VERSION +0 -1
  18. data/audit/bench.rb +0 -99
  19. data/audit/verify_findings.rb +0 -172
  20. data/functional-light-service.gemspec +0 -33
  21. data/gemfiles/dry_inflector_0_2_1.gemfile +0 -5
  22. data/gemfiles/i18n_1_8_11.gemfile +0 -5
  23. data/lib/functional-light-service/action.rb +0 -97
  24. data/lib/functional-light-service/configuration.rb +0 -26
  25. data/lib/functional-light-service/context/key_verifier.rb +0 -124
  26. data/lib/functional-light-service/context.rb +0 -195
  27. data/lib/functional-light-service/deprecations.rb +0 -26
  28. data/lib/functional-light-service/errors.rb +0 -8
  29. data/lib/functional-light-service/functional/enum.rb +0 -286
  30. data/lib/functional-light-service/functional/maybe.rb +0 -21
  31. data/lib/functional-light-service/functional/monad.rb +0 -77
  32. data/lib/functional-light-service/functional/null.rb +0 -88
  33. data/lib/functional-light-service/functional/option.rb +0 -100
  34. data/lib/functional-light-service/functional/result.rb +0 -129
  35. data/lib/functional-light-service/functional/sequencer.rb +0 -144
  36. data/lib/functional-light-service/localization_adapter.rb +0 -48
  37. data/lib/functional-light-service/organizer/execute.rb +0 -16
  38. data/lib/functional-light-service/organizer/iterate.rb +0 -30
  39. data/lib/functional-light-service/organizer/reduce_if.rb +0 -19
  40. data/lib/functional-light-service/organizer/reduce_until.rb +0 -22
  41. data/lib/functional-light-service/organizer/scoped_reducable.rb +0 -15
  42. data/lib/functional-light-service/organizer/with_callback.rb +0 -28
  43. data/lib/functional-light-service/organizer/with_reducer.rb +0 -81
  44. data/lib/functional-light-service/organizer/with_reducer_factory.rb +0 -20
  45. data/lib/functional-light-service/organizer/with_reducer_log_decorator.rb +0 -110
  46. data/lib/functional-light-service/organizer.rb +0 -114
  47. data/lib/functional-light-service/testing/context_factory.rb +0 -48
  48. data/lib/functional-light-service/testing.rb +0 -3
  49. data/lib/functional-light-service/version.rb +0 -5
  50. data/resources/fail_actions.png +0 -0
  51. data/resources/light-service.png +0 -0
  52. data/resources/organizer_and_actions.png +0 -0
  53. data/resources/skip_actions.png +0 -0
  54. data/spec/acceptance/add_numbers_spec.rb +0 -11
  55. data/spec/acceptance/after_actions_spec.rb +0 -87
  56. data/spec/acceptance/around_each_spec.rb +0 -19
  57. data/spec/acceptance/before_actions_spec.rb +0 -115
  58. data/spec/acceptance/custom_log_from_organizer_spec.rb +0 -61
  59. data/spec/acceptance/deprecation_warnings_spec.rb +0 -82
  60. data/spec/acceptance/fail_spec.rb +0 -52
  61. data/spec/acceptance/log_from_organizer_spec.rb +0 -154
  62. data/spec/acceptance/message_localization_spec.rb +0 -119
  63. data/spec/acceptance/organizer/add_aliases_spec.rb +0 -28
  64. data/spec/acceptance/organizer/add_to_context_spec.rb +0 -30
  65. data/spec/acceptance/organizer/around_each_with_reduce_if_spec.rb +0 -42
  66. data/spec/acceptance/organizer/context_failure_and_skipping_spec.rb +0 -68
  67. data/spec/acceptance/organizer/execute_spec.rb +0 -46
  68. data/spec/acceptance/organizer/iterate_spec.rb +0 -44
  69. data/spec/acceptance/organizer/reduce_if_spec.rb +0 -89
  70. data/spec/acceptance/organizer/reduce_until_spec.rb +0 -49
  71. data/spec/acceptance/organizer/with_callback_spec.rb +0 -113
  72. data/spec/acceptance/organizer_entry_point_spec.rb +0 -35
  73. data/spec/acceptance/rollback_spec.rb +0 -183
  74. data/spec/acceptance/testing/context_factory_spec.rb +0 -54
  75. data/spec/action_expected_keys_spec.rb +0 -63
  76. data/spec/action_expects_and_promises_spec.rb +0 -97
  77. data/spec/action_promised_keys_spec.rb +0 -126
  78. data/spec/action_spec.rb +0 -97
  79. data/spec/context/inspect_spec.rb +0 -42
  80. data/spec/context_spec.rb +0 -289
  81. data/spec/examples/amount_spec.rb +0 -77
  82. data/spec/examples/controller_spec.rb +0 -63
  83. data/spec/examples/validate_address_spec.rb +0 -38
  84. data/spec/lib/deterministic/class_mixin_spec.rb +0 -24
  85. data/spec/lib/deterministic/currify_spec.rb +0 -90
  86. data/spec/lib/deterministic/monad_axioms.rb +0 -46
  87. data/spec/lib/deterministic/monad_spec.rb +0 -47
  88. data/spec/lib/deterministic/null_spec.rb +0 -65
  89. data/spec/lib/deterministic/option_spec.rb +0 -140
  90. data/spec/lib/deterministic/result/failure_spec.rb +0 -65
  91. data/spec/lib/deterministic/result/result_map_spec.rb +0 -155
  92. data/spec/lib/deterministic/result/result_shared.rb +0 -25
  93. data/spec/lib/deterministic/result/success_spec.rb +0 -41
  94. data/spec/lib/deterministic/result_spec.rb +0 -63
  95. data/spec/lib/deterministic/sequencer_spec.rb +0 -506
  96. data/spec/lib/edge_cases_spec.rb +0 -156
  97. data/spec/lib/enum_spec.rb +0 -114
  98. data/spec/lib/native_pattern_matching_spec.rb +0 -74
  99. data/spec/localization_adapter_spec.rb +0 -83
  100. data/spec/organizer/with_reducer_spec.rb +0 -56
  101. data/spec/organizer_key_aliases_spec.rb +0 -29
  102. data/spec/organizer_spec.rb +0 -115
  103. data/spec/readme_spec.rb +0 -45
  104. data/spec/sample/calculates_order_tax_action_spec.rb +0 -16
  105. data/spec/sample/calculates_tax_spec.rb +0 -30
  106. data/spec/sample/looks_up_tax_percentage_action_spec.rb +0 -55
  107. data/spec/sample/provides_free_shipping_action_spec.rb +0 -25
  108. data/spec/sample/tax/calculates_order_tax_action.rb +0 -10
  109. data/spec/sample/tax/calculates_tax.rb +0 -11
  110. data/spec/sample/tax/looks_up_tax_percentage_action.rb +0 -28
  111. data/spec/sample/tax/provides_free_shipping_action.rb +0 -11
  112. data/spec/spec_helper.rb +0 -29
  113. data/spec/support.rb +0 -1
  114. data/spec/test_doubles.rb +0 -628
  115. data/spec/testing/context_factory/iterate_spec.rb +0 -39
  116. data/spec/testing/context_factory/reduce_if_spec.rb +0 -40
  117. data/spec/testing/context_factory/reduce_until_spec.rb +0 -40
  118. data/spec/testing/context_factory/with_callback_spec.rb +0 -38
  119. data/spec/testing/context_factory_spec.rb +0 -76
@@ -1,52 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe "fail_and_return!" do
4
- describe "returns immediately from executed block" do
5
- class FailAndReturnAction
6
- extend FunctionalLightService::Action
7
-
8
- promises :one, :two
9
-
10
- executed do |ctx|
11
- ctx.one = 1
12
- # Have to set it in Context
13
- ctx.two = nil
14
-
15
- ctx.fail_and_return!('Something went wrong')
16
- ctx.two = 2
17
- end
18
- end
19
-
20
- it "returns immediately from executed block" do
21
- result = FailAndReturnAction.execute
22
-
23
- expect(result).to be_failure
24
- expect(result.two).to be_nil
25
- end
26
- end
27
-
28
- describe "accepts error_code option" do
29
- class FailAndReturnWithErrorCodeAction
30
- extend FunctionalLightService::Action
31
-
32
- promises :one, :two
33
-
34
- executed do |ctx|
35
- ctx.one = 1
36
- # Have to set it in Context
37
- ctx.two = nil
38
-
39
- ctx.fail_and_return!('Something went wrong', :error_code => 401)
40
- ctx.two = 2
41
- end
42
- end
43
-
44
- it "returned context contains the error_code" do
45
- result = FailAndReturnWithErrorCodeAction.execute
46
-
47
- expect(result).to be_failure
48
- expect(result.error_code).to eq 401
49
- expect(result.two).to be_nil
50
- end
51
- end
52
- end
@@ -1,154 +0,0 @@
1
- require 'spec_helper'
2
- require 'test_doubles'
3
- require 'stringio'
4
-
5
- describe "Logs from organizer" do
6
- def collects_log
7
- original_logger = FunctionalLightService::Configuration.logger
8
-
9
- strio = StringIO.new
10
- FunctionalLightService::Configuration.logger = Logger.new(strio)
11
-
12
- yield
13
-
14
- FunctionalLightService::Configuration.logger = original_logger
15
-
16
- strio.string
17
- end
18
-
19
- context "when every action has expects or promises" do
20
- subject(:log_message) do
21
- collects_log do
22
- TestDoubles::MakesTeaAndCappuccino
23
- .call("black tea", "2% milk", "espresso coffee")
24
- end
25
- end
26
-
27
- it "describes what organizer was invoked" do
28
- organizer_log_message = "[FunctionalLightService] - calling organizer " \
29
- "<TestDoubles::MakesTeaAndCappuccino>"
30
- expect(log_message).to include(organizer_log_message)
31
- end
32
-
33
- it "describes the actions invoked" do
34
- organizer_log_message = "[FunctionalLightService] - executing " \
35
- "<TestDoubles::MakesTeaWithMilkAction>"
36
- expect(log_message).to include(organizer_log_message)
37
- organizer_log_message = "[FunctionalLightService] - executing " \
38
- "<TestDoubles::MakesLatteAction>"
39
- expect(log_message).to include(organizer_log_message)
40
- end
41
-
42
- it "lists the keys in context before the actions are executed" do
43
- organizer_log_message = "[FunctionalLightService] - " \
44
- "keys in context: :tea, :milk, :coffee"
45
- expect(log_message).to include(organizer_log_message)
46
- end
47
-
48
- it "lists the expects actions are expecting" do
49
- organizer_log_message = "[FunctionalLightService] - expects: :tea, :milk"
50
- expect(log_message).to include(organizer_log_message)
51
- organizer_log_message = "[FunctionalLightService] - expects: :coffee, :milk"
52
- expect(log_message).to include(organizer_log_message)
53
- end
54
-
55
- it "lists the promises actions are promising" do
56
- organizer_log_message = "[FunctionalLightService] - promises: :milk_tea"
57
- expect(log_message).to include(organizer_log_message)
58
- organizer_log_message = "[FunctionalLightService] - promises: :latte"
59
- expect(log_message).to include(organizer_log_message)
60
- end
61
-
62
- it "lists the keys in contect after the actions are executed" do
63
- organizer_log_message = "[FunctionalLightService] - keys in context: " \
64
- ":tea, :milk, :coffee, :milk_tea, :latte"
65
- expect(log_message).to include(organizer_log_message)
66
- end
67
- end
68
-
69
- context "when NOT every action expects or promises" do
70
- subject(:log_message) do
71
- collects_log do
72
- TestDoubles::MakesCappuccinoAddsTwo.call("2% milk", "espresso coffee")
73
- end
74
- end
75
-
76
- it "describes what organizer was invoked" do
77
- organizer_log_message = "[FunctionalLightService] - calling organizer " \
78
- "<TestDoubles::MakesCappuccinoAddsTwo>"
79
- expect(log_message).to include(organizer_log_message)
80
- end
81
-
82
- it "does not list empty expects or promises" do
83
- organizer_log_message = "[FunctionalLightService] - expects:\n"
84
- expect(log_message).not_to include(organizer_log_message)
85
- organizer_log_message = "[FunctionalLightService] - promises:\n"
86
- expect(log_message).not_to include(organizer_log_message)
87
- end
88
- end
89
-
90
- context "when the context has failed" do
91
- subject(:log_message) do
92
- collects_log do
93
- TestDoubles::MakesCappuccinoAddsTwoAndFails
94
- .call("espresso coffee")
95
- end
96
- end
97
-
98
- it "logs it with a warning" do
99
- organizer_log_message = "WARN -- : [FunctionalLightService] - :-((( " \
100
- "<TestDoubles::MakesLatteAction> has failed..."
101
- expect(log_message).to include(organizer_log_message)
102
- organizer_log_message = "WARN -- : [FunctionalLightService] - context message: " \
103
- "Can't make a latte from a milk that's very hot!"
104
- expect(log_message).to include(organizer_log_message)
105
- organizer_log_message = "[FunctionalLightService] - :-((( " \
106
- "<TestDoubles::AddsTwoAction> has failed..."
107
- expect(log_message).not_to include(organizer_log_message)
108
- end
109
- end
110
-
111
- context "when the context has failed with rollback" do
112
- subject(:log_message) do
113
- collects_log do
114
- TestDoubles::MakesCappuccinoAddsTwoAndFails
115
- .call("espresso coffee", :super_hot)
116
- end
117
- end
118
-
119
- it "logs it with a warning" do
120
- organizer_log_message = "WARN -- : [FunctionalLightService] - :-((( " \
121
- "<TestDoubles::MakesLatteAction> has failed..."
122
- expect(log_message).to include(organizer_log_message)
123
- organizer_log_message = "WARN -- : [FunctionalLightService] - context message: " \
124
- "Can't make a latte from a milk that's super hot!"
125
- expect(log_message).to include(organizer_log_message)
126
- organizer_log_message = "[FunctionalLightService] - :-((( " \
127
- "<TestDoubles::AddsTwoAction> " \
128
- "has failed..."
129
- expect(log_message).not_to include(organizer_log_message)
130
- end
131
- end
132
-
133
- context "when the context is skipping the rest" do
134
- subject(:log_message) do
135
- collects_log do
136
- TestDoubles::MakesCappuccinoSkipsAddsTwo.call("espresso coffee")
137
- end
138
- end
139
-
140
- it "logs it with a warning" do
141
- organizer_log_message = "INFO -- : [FunctionalLightService] - ;-) " \
142
- "<TestDoubles::MakesLatteAction> has decided " \
143
- "to skip the rest of the actions"
144
- expect(log_message).to include(organizer_log_message)
145
- organizer_log_message = "INFO -- : [FunctionalLightService] - context message: " \
146
- "Can't make a latte with a fatty milk like that!"
147
- expect(log_message).to include(organizer_log_message)
148
- organizer_log_message = "INFO -- : [FunctionalLightService] - ;-) " \
149
- "<TestDoubles::AddsTwoAction> has decided " \
150
- "to skip the rest of the actions"
151
- expect(log_message).not_to include(organizer_log_message)
152
- end
153
- end
154
- end
@@ -1,119 +0,0 @@
1
- require "spec_helper"
2
- require "test_doubles"
3
-
4
- class TestsLocalizationAdapter
5
- extend FunctionalLightService::Organizer
6
-
7
- def self.call(pass_or_fail, message_or_key, i18n_options = {})
8
- with(
9
- :pass_or_fail => pass_or_fail,
10
- :message_or_key => message_or_key,
11
- :i18n_options => i18n_options
12
- ).reduce(TestsLocalizationInvocationOptionsAction)
13
- end
14
- end
15
-
16
- class TestsLocalizationInvocationOptionsAction
17
- extend FunctionalLightService::Action
18
-
19
- expects :pass_or_fail, :message_or_key, :i18n_options
20
-
21
- executed do |context|
22
- if context.pass_or_fail == true
23
- context.succeed!(context.message_or_key, context.i18n_options)
24
- else
25
- context.fail!(context.message_or_key, context.i18n_options)
26
- end
27
- end
28
- end
29
-
30
- def pass_with(message_or_key, i18n_options = {})
31
- TestsLocalizationAdapter.call(true, message_or_key, i18n_options)
32
- end
33
-
34
- def fail_with(message_or_key, i18n_options = {})
35
- TestsLocalizationAdapter.call(false, message_or_key, i18n_options)
36
- end
37
-
38
- describe "Localization Adapter" do
39
- before do
40
- I18n.backend.store_translations(
41
- :en,
42
- :tests_localization_invocation_options_action =>
43
- {
44
- :light_service => {
45
- :failures => {
46
- :some_failure_reason => "This has failed",
47
- :failure_with_interpolation => "Failed with %{reason}"
48
- },
49
- :successes => {
50
- :some_success_reason => "This has passed",
51
- :success_with_interpolation => "Passed with %{reason}"
52
- }
53
- }
54
- }
55
- )
56
- end
57
-
58
- describe "passing a simple string message" do
59
- describe "by failing the context" do
60
- it "returns the string" do
61
- result = fail_with("string message")
62
-
63
- expect(result).to be_failure
64
- expect(result.message).to eq("string message")
65
- end
66
- end
67
-
68
- describe "by passing the context" do
69
- it "returns the string" do
70
- result = pass_with("string message")
71
-
72
- expect(result).to be_success
73
- expect(result.message).to eq("string message")
74
- end
75
- end
76
- end
77
-
78
- describe "passing a Symbol" do
79
- describe "by failing the context" do
80
- it "performs a translation" do
81
- result = fail_with(:some_failure_reason)
82
-
83
- expect(result).to be_failure
84
- expect(result.message).to eq("This has failed")
85
- end
86
- end
87
-
88
- describe "by passing the contenxt" do
89
- it "performs a translation" do
90
- result = pass_with(:some_success_reason)
91
-
92
- expect(result).to be_success
93
- expect(result.message).to eq("This has passed")
94
- end
95
- end
96
- end
97
-
98
- describe "passing a Symbol with interpolation variables" do
99
- describe "by failing the context" do
100
- it "performs a translation with interpolation" do
101
- result = fail_with(:failure_with_interpolation,
102
- :reason => "bad account")
103
-
104
- expect(result).to be_failure
105
- expect(result.message).to eq("Failed with bad account")
106
- end
107
- end
108
-
109
- describe "by passing the context" do
110
- it "performs a translation with interpolation" do
111
- result = pass_with(:success_with_interpolation,
112
- :reason => "account in good standing")
113
-
114
- expect(result).to be_success
115
- expect(result.message).to eq("Passed with account in good standing")
116
- end
117
- end
118
- end
119
- end
@@ -1,28 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe FunctionalLightService::Organizer do
4
- class TestAddAliases
5
- extend FunctionalLightService::Organizer
6
-
7
- def self.call(context = FunctionalLightService::Context.make)
8
- with(context).reduce(steps)
9
- end
10
-
11
- def self.steps
12
- [
13
- add_to_context(:my_message => "Hello There"),
14
- # This will add the alias `:a_message` which points
15
- # to the :my_message key's value
16
- add_aliases(:my_message => :a_message),
17
- TestDoubles::CapitalizeMessage
18
- ]
19
- end
20
- end
21
-
22
- it "adds aliases to the context embedded in the series of actions" do
23
- result = TestAddAliases.call
24
-
25
- expect(result).to be_success
26
- expect(result.final_message).to eq("HELLO THERE")
27
- end
28
- end
@@ -1,30 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe FunctionalLightService::Organizer do
4
- class TestAddToContext
5
- extend FunctionalLightService::Organizer
6
-
7
- def self.call(context = FunctionalLightService::Context.make)
8
- with(context).reduce(steps)
9
- end
10
-
11
- def self.steps
12
- [
13
- # This will add the `:number` key to the context
14
- # with the value of 0, so it's available for
15
- # AddsOneAction
16
- add_to_context(:number => 0),
17
- TestDoubles::AddsOneAction,
18
- add_to_context(:something => "hello")
19
- ]
20
- end
21
- end
22
-
23
- it "adds items to the context on the fly" do
24
- result = TestAddToContext.call
25
-
26
- expect(result).to be_success
27
- expect(result.number).to eq(1)
28
- expect(result[:something]).to eq("hello")
29
- end
30
- end
@@ -1,42 +0,0 @@
1
- require 'spec_helper'
2
- require 'test_doubles'
3
-
4
- RSpec.describe FunctionalLightService::Organizer do
5
- class TestReduceIfWithAroundEach
6
- extend FunctionalLightService::Organizer
7
-
8
- def self.call(context)
9
- with(context)
10
- .around_each(TestDoubles::AroundEachLoggerHandler)
11
- .reduce(actions)
12
- end
13
-
14
- def self.actions
15
- [
16
- TestDoubles::AddsOneAction,
17
- reduce_if(->(ctx) { ctx.number == 1 },
18
- TestDoubles::AddsOneAction)
19
- ]
20
- end
21
- end
22
-
23
- it 'can be used to log data' do
24
- result =
25
- TestReduceIfWithAroundEach
26
- .call(:number => 0,
27
- :logger => TestDoubles::TestLogger.new)
28
-
29
- expect(result.fetch(:number)).to eq(2)
30
- expect(result[:logger].logs).to eq(
31
- [{
32
- :action => TestDoubles::AddsOneAction,
33
- :before => 0,
34
- :after => 1
35
- }, {
36
- :action => TestDoubles::AddsOneAction,
37
- :before => 1,
38
- :after => 2
39
- }]
40
- )
41
- end
42
- end
@@ -1,68 +0,0 @@
1
- require 'spec_helper'
2
- require 'test_doubles'
3
-
4
- RSpec.describe FunctionalLightService::Organizer do
5
- class TestSkipBefore
6
- extend FunctionalLightService::Organizer
7
-
8
- def self.call
9
- with(:number => 1)
10
- .reduce([
11
- TestDoubles::SkipAllAction,
12
- reduce_until(->(ctx) { ctx.number == 3 },
13
- TestDoubles::AddsOneAction)
14
- ])
15
- end
16
- end
17
-
18
- class TestSkipAfter
19
- extend FunctionalLightService::Organizer
20
-
21
- def self.call
22
- with(:number => 1)
23
- .reduce([
24
- TestDoubles::AddsOneAction,
25
- reduce_until(->(ctx) { ctx.number == 3 }, [
26
- TestDoubles::AddsOneAction
27
- ]),
28
- TestDoubles::SkipAllAction,
29
- TestDoubles::AddsOneAction
30
- ])
31
- end
32
- end
33
-
34
- class TestContextFailure
35
- extend FunctionalLightService::Organizer
36
-
37
- def self.call
38
- with(:number => 1)
39
- .reduce([
40
- TestDoubles::FailureAction,
41
- reduce_until(->(ctx) { ctx[:number] == 3 },
42
- TestDoubles::AddsOneAction),
43
- TestDoubles::AddsOneAction
44
- ])
45
- end
46
- end
47
-
48
- it 'skips all the rest of the actions' do
49
- result = TestSkipBefore.call
50
-
51
- expect(result).to be_success
52
- expect(result[:number]).to eq(1)
53
- end
54
-
55
- it 'skips after an action in nested context' do
56
- result = TestSkipAfter.call
57
-
58
- expect(result).to be_success
59
- expect(result[:number]).to eq(3)
60
- end
61
-
62
- it 'respects failure across all nestings' do
63
- result = TestContextFailure.call
64
-
65
- expect(result).to be_failure
66
- expect(result[:number]).to eq(1)
67
- end
68
- end
@@ -1,46 +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
- 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,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