convenient_service 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +1 -1
  5. data/ROADMAP.md +14 -3
  6. data/Taskfile.yml +30 -0
  7. data/convenient_service.gemspec +3 -1
  8. data/lib/convenient_service/aliases.rb +1 -0
  9. data/lib/convenient_service/common/plugins/assigns_attributes_in_constructor/using_active_model_attribute_assignment/concern.rb +0 -2
  10. data/lib/convenient_service/common/plugins/assigns_attributes_in_constructor/using_dry_initializer/concern.rb +0 -2
  11. data/lib/convenient_service/core/entities/config/entities/method_middlewares/entities/container/commands/resolve_methods_middlewares_callers.rb +1 -24
  12. data/lib/convenient_service/dependencies.rb +7 -0
  13. data/lib/convenient_service/examples/dry/gemfile/dry_service/config.rb +3 -1
  14. data/lib/convenient_service/examples/rails/gemfile/rails_service/config.rb +3 -1
  15. data/lib/convenient_service/examples/standard/request_params/constants.rb +15 -0
  16. data/lib/convenient_service/examples/standard/request_params/entities/description.rb +40 -0
  17. data/lib/convenient_service/examples/standard/request_params/entities/format.rb +40 -0
  18. data/lib/convenient_service/examples/standard/request_params/entities/id.rb +47 -0
  19. data/lib/convenient_service/examples/standard/request_params/entities/logger.rb +21 -0
  20. data/lib/convenient_service/examples/standard/request_params/entities/request.rb +23 -0
  21. data/lib/convenient_service/examples/standard/request_params/entities/source.rb +40 -0
  22. data/lib/convenient_service/examples/standard/request_params/entities/tag.rb +40 -0
  23. data/lib/convenient_service/examples/standard/request_params/entities/title.rb +40 -0
  24. data/lib/convenient_service/examples/standard/request_params/entities.rb +11 -0
  25. data/lib/convenient_service/examples/standard/request_params/services/apply_default_param_values.rb +26 -0
  26. data/lib/convenient_service/examples/standard/request_params/services/cast_params.rb +38 -0
  27. data/lib/convenient_service/examples/standard/request_params/services/extract_params_from_body.rb +70 -0
  28. data/lib/convenient_service/examples/standard/request_params/services/extract_params_from_path.rb +62 -0
  29. data/lib/convenient_service/examples/standard/request_params/services/filter_out_unpermitted_params.rb +26 -0
  30. data/lib/convenient_service/examples/standard/request_params/services/log_request_params.rb +54 -0
  31. data/lib/convenient_service/examples/standard/request_params/services/merge_params.rb +26 -0
  32. data/lib/convenient_service/examples/standard/request_params/services/prepare.rb +65 -0
  33. data/lib/convenient_service/examples/standard/request_params/services/validate_casted_params.rb +94 -0
  34. data/lib/convenient_service/examples/standard/request_params/services/validate_uncasted_params.rb +72 -0
  35. data/lib/convenient_service/examples/standard/request_params/services.rb +13 -0
  36. data/lib/convenient_service/examples/standard/request_params/utils/array/wrap.rb +46 -0
  37. data/lib/convenient_service/examples/standard/request_params/utils/array.rb +21 -0
  38. data/lib/convenient_service/examples/standard/request_params/utils/http/request/parse_body.rb +42 -0
  39. data/lib/convenient_service/examples/standard/request_params/utils/http/request/parse_path.rb +40 -0
  40. data/lib/convenient_service/examples/standard/request_params/utils/http/request.rb +28 -0
  41. data/lib/convenient_service/examples/standard/request_params/utils/http.rb +3 -0
  42. data/lib/convenient_service/examples/standard/request_params/utils/integer/safe_parse.rb +31 -0
  43. data/lib/convenient_service/examples/standard/request_params/utils/integer.rb +25 -0
  44. data/lib/convenient_service/examples/standard/request_params/utils/json/safe_parse.rb +40 -0
  45. data/lib/convenient_service/examples/standard/request_params/utils/json.rb +21 -0
  46. data/lib/convenient_service/examples/standard/request_params/utils/object/blank.rb +34 -0
  47. data/lib/convenient_service/examples/standard/request_params/utils/object/present.rb +31 -0
  48. data/lib/convenient_service/examples/standard/request_params/utils/object.rb +26 -0
  49. data/lib/convenient_service/examples/standard/request_params/utils.rb +7 -0
  50. data/lib/convenient_service/examples/standard/request_params.rb +48 -0
  51. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/commands/generate_printable_method.rb +50 -0
  52. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/commands.rb +3 -0
  53. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments/commands/apply_stub_to_track_delegations.rb +78 -0
  54. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments/commands/generate_printable_arguments.rb +100 -0
  55. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments/commands.rb +4 -0
  56. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/arguments.rb +95 -0
  57. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/base.rb +87 -0
  58. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/return_its_value.rb +129 -0
  59. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/with_any_arguments.rb +37 -0
  60. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/with_concrete_arguments.rb +37 -0
  61. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/sub_matchers/without_arguments.rb +37 -0
  62. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/values/base.rb +41 -0
  63. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/values/with_calling_original.rb +30 -0
  64. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings/values/without_calling_original.rb +30 -0
  65. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings.rb +12 -0
  66. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings_collection/errors.rb +57 -0
  67. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/chainings_collection.rb +171 -0
  68. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities/delegation.rb +79 -0
  69. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher/entities.rb +5 -0
  70. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities/matcher.rb +276 -0
  71. data/lib/convenient_service/rspec/matchers/custom/delegate_to/entities.rb +3 -0
  72. data/lib/convenient_service/rspec/matchers/custom/delegate_to.rb +58 -234
  73. data/lib/convenient_service/rspec/matchers/custom/singleton_prepend_module.rb +79 -0
  74. data/lib/convenient_service/rspec/matchers/custom.rb +1 -0
  75. data/lib/convenient_service/rspec/matchers/singleton_prepend_module.rb +13 -0
  76. data/lib/convenient_service/rspec/matchers.rb +2 -0
  77. data/lib/convenient_service/service/plugins/has_result_steps/concern.rb +25 -0
  78. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method.rb +3 -0
  79. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_caller.rb +8 -1
  80. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_direction.rb +3 -0
  81. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_key.rb +8 -1
  82. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/cast_method_name.rb +9 -5
  83. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/concern/instance_methods.rb +4 -0
  84. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/base.rb +4 -0
  85. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment/commands/define_method_in_container.rb +74 -0
  86. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment/commands.rb +3 -0
  87. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers/reassignment.rb +50 -0
  88. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/callers.rb +1 -0
  89. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/values/reassignment.rb +43 -0
  90. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities/values.rb +3 -0
  91. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/entities.rb +1 -0
  92. data/lib/convenient_service/service/plugins/has_result_steps/entities/method/errors.rb +22 -0
  93. data/lib/convenient_service/service/plugins/has_result_steps/entities/step/concern/instance_methods.rb +13 -2
  94. data/lib/convenient_service/support/arguments/null_arguments.rb +28 -0
  95. data/lib/convenient_service/support/arguments.rb +87 -0
  96. data/lib/convenient_service/support/dependency_container/commands/assert_valid_scope.rb +32 -0
  97. data/lib/convenient_service/support/dependency_container/commands/import_method.rb +13 -23
  98. data/lib/convenient_service/support/dependency_container/commands.rb +1 -0
  99. data/lib/convenient_service/support/dependency_container/constants.rb +4 -2
  100. data/lib/convenient_service/support/dependency_container/entities/method.rb +2 -9
  101. data/lib/convenient_service/support/dependency_container/entities/namespace_collection.rb +0 -9
  102. data/lib/convenient_service/support/dependency_container/errors.rb +25 -0
  103. data/lib/convenient_service/support/dependency_container/export.rb +8 -0
  104. data/lib/convenient_service/support/dependency_container/import.rb +3 -1
  105. data/lib/convenient_service/support/version/null_version.rb +7 -0
  106. data/lib/convenient_service/support/version.rb +11 -1
  107. data/lib/convenient_service/support.rb +1 -0
  108. data/lib/convenient_service/utils/object/instance_variable_delete.rb +41 -0
  109. data/lib/convenient_service/utils/object/instance_variable_fetch.rb +4 -0
  110. data/lib/convenient_service/utils/object.rb +9 -0
  111. data/lib/convenient_service/utils/proc/display.rb +43 -0
  112. data/lib/convenient_service/utils/proc.rb +5 -0
  113. data/lib/convenient_service/version.rb +1 -1
  114. metadata +101 -4
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module RSpec
5
+ module Matchers
6
+ module Custom
7
+ class DelegateTo
8
+ module Entities
9
+ class Matcher
10
+ module Entities
11
+ module Chainings
12
+ module SubMatchers
13
+ class Base
14
+ ##
15
+ # @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo]
16
+ #
17
+ attr_reader :matcher
18
+
19
+ ##
20
+ # @return [Object] Can be any type.
21
+ #
22
+ attr_reader :block_expectation_value
23
+
24
+ ##
25
+ # @overload initialize(matcher:)
26
+ # @param matcher [ConvenientService::RSpec::Matchers::Custom::DelegateTo]
27
+ # @return [void]
28
+ #
29
+ # @overload initialize(matcher:, block_expectation_value:)
30
+ # @param matcher [ConvenientService::RSpec::Matchers::Custom::DelegateTo]
31
+ # @param block_expectation_value [Object] Can be any type.
32
+ # @return [void]
33
+ # @api private
34
+ #
35
+ def initialize(matcher:, block_expectation_value: nil)
36
+ @matcher = matcher
37
+ @block_expectation_value = block_expectation_value
38
+ end
39
+
40
+ ##
41
+ # @return [void]
42
+ #
43
+ def apply_stubs!
44
+ end
45
+
46
+ ##
47
+ # @param block_expectation_value [Object] Can be any type.
48
+ # @return [Boolean]
49
+ #
50
+ def matches?(block_expectation_value)
51
+ @block_expectation_value = block_expectation_value
52
+
53
+ false
54
+ end
55
+
56
+ ##
57
+ # @param block_expectation_value [Object] Can be any type.
58
+ # @return [Boolean]
59
+ #
60
+ def does_not_match?(block_expectation_value)
61
+ !matches?(block_expectation_value)
62
+ end
63
+
64
+ ##
65
+ # @return [String]
66
+ #
67
+ def failure_message
68
+ ""
69
+ end
70
+
71
+ ##
72
+ # @return [String]
73
+ #
74
+ def failure_message_when_negated
75
+ ""
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,129 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module RSpec
5
+ module Matchers
6
+ module Custom
7
+ class DelegateTo
8
+ module Entities
9
+ class Matcher
10
+ module Entities
11
+ module Chainings
12
+ module SubMatchers
13
+ class ReturnItsValue < Chainings::SubMatchers::Base
14
+ ##
15
+ # @param block_expectation_value [Object]
16
+ # @return [Boolean]
17
+ #
18
+ def matches?(block_expectation_value)
19
+ super
20
+
21
+ ##
22
+ # TODO: `with_warmup`.
23
+ #
24
+ # IMPORTANT: `and_return_its_value` works only when `delegate_to` checks a pure function.
25
+ #
26
+ # For example (1):
27
+ # def require_dependencies_pure
28
+ # RequireDependenciesPure.call
29
+ # end
30
+ #
31
+ # class RequireDependenciesPure
32
+ # def self.call
33
+ # dependencies.require!
34
+ #
35
+ # true
36
+ # end
37
+ # end
38
+ #
39
+ # # Works since `RequireDependenciesPure.call` always returns `true`.
40
+ # specify do
41
+ # expect { require_dependencies_pure }
42
+ # .to delegate_to(RequireDependenciesPure, :call)
43
+ # .and_return_its_value
44
+ # end
45
+ #
46
+ # Example (2):
47
+ # def require_dependencies_not_pure
48
+ # RequireDependenciesNotPure.call
49
+ # end
50
+ #
51
+ # class RequireDependenciesNotPure
52
+ # def self.call
53
+ # return false if dependencies.required?
54
+ #
55
+ # dependencies.require!
56
+ #
57
+ # true
58
+ # end
59
+ # end
60
+ #
61
+ # # Does NOT work since `RequireDependenciesNotPure.call` returns `true` for the first time and `false` for the subsequent call.
62
+ # specify do
63
+ # expect { require_dependencies_not_pure }
64
+ # .to delegate_to(RequireDependenciesNotPure, :call)
65
+ # .and_return_its_value
66
+ # end
67
+ #
68
+ block_expectation_value == matcher.delegation_value
69
+ end
70
+
71
+ ##
72
+ # @return [String]
73
+ #
74
+ def failure_message
75
+ [failure_message_permanent_part, same_visual_output_note].reject(&:empty?).join("\n\n")
76
+ end
77
+
78
+ ##
79
+ # @return [String]
80
+ #
81
+ def failure_message_when_negated
82
+ "expected `#{matcher.printable_block_expectation}` NOT to delegate to `#{matcher.printable_method}` and return its value, but it did."
83
+ end
84
+
85
+ private
86
+
87
+ def failure_message_permanent_part
88
+ <<~MESSAGE.chomp
89
+ expected `#{matcher.printable_block_expectation}` to delegate to `#{matcher.printable_method}` and return its value, but it didn't.
90
+
91
+ `#{matcher.printable_block_expectation}` returns `#{block_expectation_value.inspect}`, but delegation returns `#{matcher.delegation_value.inspect}`.
92
+ MESSAGE
93
+ end
94
+
95
+ ##
96
+ # @internal
97
+ # NOTE: Early return is harder to understand in this particular case, that is why a casual if is used.
98
+ #
99
+ # def note
100
+ # return "" if block_expectation_value.inspect != matcher.delegation_value.inspect
101
+ # return "" if block_expectation_value == matcher.delegation_value
102
+ #
103
+ # # ...
104
+ # end
105
+ #
106
+ def same_visual_output_note
107
+ if block_expectation_value.inspect == matcher.delegation_value.inspect && block_expectation_value != matcher.delegation_value
108
+ <<~MESSAGE.chomp
109
+ NOTE: `#{block_expectation_value.inspect}` and `#{matcher.delegation_value.inspect}` have the same visual output, but they are different objects in terms of `#==`.
110
+
111
+ If it is expected behavior, ignore this note.
112
+
113
+ Otherwise, define a meaningful `#==` for `#{block_expectation_value.class}` or adjust its `#inspect` to generate different output.
114
+ MESSAGE
115
+ else
116
+ ""
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module RSpec
5
+ module Matchers
6
+ module Custom
7
+ class DelegateTo
8
+ module Entities
9
+ class Matcher
10
+ module Entities
11
+ module Chainings
12
+ module SubMatchers
13
+ class WithAnyArguments < Chainings::SubMatchers::Arguments
14
+ ##
15
+ # @return [Boolean]
16
+ #
17
+ def matches_arguments?(arguments)
18
+ true
19
+ end
20
+
21
+ ##
22
+ # @return [String]
23
+ #
24
+ def printable_expected_arguments
25
+ "with any arguments (no arguments is also valid)"
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module RSpec
5
+ module Matchers
6
+ module Custom
7
+ class DelegateTo
8
+ module Entities
9
+ class Matcher
10
+ module Entities
11
+ module Chainings
12
+ module SubMatchers
13
+ class WithConcreteArguments < Chainings::SubMatchers::Arguments
14
+ ##
15
+ # @return [Boolean]
16
+ #
17
+ def matches_arguments?(arguments)
18
+ Utils::Bool.to_bool(arguments == matcher.expected_arguments)
19
+ end
20
+
21
+ ##
22
+ # @return [String]
23
+ #
24
+ def printable_expected_arguments
25
+ "with `#{Arguments::Commands::GeneratePrintableArguments.call(arguments: matcher.expected_arguments)}`"
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module RSpec
5
+ module Matchers
6
+ module Custom
7
+ class DelegateTo
8
+ module Entities
9
+ class Matcher
10
+ module Entities
11
+ module Chainings
12
+ module SubMatchers
13
+ class WithoutArguments < Chainings::SubMatchers::Arguments
14
+ ##
15
+ # @return [Boolean]
16
+ #
17
+ def matches_arguments?(arguments)
18
+ arguments.none?
19
+ end
20
+
21
+ ##
22
+ # @return [String]
23
+ #
24
+ def printable_expected_arguments
25
+ "without arguments"
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module RSpec
5
+ module Matchers
6
+ module Custom
7
+ class DelegateTo
8
+ module Entities
9
+ class Matcher
10
+ module Entities
11
+ module Chainings
12
+ module Values
13
+ class Base
14
+ ##
15
+ # @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo]
16
+ #
17
+ attr_reader :matcher
18
+
19
+ ##
20
+ # @return [Object] Can be any type.
21
+ #
22
+ attr_reader :value
23
+
24
+ ##
25
+ # @param matcher [ConvenientService::RSpec::Matchers::Custom::DelegateTo]
26
+ # @return [void]
27
+ #
28
+ def initialize(matcher:)
29
+ @matcher = matcher
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module RSpec
5
+ module Matchers
6
+ module Custom
7
+ class DelegateTo
8
+ module Entities
9
+ class Matcher
10
+ module Entities
11
+ module Chainings
12
+ module Values
13
+ class WithCallingOriginal < Chainings::Values::Base
14
+ ##
15
+ # @return [Boolean]
16
+ #
17
+ def value
18
+ true
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module RSpec
5
+ module Matchers
6
+ module Custom
7
+ class DelegateTo
8
+ module Entities
9
+ class Matcher
10
+ module Entities
11
+ module Chainings
12
+ module Values
13
+ class WithoutCallingOriginal < Chainings::Values::Base
14
+ ##
15
+ # @return [Boolean]
16
+ #
17
+ def value
18
+ false
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "chainings/sub_matchers/base"
4
+ require_relative "chainings/sub_matchers/arguments"
5
+ require_relative "chainings/sub_matchers/with_any_arguments"
6
+ require_relative "chainings/sub_matchers/with_concrete_arguments"
7
+ require_relative "chainings/sub_matchers/without_arguments"
8
+ require_relative "chainings/sub_matchers/return_its_value"
9
+
10
+ require_relative "chainings/values/base"
11
+ require_relative "chainings/values/with_calling_original"
12
+ require_relative "chainings/values/without_calling_original"
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module RSpec
5
+ module Matchers
6
+ module Custom
7
+ class DelegateTo
8
+ module Entities
9
+ class Matcher
10
+ module Entities
11
+ class ChainingsCollection
12
+ module Errors
13
+ class CallOriginalChainingIsAlreadySet < ConvenientService::Error
14
+ def initialize
15
+ message = <<~TEXT
16
+ Call original chaining is already set.
17
+
18
+ Did you use `with_calling_original` or `without_calling_original` multiple times? Or a combination of them?
19
+ TEXT
20
+
21
+ super(message)
22
+ end
23
+ end
24
+
25
+ class ArgumentsChainingIsAlreadySet < ConvenientService::Error
26
+ def initialize
27
+ message = <<~TEXT
28
+ Arguments chaining is already set.
29
+
30
+ Did you use `with_arguments` or `without_arguments` multiple times? Or a combination of them?
31
+ TEXT
32
+
33
+ super(message)
34
+ end
35
+ end
36
+
37
+ class ReturnItsValueChainingIsAlreadySet < ConvenientService::Error
38
+ def initialize
39
+ message = <<~TEXT
40
+ Returns its value chaining is already set.
41
+
42
+ Did you use `and_returns_its_value` multiple times?
43
+ TEXT
44
+
45
+ super(message)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,171 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "chainings_collection/errors"
4
+
5
+ module ConvenientService
6
+ module RSpec
7
+ module Matchers
8
+ module Custom
9
+ class DelegateTo
10
+ module Entities
11
+ class Matcher
12
+ module Entities
13
+ class ChainingsCollection
14
+ include Support::Delegate
15
+
16
+ ##
17
+ # @!attribute [r] matcher
18
+ # @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Entities::Matcher::Entities::Chainings]
19
+ #
20
+ attr_reader :matcher
21
+
22
+ ##
23
+ # @!attribute [r] block_expectation_value
24
+ # @return [Object]
25
+ #
26
+ attr_reader :block_expectation_value
27
+
28
+ ##
29
+ # @!attribute [r] call_original
30
+ # @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Entities::Matcher::Entities::Chainings::Permissions:Base, nil]
31
+ #
32
+ attr_reader :call_original
33
+
34
+ ##
35
+ # @!attribute [r] arguments
36
+ # @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Entities::Matcher::Entities::Chainings::SubMatchers::Base, nil]
37
+ #
38
+ attr_reader :arguments
39
+
40
+ ##
41
+ # @!attribute [r] return_its_value
42
+ # @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Entities::Matcher::Entities::Chainings::SubMatchers::Base, nil]
43
+ #
44
+ attr_reader :return_its_value
45
+
46
+ ##
47
+ # @param matcher [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Entities::Matcher]
48
+ # @return [void]
49
+ #
50
+ def initialize(matcher:)
51
+ @matcher = matcher
52
+ end
53
+
54
+ ##
55
+ # @param block_expectation [Proc]
56
+ # @return [Boolean]
57
+ #
58
+ def sub_matchers_match?(block_expectation)
59
+ sub_matchers.each(&:apply_stubs!)
60
+
61
+ @block_expectation_value = block_expectation.call
62
+
63
+ sub_matchers.all? { |sub_matcher| sub_matcher.matches?(@block_expectation_value) }
64
+ end
65
+
66
+ ##
67
+ # @return [String]
68
+ #
69
+ def failure_message
70
+ sub_matchers
71
+ .lazy
72
+ .reject { |sub_matcher| sub_matcher.matches?(block_expectation_value) }
73
+ .first
74
+ &.failure_message
75
+ .to_s
76
+ end
77
+
78
+ ##
79
+ # @return [String]
80
+ #
81
+ def failure_message_when_negated
82
+ sub_matchers
83
+ .reject { |sub_matcher| sub_matcher.does_not_match?(block_expectation_value) }
84
+ .last
85
+ &.failure_message_when_negated
86
+ .to_s
87
+ end
88
+
89
+ ##
90
+ # @return [Array]
91
+ #
92
+ # @internal
93
+ # IMPORTANT: Order of sub matcher chainings matters.
94
+ #
95
+ def sub_matchers
96
+ [arguments, return_its_value].compact
97
+ end
98
+
99
+ ##
100
+ # @return [Array]
101
+ #
102
+ def values
103
+ [call_original].compact
104
+ end
105
+
106
+ ##
107
+ # @return [Boolean]
108
+ #
109
+ def should_call_original?
110
+ Utils::Bool.to_bool(call_original&.value)
111
+ end
112
+
113
+ ##
114
+ # @return [Boolean]
115
+ #
116
+ def has_call_original?
117
+ Utils::Bool.to_bool(call_original)
118
+ end
119
+
120
+ ##
121
+ # @return [Boolean]
122
+ #
123
+ def has_arguments?
124
+ Utils::Bool.to_bool(arguments)
125
+ end
126
+
127
+ ##
128
+ # @return [Boolean]
129
+ #
130
+ def has_return_its_value?
131
+ Utils::Bool.to_bool(return_its_value)
132
+ end
133
+
134
+ ##
135
+ # @param chaining [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Entities::Matcher::Entities::Chainings::Values::Base]
136
+ # @raise [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Errors::ReturnItsValueChainingIsAlreadySet]
137
+ #
138
+ def call_original=(chaining)
139
+ raise Errors::CallOriginalChainingIsAlreadySet.new if @call_original
140
+
141
+ @call_original = chaining
142
+ end
143
+
144
+ ##
145
+ # @param chaining [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Entities::Matcher::Entities::Chainings::SubMatchers::Base]
146
+ # @raise [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Errors::ArgumentsChainingIsAlreadySet]
147
+ #
148
+ def arguments=(chaining)
149
+ raise Errors::ArgumentsChainingIsAlreadySet.new if @arguments
150
+
151
+ @arguments = chaining
152
+ end
153
+
154
+ ##
155
+ # @param chaining [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Entities::Matcher::Entities::Chainings::SubMatchers::Base]
156
+ # @raise [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Errors::ReturnItsValueChainingIsAlreadySet]
157
+ #
158
+ def return_its_value=(chaining)
159
+ raise Errors::ReturnItsValueChainingIsAlreadySet.new if @return_its_value
160
+
161
+ @return_its_value = chaining
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end
170
+ end
171
+ end