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
@@ -1,291 +1,115 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "delegate_to/entities"
4
+
3
5
  ##
4
- # IMPORTANT: This matcher has a dedicated end-user doc. Do NOT forget to update it when needed.
5
- # https://github.com/marian13/convenient_service_docs/blob/main/docs/api/tests/rspec/matchers/delegate_to.mdx
6
- #
7
- # TODO: Refactor into composition:
8
- # - Ability to compose when `delegate_to` is used `without_arguments`.
9
- # - Ability to compose when `delegate_to` is used `with_arguments`.
10
- # - Ability to compose when `delegate_to` is used `and_return_its_value`.
11
- #
12
- # TODO: Refactor to NOT use `expect` inside this matcher.
13
- # This way the matcher will return true or false, but never raise exceptions (descendant of Exception, not StandardError).
14
- # Then it will be easier to developer a fully comprehensive spec suite for `delegate_to`.
6
+ # @internal
7
+ # IMPORTANT: This matcher has a dedicated end-user doc. Do NOT forget to update it when needed.
8
+ # https://github.com/marian13/convenient_service_docs/blob/main/docs/api/tests/rspec/matchers/delegate_to.mdx
15
9
  #
16
10
  module ConvenientService
17
11
  module RSpec
18
12
  module Matchers
19
13
  module Custom
20
14
  ##
21
- # specify {
22
- # expect { method_class.cast(other, **options) }
23
- # .to delegate_to(ConvenientService::Service::Plugins::HasResultSteps::Entities::Method::Commands::CastMethod, :call)
24
- # .with_arguments(other: other, options: options)
25
- # .and_return_its_value
26
- # }
27
- #
28
- # { method_class.cast(other, **options) }
29
- # # => block_expectation
30
- #
31
- # ConvenientService::Service::Plugins::HasResultSteps::Entities::Method::Commands::CastMethod
32
- # # => object
33
- #
34
- # :call
35
- # #=> method
36
- #
37
- # (other: other, options: options)
38
- # # => chain[:with]
39
- #
40
- # and_return_its_value
41
- # # => chain[:and_return_its_value]
42
- #
43
- # NOTE: A similar (with different behaviour) matcher exists in `saharspec`.
44
- # https://github.com/zverok/saharspec#send_messageobject-method-matcher
15
+ # @internal
16
+ # NOTE: This is a Facade class.
17
+ # https://refactoring.guru/design-patterns/facade
45
18
  #
46
19
  class DelegateTo
20
+ include Support::Delegate
21
+
47
22
  ##
48
- # NOTE: `include ::RSpec::Expectations`.
49
- # - https://github.com/rspec/rspec-expectations/blob/v3.11.0/lib/rspec/expectations.rb
50
- # - https://github.com/rspec/rspec-expectations/blob/main/lib/rspec/expectations.rb#L60
23
+ # @return [Boolean]
51
24
  #
52
- include ::RSpec::Expectations
25
+ delegate :matches?, to: :matcher
53
26
 
54
27
  ##
55
- # NOTE: `include ::RSpec::Matchers`.
56
- # - https://github.com/rspec/rspec-expectations/blob/v3.11.0/lib/rspec/matchers.rb
57
- # - https://github.com/rspec/rspec-expectations/blob/main/lib/rspec/matchers.rb
28
+ # @internal
29
+ # NOTE: Required by RSpec.
30
+ # https://relishapp.com/rspec/rspec-expectations/v/3-8/docs/custom-matchers/define-a-matcher-supporting-block-expectations
58
31
  #
59
- include ::RSpec::Matchers
32
+ delegate :supports_block_expectations?, to: :matcher
60
33
 
61
34
  ##
62
- # NOTE: `include ::RSpec::Mocks::ExampleMethods`.
63
- # - https://github.com/rspec/rspec-mocks/blob/v3.11.1/lib/rspec/mocks/example_methods.rb
64
- # - https://github.com/rspec/rspec-mocks/blob/main/lib/rspec/mocks/example_methods.rb
35
+ # @return [String]
65
36
  #
66
- include ::RSpec::Mocks::ExampleMethods
37
+ delegate :description, to: :matcher
67
38
 
68
39
  ##
40
+ # @return [String]
69
41
  #
70
- #
71
- def initialize(object, method)
72
- @object = object
73
- @method = method
74
- end
42
+ delegate :failure_message, to: :matcher
75
43
 
76
44
  ##
45
+ # @return [String]
77
46
  #
78
- #
79
- def matches?(block_expectation)
80
- @block_expectation = block_expectation
81
-
82
- ##
83
- # TODO: Support multiple `with_arguments` calls.
84
- #
85
- if used_with_arguments?
86
- ##
87
- # NOTE: RSpec `allow(object).to receive(method).with(*args, **kwargs)` does NOT support block.
88
- # https://github.com/rspec/rspec-mocks/issues/1182#issuecomment-679820352
89
- #
90
- # NOTE: RSpec `allow(object).to receive(method) do` does NOT support `and_call_original`.
91
- # https://github.com/rspec/rspec-mocks/issues/774#issuecomment-54245277
92
- #
93
- # NOTE: That is why `and_wrap_original` is used.
94
- # https://relishapp.com/rspec/rspec-mocks/docs/configuring-responses/wrapping-the-original-implementation
95
- #
96
- allow(object).to receive(method).and_wrap_original do |original, *actual_args, **actual_kwargs, &actual_block|
97
- actual_arguments_collection << [actual_args, actual_kwargs, actual_block]
98
-
99
- ##
100
- # NOTE: Imitates `and_call_original`.
101
- #
102
- original.call(*actual_args, **actual_kwargs, &actual_block)
103
- end
104
- else
105
- allow(object).to receive(method).and_call_original
106
- end
107
-
108
- ##
109
- # NOTE: Calls `block_expectation` before checking expections.
110
- #
111
- value = block_expectation.call
112
-
113
- ##
114
- # NOTE: If this expectation fails, it means `delegate_to` is NOT met.
115
- #
116
- expect(object).to have_received(method).at_least(1) unless used_with_arguments?
117
-
118
- ##
119
- # IMPORTANT: `and_return_its_value` works only when `delegate_to` checks a pure function.
120
- #
121
- # For example (1):
122
- # def require_dependencies_pure
123
- # RequireDependenciesPure.call
124
- # end
125
- #
126
- # class RequireDependenciesPure
127
- # def self.call
128
- # dependencies.require!
129
- #
130
- # true
131
- # end
132
- # end
133
- #
134
- # # Works since `RequireDependenciesPure.call` always returns `true`.
135
- # specify do
136
- # expect { require_dependencies_pure }
137
- # .to delegate_to(RequireDependenciesPure, :call)
138
- # .and_return_its_value
139
- # end
140
- #
141
- # Example (2):
142
- # def require_dependencies_not_pure
143
- # RequireDependenciesNotPure.call
144
- # end
145
- #
146
- # class RequireDependenciesNotPure
147
- # def self.call
148
- # return false if dependencies.required?
149
- #
150
- # dependencies.require!
151
- #
152
- # true
153
- # end
154
- # end
155
- #
156
- # # Does NOT work since `RequireDependenciesNotPure.call` returns `true` for the first time and `false` for the subsequent call.
157
- # specify do
158
- # expect { require_dependencies_not_pure }
159
- # .to delegate_to(RequireDependenciesNotPure, :call)
160
- # .and_return_its_value
161
- # end
162
- #
163
- # NOTE: If this expectation fails, it means `and_return_its_value` is NOT met.
164
- #
165
- expect(value).to eq(object.__send__(method, *args, **kwargs, &block)) if used_and_return_its_value?
166
-
167
- ##
168
- # IMPORTANT: A matcher should always return a boolean.
169
- # https://github.com/zverok/saharspec/blob/master/lib/saharspec/matchers/send_message.rb#L59
170
- #
171
- # NOTE: RSpec raises exception when any `expect` is NOT satisfied.
172
- # So, this `true` is returned only when all `expect` are successful.
173
- #
174
- if used_with_arguments?
175
- actual_arguments_collection.any? do |(actual_args, actual_kwargs, actual_block)|
176
- actual_args == expected_args && actual_kwargs == expected_kwargs && actual_block == expected_block
177
- end
178
- else
179
- true
180
- end
181
- end
47
+ delegate :failure_message_when_negated, to: :matcher
182
48
 
183
49
  ##
184
- # NOTE: Required by RSpec.
185
- # https://relishapp.com/rspec/rspec-expectations/v/3-8/docs/custom-matchers/define-a-matcher-supporting-block-expectations
50
+ # @overload initialize(object, method)
51
+ # @param object [Object]
52
+ # @param method [String, Symbol]
53
+ # @return [void]
186
54
  #
187
- def supports_block_expectations?
188
- true
55
+ # @overload initialize(matcher)
56
+ # @param matcher [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Entities::Matcher]
57
+ # @return [void]
58
+ # @api private
59
+ #
60
+ # @internal
61
+ # TODO: `overload do`?
62
+ #
63
+ def initialize(object = nil, method = nil, matcher: nil)
64
+ @matcher = matcher || Entities::Matcher.new(object, method)
189
65
  end
190
66
 
191
67
  ##
68
+ # @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo]
69
+ # @raise [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Errors::ArgumentsChainingIsAlreadySet]
192
70
  #
193
- #
194
- def description
195
- "delegate to `#{printable_method}`"
196
- end
71
+ def with_arguments(...)
72
+ matcher.with_arguments(...)
197
73
 
198
- def failure_message
199
- if used_with_arguments?
200
- "expected `#{printable_block_expectation}` to delegate to `#{printable_method}` with expected arguments at least once, but it didn't."
201
- else
202
- "expected `#{printable_block_expectation}` to delegate to `#{printable_method}` at least once, but it didn't."
203
- end
74
+ self
204
75
  end
205
76
 
206
77
  ##
207
- # IMPORTANT: `failure_message_when_negated` is NOT supported yet.
78
+ # @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo]
79
+ # @raise [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Errors::ArgumentsChainingIsAlreadySet]
208
80
  #
209
-
210
- def with_arguments(*args, **kwargs, &block)
211
- chain[:with_arguments] = {args: args, kwargs: kwargs, block: block}
81
+ def without_arguments
82
+ matcher.without_arguments
212
83
 
213
84
  self
214
85
  end
215
86
 
87
+ ##
88
+ # @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo]
89
+ # @raise [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Errors::ReturnItsValueChainingIsAlreadySet]
90
+ #
216
91
  def and_return_its_value
217
- chain[:and_return_its_value] = true
92
+ matcher.and_return_its_value
218
93
 
219
94
  self
220
95
  end
221
96
 
222
- def printable_method
223
- @printable_method ||=
224
- case Utils::Object.resolve_type(object)
225
- when "class", "module"
226
- "#{object}.#{method}"
227
- when "instance"
228
- "#{object.class}##{method}"
229
- end
230
- end
231
-
232
- private
233
-
234
- attr_reader :object, :method, :block_expectation
235
-
236
- def used_with_arguments?
237
- chain.key?(:with_arguments)
238
- end
239
-
240
- def used_and_return_its_value?
241
- chain.key?(:and_return_its_value)
242
- end
243
-
244
- def chain
245
- @chain ||= {}
246
- end
247
-
248
- def args
249
- @args ||= chain.dig(:with_arguments, :args) || []
250
- end
251
-
252
- alias_method :expected_args, :args
253
-
254
- def kwargs
255
- @kwargs ||= chain.dig(:with_arguments, :kwargs) || {}
256
- end
257
-
258
- alias_method :expected_kwargs, :kwargs
259
-
260
97
  ##
261
- # NOTE: `if defined?` is used in order to cache `nil` if needed.
98
+ # @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo]
262
99
  #
263
- def block
264
- return @block if defined? @block
100
+ def without_calling_original
101
+ matcher.without_calling_original
265
102
 
266
- @block = chain.dig(:with_arguments, :block)
103
+ self
267
104
  end
268
105
 
269
- alias_method :expected_block, :block
270
-
271
- def actual_arguments_collection
272
- @actual_arguments_collection ||= []
273
- end
106
+ private
274
107
 
275
108
  ##
276
- # NOTE: An example of how RSpec extracts block source, but they marked it as private.
277
- # https://github.com/rspec/rspec-expectations/blob/311aaf245f2c5493572bf683b8c441cb5f7e44c8/lib/rspec/matchers/built_in/change.rb#L437
109
+ # @!attribute [r] matcher
110
+ # @return [ConvenientService::RSpec::Matchers::Custom::DelegateTo::Entities::Matcher]
278
111
  #
279
- # TODO: `printable_block_expectation` when `method_source` is available.
280
- # https://github.com/banister/method_source
281
- #
282
- # def printable_block_expectation
283
- # @printable_block_expectation ||= block_expectation.source
284
- # end
285
- #
286
- def printable_block_expectation
287
- @printable_block_expectation ||= "{ ... }"
288
- end
112
+ attr_reader :matcher
289
113
  end
290
114
  end
291
115
  end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module RSpec
5
+ module Matchers
6
+ module Custom
7
+ ##
8
+ # @internal
9
+ # class User
10
+ # include InstanceMethods
11
+ #
12
+ # singleton_class.include ClassMethods # or more common `extend ClassMethods`.
13
+ #
14
+ # prepend InstanceMethods
15
+ #
16
+ # singleton_class.prepend ClassMethods # no short form like `extend`.
17
+ # end
18
+ #
19
+ class SingletonPrependModule
20
+ ##
21
+ # @param mod [Module]
22
+ # @return [void]
23
+ #
24
+ # @internal
25
+ # NOTE: `mod` since `module` is a keyword.
26
+ #
27
+ def initialize(mod)
28
+ @mod = mod
29
+ end
30
+
31
+ ##
32
+ # @param klass [Class, Module]
33
+ # @return [Boolean]
34
+ #
35
+ def matches?(klass)
36
+ @klass = klass
37
+
38
+ klass.singleton_class.ancestors.take_while { |ancestor| ancestor != klass }.include?(mod)
39
+ end
40
+
41
+ ##
42
+ # @return [String]
43
+ #
44
+ def description
45
+ "`singleton_class.prepend` module `#{mod}`"
46
+ end
47
+
48
+ ##
49
+ # @return [String]
50
+ #
51
+ def failure_message
52
+ "expected `#{klass}` to `singleton_class.prepend` module `#{mod}`"
53
+ end
54
+
55
+ ##
56
+ # @return [String]
57
+ #
58
+ def failure_message_when_negated
59
+ "expected `#{klass}` NOT to `singleton_class.prepend` module `#{mod}`"
60
+ end
61
+
62
+ private
63
+
64
+ ##
65
+ # @!attribute [r] klass
66
+ # @return [Class, Module]
67
+ #
68
+ attr_reader :klass
69
+
70
+ ##
71
+ # @!attribute [r] mod
72
+ # @return [Module]
73
+ #
74
+ attr_reader :mod
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -14,3 +14,4 @@ require_relative "custom/have_attr_writer"
14
14
  require_relative "custom/include_module"
15
15
  require_relative "custom/prepend_module"
16
16
  require_relative "custom/results"
17
+ require_relative "custom/singleton_prepend_module"
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConvenientService
4
+ module RSpec
5
+ module Matchers
6
+ module SingletonPrependModule
7
+ def singleton_prepend_module(...)
8
+ Custom::SingletonPrependModule.new(...)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -16,6 +16,7 @@ require_relative "matchers/have_attr_writer"
16
16
  require_relative "matchers/include_module"
17
17
  require_relative "matchers/prepend_module"
18
18
  require_relative "matchers/results"
19
+ require_relative "matchers/singleton_prepend_module"
19
20
 
20
21
  module ConvenientService
21
22
  module RSpec
@@ -37,6 +38,7 @@ module ConvenientService
37
38
  include Matchers::IncludeModule
38
39
  include Matchers::PrependModule
39
40
  include Matchers::Results
41
+ include Matchers::SingletonPrependModule
40
42
  end
41
43
  end
42
44
  end
@@ -8,6 +8,9 @@ module ConvenientService
8
8
  include Support::Concern
9
9
 
10
10
  instance_methods do
11
+ ##
12
+ # @return [Array]
13
+ #
11
14
  def steps
12
15
  internals.cache.fetch(:steps) do
13
16
  self.class
@@ -27,6 +30,11 @@ module ConvenientService
27
30
  end
28
31
 
29
32
  class_methods do
33
+ ##
34
+ # @params args [Array]
35
+ # @params kwargs [Hash]
36
+ # @return [ConvenientService::Service::Plugins::HasResultSteps::Entities::Step]
37
+ #
30
38
  def step(*args, **kwargs)
31
39
  step_class.new(*args, **kwargs.merge(container: self))
32
40
  .tap { |step_instance| steps << step_instance }
@@ -41,14 +49,31 @@ module ConvenientService
41
49
  # # that is converted to the following service invocation:
42
50
  # AssertFeatureEnabled.result(name: :chat_v2)
43
51
  #
52
+ # @param value [Object] Can be any type.
53
+ # @return [ConvenientService::Support::RawValue]
54
+ #
44
55
  def raw(value)
45
56
  Support::RawValue.wrap(value)
46
57
  end
47
58
 
59
+ ##
60
+ # @param method_name [String, Symbol]
61
+ # @return [ConvenientService::Service::Plugins::HasResultSteps::Entities::Method::Entities::Values::Reassignment]
62
+ #
63
+ def reassign(method_name)
64
+ Entities::Method::Entities::Values::Reassignment.new(method_name)
65
+ end
66
+
67
+ ##
68
+ # @return [ConvenientService::Service::Plugins::HasResultSteps::Entities::StepCollection]
69
+ #
48
70
  def steps
49
71
  @steps ||= Entities::StepCollection.new
50
72
  end
51
73
 
74
+ ##
75
+ # @return [ConvenientService::Service::Plugins::HasResultSteps::Entities::Step]
76
+ #
52
77
  def step_class
53
78
  @step_class ||= Commands::CreateStepClass.call(service_class: self)
54
79
  end
@@ -7,6 +7,9 @@ module ConvenientService
7
7
  module Entities
8
8
  class Method
9
9
  module Commands
10
+ ##
11
+ # TODO: Abstract factory.
12
+ #
10
13
  class CastMethod < Support::Command
11
14
  attr_reader :other, :options
12
15
 
@@ -8,7 +8,7 @@ module ConvenientService
8
8
  class Method
9
9
  module Commands
10
10
  ##
11
- # TODO: Replace `CastMethodKey`, `CastMethodName`, `CastMethodCaller` by declarative caster?
11
+ # TODO: Abstract factory.
12
12
  #
13
13
  class CastMethodCaller < Support::Command
14
14
  attr_reader :other, :options
@@ -23,6 +23,7 @@ module ConvenientService
23
23
  when ::Symbol then cast_symbol
24
24
  when ::String then cast_string
25
25
  when ::Hash then cast_hash
26
+ when Entities::Values::Reassignment then cast_reassignment
26
27
  when Method then cast_method
27
28
  end
28
29
  end
@@ -37,6 +38,10 @@ module ConvenientService
37
38
  Entities::Callers::Usual.new(other)
38
39
  end
39
40
 
41
+ def cast_reassignment
42
+ Entities::Callers::Reassignment.new(other)
43
+ end
44
+
40
45
  def cast_hash
41
46
  return unless other.keys.one?
42
47
 
@@ -51,6 +56,8 @@ module ConvenientService
51
56
  Entities::Callers::Proc.new(value)
52
57
  when Support::RawValue
53
58
  Entities::Callers::Raw.new(value)
59
+ when Entities::Values::Reassignment
60
+ Entities::Callers::Reassignment.new(value)
54
61
  end
55
62
  end
56
63
 
@@ -7,6 +7,9 @@ module ConvenientService
7
7
  module Entities
8
8
  class Method
9
9
  module Commands
10
+ ##
11
+ # TODO: Abstract factory.
12
+ #
10
13
  class CastMethodDirection < Support::Command
11
14
  attr_reader :other, :options
12
15
 
@@ -8,7 +8,7 @@ module ConvenientService
8
8
  class Method
9
9
  module Commands
10
10
  ##
11
- # TODO: Replace `CastMethodKey`, `CastMethodName`, `CastMethodCaller` by declarative caster?
11
+ # TODO: Abstract factory.
12
12
  #
13
13
  class CastMethodKey < Support::Command
14
14
  attr_reader :other, :options
@@ -26,6 +26,7 @@ module ConvenientService
26
26
  when ::Symbol then cast_symbol
27
27
  when ::String then cast_string
28
28
  when ::Hash then cast_hash
29
+ when Entities::Values::Reassignment then cast_reassignment
29
30
  when Method then cast_method
30
31
  end
31
32
  end
@@ -40,6 +41,10 @@ module ConvenientService
40
41
  Entities::Key.new(other)
41
42
  end
42
43
 
44
+ def cast_reassignment
45
+ Entities::Key.new(other.to_sym)
46
+ end
47
+
43
48
  def cast_hash
44
49
  return unless other.keys.one?
45
50
 
@@ -55,6 +60,8 @@ module ConvenientService
55
60
  Entities::Key.new(key)
56
61
  when Support::RawValue
57
62
  Entities::Key.new(key)
63
+ when Entities::Values::Reassignment
64
+ Entities::Key.new(key)
58
65
  end
59
66
  end
60
67
 
@@ -7,8 +7,8 @@ module ConvenientService
7
7
  module Entities
8
8
  class Method
9
9
  module Commands
10
- ##
11
- # TODO: Replace `CastMethodKey`, `CastMethodName`, `CastMethodCaller` by declarative caster?
10
+ #
11
+ # TODO: Abstract factory.
12
12
  #
13
13
  class CastMethodName < Support::Command
14
14
  attr_reader :other, :options
@@ -26,6 +26,7 @@ module ConvenientService
26
26
  when ::Symbol then cast_symbol
27
27
  when ::String then cast_string
28
28
  when ::Hash then cast_hash
29
+ when Entities::Values::Reassignment then cast_reassignment
29
30
  when Method then cast_method
30
31
  end
31
32
  end
@@ -40,6 +41,10 @@ module ConvenientService
40
41
  Entities::Name.new(other)
41
42
  end
42
43
 
44
+ def cast_reassignment
45
+ Entities::Name.new(other.to_sym)
46
+ end
47
+
43
48
  def cast_hash
44
49
  return unless other.keys.one?
45
50
 
@@ -55,12 +60,11 @@ module ConvenientService
55
60
  Entities::Name.new(key)
56
61
  when Support::RawValue
57
62
  Entities::Name.new(key)
63
+ when Entities::Values::Reassignment
64
+ Entities::Name.new(value.to_sym)
58
65
  end
59
66
  end
60
67
 
61
- ##
62
- # TODO: Specs.
63
- #
64
68
  def cast_method
65
69
  other.name.copy
66
70
  end
@@ -38,6 +38,10 @@ module ConvenientService
38
38
  Utils::Bool.to_bool(organizer)
39
39
  end
40
40
 
41
+ def reassignment?(name)
42
+ caller.reassignment?(name)
43
+ end
44
+
41
45
  def validate_as_input_for_container!(container)
42
46
  direction.validate_as_input_for_container!(container, method: self)
43
47
 
@@ -27,6 +27,10 @@ module ConvenientService
27
27
  @object = object
28
28
  end
29
29
 
30
+ def reassignment?(name)
31
+ false
32
+ end
33
+
30
34
  def ==(other)
31
35
  return unless other.instance_of?(self.class)
32
36