convenient_service 0.20.0 → 0.21.1

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 (121) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +8 -4
  3. data/lib/convenient_service/aliases.rb +113 -1
  4. data/lib/convenient_service/common/plugins/aliases.rb +69 -0
  5. data/lib/convenient_service/common/plugins/can_have_callbacks/entities/type_collection.rb +2 -2
  6. data/lib/convenient_service/common/plugins/has_instance_proxy/entities/instance_proxy.rb +8 -8
  7. data/lib/convenient_service/common.rb +14 -0
  8. data/lib/convenient_service/config.rb +8 -0
  9. data/lib/convenient_service/core/aliases.rb +84 -0
  10. data/lib/convenient_service/dependencies/built_in.rb +15 -1
  11. data/lib/convenient_service/dependencies/only_queries.rb +0 -6
  12. data/lib/convenient_service/dependencies/queries/gems/minitest.rb +55 -0
  13. data/lib/convenient_service/dependencies/queries/gems/rspec.rb +1 -1
  14. data/lib/convenient_service/dependencies/queries/gems.rb +2 -0
  15. data/lib/convenient_service/dependencies/queries/ruby.rb +2 -2
  16. data/lib/convenient_service/dependencies/queries.rb +67 -16
  17. data/lib/convenient_service/dependencies.rb +6 -6
  18. data/lib/convenient_service/extras/alias.rb +4 -1
  19. data/lib/convenient_service/feature/configs/standard.rb +34 -13
  20. data/lib/convenient_service/feature/core.rb +41 -0
  21. data/lib/convenient_service/feature/plugins/aliases.rb +29 -0
  22. data/lib/convenient_service/feature/plugins/can_have_rspec_stubbed_entries/concern.rb +34 -0
  23. data/lib/convenient_service/{rspec/helpers/classes/stub_entry/entities.rb → feature/plugins/can_have_rspec_stubbed_entries.rb} +1 -2
  24. data/lib/convenient_service/feature/plugins/can_have_stubbed_entries/commands/delete_feature_stubbed_entry.rb +71 -0
  25. data/lib/convenient_service/feature/plugins/can_have_stubbed_entries/commands/fetch_all_features_stubbed_entries_cache.rb +16 -2
  26. data/lib/convenient_service/feature/plugins/can_have_stubbed_entries/commands/fetch_feature_stubbed_entries_cache.rb +1 -1
  27. data/lib/convenient_service/feature/plugins/can_have_stubbed_entries/commands.rb +2 -0
  28. data/lib/convenient_service/feature/plugins/can_have_stubbed_entries/concern.rb +23 -0
  29. data/lib/convenient_service/feature/plugins/can_have_stubbed_entries/entities/feature_stub.rb +123 -0
  30. data/lib/convenient_service/feature/plugins/can_have_stubbed_entries/entities/feature_unstub.rb +122 -0
  31. data/lib/convenient_service/feature/plugins/can_have_stubbed_entries/entities/value_mock.rb +118 -0
  32. data/lib/convenient_service/feature/plugins/can_have_stubbed_entries/entities/value_unmock.rb +88 -0
  33. data/lib/convenient_service/feature/plugins/can_have_stubbed_entries/entities.rb +12 -0
  34. data/lib/convenient_service/feature/plugins/can_have_stubbed_entries.rb +1 -14
  35. data/lib/convenient_service/feature/plugins/has_amazing_print_inspect/concern.rb +34 -0
  36. data/lib/convenient_service/feature/{configs/standard/commands.rb → plugins/has_amazing_print_inspect.rb} +1 -2
  37. data/lib/convenient_service/feature/plugins/has_awesome_print_inspect/concern.rb +34 -0
  38. data/lib/convenient_service/{rspec/helpers/classes/stub_service/entities.rb → feature/plugins/has_awesome_print_inspect.rb} +1 -2
  39. data/lib/convenient_service/feature/plugins/has_inspect/concern.rb +27 -0
  40. data/lib/convenient_service/{service/configs/standard/commands.rb → feature/plugins/has_inspect.rb} +1 -2
  41. data/lib/convenient_service/feature/plugins.rb +1 -0
  42. data/lib/convenient_service/feature.rb +16 -0
  43. data/lib/convenient_service/logger.rb +11 -12
  44. data/lib/convenient_service/rspec/helpers/classes.rb +0 -2
  45. data/lib/convenient_service/rspec/helpers/stub_entry.rb +23 -5
  46. data/lib/convenient_service/rspec/helpers/stub_service.rb +79 -11
  47. data/lib/convenient_service/rspec/matchers/classes/cache_its_value.rb +2 -1
  48. data/lib/convenient_service/rspec/matchers/classes/delegate_to.rb +3 -5
  49. data/lib/convenient_service/rspec/matchers/classes/include_config.rb +4 -0
  50. data/lib/convenient_service/service/configs/aliases.rb +42 -0
  51. data/lib/convenient_service/service/configs/standard/aliases.rb +24 -0
  52. data/lib/convenient_service/service/configs/standard/v1.rb +17 -0
  53. data/lib/convenient_service/service/configs/standard.rb +50 -25
  54. data/lib/convenient_service/service/core.rb +39 -0
  55. data/lib/convenient_service/service/plugins/aliases.rb +48 -0
  56. data/lib/convenient_service/service/plugins/can_have_rollbacks/middleware.rb +33 -2
  57. data/lib/convenient_service/service/plugins/can_have_rspec_stubbed_results/concern.rb +34 -0
  58. data/lib/convenient_service/service/plugins/can_have_rspec_stubbed_results.rb +8 -0
  59. data/lib/convenient_service/service/plugins/can_have_stubbed_results/commands/delete_service_stubbed_result.rb +63 -0
  60. data/lib/convenient_service/service/plugins/can_have_stubbed_results/commands/fetch_all_services_stubbed_results_cache.rb +15 -10
  61. data/lib/convenient_service/service/plugins/can_have_stubbed_results/commands/fetch_service_stubbed_results_cache.rb +3 -3
  62. data/lib/convenient_service/service/plugins/can_have_stubbed_results/commands.rb +2 -0
  63. data/lib/convenient_service/service/plugins/can_have_stubbed_results/concern.rb +25 -0
  64. data/lib/convenient_service/service/plugins/can_have_stubbed_results/entities/result_mock.rb +254 -0
  65. data/lib/convenient_service/service/plugins/can_have_stubbed_results/entities/result_unmock.rb +82 -0
  66. data/lib/convenient_service/service/plugins/can_have_stubbed_results/entities/service_stub.rb +135 -0
  67. data/lib/convenient_service/service/plugins/can_have_stubbed_results/entities/service_unstub.rb +113 -0
  68. data/lib/convenient_service/service/plugins/can_have_stubbed_results/entities.rb +12 -0
  69. data/lib/convenient_service/service/plugins/can_have_stubbed_results.rb +1 -14
  70. data/lib/convenient_service/service/plugins/forbids_convenient_service_entities_as_constructor_arguments/exceptions.rb +84 -0
  71. data/lib/convenient_service/service/plugins/forbids_convenient_service_entities_as_constructor_arguments/middleware.rb +84 -32
  72. data/lib/convenient_service/service/plugins/has_j_send_result/entities/result/concern.rb +42 -2
  73. data/lib/convenient_service/service/plugins/has_j_send_result/entities/result/plugins/can_be_called/exceptions.rb +3 -2
  74. data/lib/convenient_service/service/plugins/has_j_send_result/entities/result/plugins/has_j_send_status_and_attributes/entities/status/concern/instance_methods.rb +5 -0
  75. data/lib/convenient_service/service.rb +16 -0
  76. data/lib/convenient_service/specification.rb +42 -12
  77. data/lib/convenient_service/support/backtrace_cleaner.rb +6 -7
  78. data/lib/convenient_service/support/cache/entities/caches/array.rb +5 -1
  79. data/lib/convenient_service/support/cache/entities/caches/hash.rb +5 -1
  80. data/lib/convenient_service/support/middleware/stack_builder/entities/builders/custom.rb +4 -0
  81. data/lib/convenient_service/support/middleware/stack_builder/entities/builders/ruby_middleware.rb +4 -0
  82. data/lib/convenient_service/support/unique_value.rb +4 -1
  83. data/lib/convenient_service/support.rb +13 -0
  84. data/lib/convenient_service/utils/array/find_last.rb +18 -9
  85. data/lib/convenient_service/utils/class/display_name.rb +9 -9
  86. data/lib/convenient_service/utils/enumerable/find_last.rb +48 -0
  87. data/lib/convenient_service/utils/enumerable.rb +20 -0
  88. data/lib/convenient_service/utils/hash/assert_valid_keys.rb +4 -4
  89. data/lib/convenient_service/utils/hash/except.rb +5 -4
  90. data/lib/convenient_service/utils/hash/triple_equality_compare.rb +8 -8
  91. data/lib/convenient_service/utils/kernel/silence_warnings.rb +4 -4
  92. data/lib/convenient_service/utils/module/fetch_own_const.rb +36 -31
  93. data/lib/convenient_service/utils/module/get_namespace.rb +26 -21
  94. data/lib/convenient_service/utils/module/get_own_const.rb +21 -16
  95. data/lib/convenient_service/utils/object/clamp_class.rb +50 -47
  96. data/lib/convenient_service/utils/object/duck_class.rb +84 -81
  97. data/lib/convenient_service/utils/object/get_own_method.rb +1 -1
  98. data/lib/convenient_service/utils/object/instance_variable_delete.rb +0 -3
  99. data/lib/convenient_service/utils/object/instance_variable_fetch.rb +0 -3
  100. data/lib/convenient_service/utils/object/memoize_including_falsy_values.rb +0 -3
  101. data/lib/convenient_service/utils/object/resolve_type.rb +13 -11
  102. data/lib/convenient_service/utils/object/safe_send.rb +2 -0
  103. data/lib/convenient_service/utils/string/enclose.rb +11 -11
  104. data/lib/convenient_service/utils/string/tab.rb +60 -0
  105. data/lib/convenient_service/utils/string.rb +5 -0
  106. data/lib/convenient_service/utils.rb +9 -0
  107. data/lib/convenient_service/version.rb +8 -1
  108. data/lib/convenient_service.rb +232 -14
  109. metadata +37 -754
  110. data/lib/convenient_service/feature/configs/standard/commands/is_feature.rb +0 -39
  111. data/lib/convenient_service/feature/configs/standard/commands/is_feature_class.rb +0 -41
  112. data/lib/convenient_service/rspec/helpers/classes/stub_entry/constants.rb +0 -25
  113. data/lib/convenient_service/rspec/helpers/classes/stub_entry/entities/stubbed_feature.rb +0 -128
  114. data/lib/convenient_service/rspec/helpers/classes/stub_entry/entities/value_spec.rb +0 -79
  115. data/lib/convenient_service/rspec/helpers/classes/stub_entry.rb +0 -48
  116. data/lib/convenient_service/rspec/helpers/classes/stub_service/constants.rb +0 -25
  117. data/lib/convenient_service/rspec/helpers/classes/stub_service/entities/result_spec.rb +0 -211
  118. data/lib/convenient_service/rspec/helpers/classes/stub_service/entities/stubbed_service.rb +0 -119
  119. data/lib/convenient_service/rspec/helpers/classes/stub_service.rb +0 -43
  120. data/lib/convenient_service/service/configs/standard/commands/is_service.rb +0 -39
  121. data/lib/convenient_service/service/configs/standard/commands/is_service_class.rb +0 -41
@@ -57,32 +57,212 @@ require_relative "convenient_service/feature"
57
57
  #
58
58
  require_relative "convenient_service/aliases"
59
59
 
60
+ ##
61
+ # Convenient Service module/namespace.
62
+ #
63
+ # @api public
64
+ # @since 1.0.0
65
+ # @note This module is NOT expected to be included or extended by the end-user classes and modules. See {ConvenientService::Standard::Config} as the main entrypoint.
66
+ #
67
+ # @example How to load `CS` - Convenient Service alias?
68
+ # require "convenient_service/extras/alias"
69
+ #
70
+ # class Service
71
+ # include CS::Standard::Config
72
+ #
73
+ # def result
74
+ # success
75
+ # end
76
+ # end
77
+ #
78
+ # @example How to load RSpec extensions?
79
+ # ##
80
+ # # RSpec extensions expected to be required from RSpec entry points like `spec_helper.rb`.
81
+ # #
82
+ # require "convenient_service/extras/rspec"
83
+ #
84
+ # ##
85
+ # # In some spec file like `service_spec.rb`
86
+ # #
87
+ # RSpec.describe Service do
88
+ # include ConvenientService::RSpec::Matchers::Results
89
+ #
90
+ # it "returns success" do
91
+ # expect(Service.result).to be_success
92
+ # end
93
+ # end
94
+ #
95
+ # ##
96
+ # # In some spec file like `other_service_spec.rb`
97
+ # #
98
+ # RSpec.describe OtherService do
99
+ # include ConvenientService::RSpec::Helpers::StubService
100
+ #
101
+ # before do
102
+ # stub_service(Service).to return_success.with_data(foo: :bar)
103
+ # end
104
+ #
105
+ # # ...
106
+ # end
107
+ #
108
+ # @example How to load `active_model_validations` standard config option?
109
+ # require "convenient_service/extras/standard/config/options/active_model_validations"
110
+ #
111
+ # class Service
112
+ # include ConvenientService::Standard::Config.with(:active_model_validations)
113
+ #
114
+ # attr_reader :foo
115
+ #
116
+ # validates :foo, presence: true
117
+ #
118
+ # def initialize(foo:)
119
+ # @foo = foo
120
+ # end
121
+ #
122
+ # def result
123
+ # success
124
+ # end
125
+ # end
126
+ #
127
+ # Service.result(foo: nil)
128
+ # # => <Service::Result status: :error, data_keys: [:foo], message: "foo can't be blank">
129
+ #
130
+ # Service.result(foo: :bar)
131
+ # # => <Service::Result status: :success>
132
+ #
133
+ # @example How to load `amazing_print_inspect` standard config option?
134
+ # require "convenient_service/extras/standard/config/options/amazing_print_inspect"
135
+ #
136
+ # class Service
137
+ # include ConvenientService::Standard::Config.with(:amazing_print_inspect)
138
+ #
139
+ # def result
140
+ # success
141
+ # end
142
+ # end
143
+ #
144
+ # ap Service.result
145
+ # # {
146
+ # # :ConvenientService => {
147
+ # # :entity => "Result",
148
+ # # :service => "Service",
149
+ # # :status => :success
150
+ # # }
151
+ #
152
+ # @example How to load `awesome_print_inspect` standard config option?
153
+ # require "convenient_service/extras/standard/config/options/awesome_print_inspect"
154
+ #
155
+ # class Service
156
+ # include ConvenientService::Standard::Config.with(:awesome_print_inspect)
157
+ #
158
+ # def result
159
+ # success
160
+ # end
161
+ # end
162
+ #
163
+ # ap Service.result
164
+ # # {
165
+ # # :ConvenientService => {
166
+ # # :entity => "Result",
167
+ # # :service => "Service",
168
+ # # :status => :success
169
+ # # }
170
+ #
171
+ # @example How to load `dry_initializer` standard config option?
172
+ # require "convenient_service/extras/standard/config/options/dry_initializer"
173
+ #
174
+ # class Service
175
+ # include ConvenientService::Standard::Config.with(:dry_initializer)
176
+ #
177
+ # option :foo
178
+ #
179
+ # def result
180
+ # success
181
+ # end
182
+ # end
183
+ #
184
+ # @example How to load `memo_wise` standard config option?
185
+ # require "convenient_service/extras/standard/config/options/memo_wise"
186
+ #
187
+ # class Service
188
+ # include ConvenientService::Standard::Config.with(:memo_wise)
189
+ #
190
+ # def result
191
+ # success(foo: foo, bar: bar)
192
+ # end
193
+ #
194
+ # private
195
+ #
196
+ # memo_wise \
197
+ # def foo
198
+ # :foo
199
+ # end
200
+ #
201
+ # memo_wise \
202
+ # def bar
203
+ # :bar
204
+ # end
205
+ # end
206
+ #
60
207
  module ConvenientService
61
208
  class << self
62
209
  ##
63
- # @api private
210
+ # Returns `true` when Convenient Service is in debug mode. In other words `$CONVENIENT_SERVICE_DEBUG` env variable is set to `true`.
64
211
  #
212
+ # @api private
213
+ # @since 1.0.0
65
214
  # @return [Boolean]
66
215
  #
67
216
  def debug?
68
- ::ENV["CONVENIENT_SERVICE_DEBUG"] == "true"
217
+ Dependencies.debug?
69
218
  end
70
219
 
71
220
  ##
72
- # @api public
221
+ # Returns `true` when Convenient Service is in benchmark mode. In other words `$CONVENIENT_SERVICE_BENCHMARK` env variable is set to `true`.
222
+ #
223
+ # @api private
224
+ # @since 1.0.0
225
+ # @return [Boolean]
226
+ #
227
+ def benchmark?
228
+ Dependencies.benchmark?
229
+ end
230
+
231
+ ##
232
+ # Returns `true` when Convenient Service is in CI mode. In other words `$CONVENIENT_SERVICE_CI` env variable is set to `true`.
73
233
  #
234
+ # @api private
235
+ # @since 1.0.0
236
+ # @return [Boolean]
237
+ #
238
+ def ci?
239
+ Dependencies.ci?
240
+ end
241
+
242
+ ##
243
+ # Returns Convenient Service internal logger, that is an instance of Ruby stdlib logger.
244
+ # Useful for debugging Convenient Service internals.
245
+ #
246
+ # @api public
247
+ # @since 1.0.0
74
248
  # @return [ConvenientService::Logger]
75
249
  #
250
+ # @see https://github.com/ruby/logger
251
+ # @see https://ruby-doc.org/stdlib-2.7.0/libdoc/logger/rdoc/Logger.html
252
+ #
253
+ # @example Set log level.
254
+ # ConvenientService.logger.level = Logger::DEBUG
255
+ #
76
256
  def logger
77
257
  Logger.instance
78
258
  end
79
259
 
80
260
  ##
81
261
  # Returns Convenient Service root folder. Inspired by `Rails.root`.
82
- # For example, it may return something like: `/Users/user/.asdf/installs/ruby/2.7.0/lib/ruby/gems/2.7.0/gems/convenient_service-0.16.0`.
262
+ # For example, it may return something like: `/Users/marian/.asdf/installs/ruby/2.7.0/lib/ruby/gems/2.7.0/gems/convenient_service-1.0.0`.
83
263
  #
84
264
  # @api public
85
- #
265
+ # @since 1.0.0
86
266
  # @return [Pathname]
87
267
  #
88
268
  # @see https://ruby-doc.org/core-2.7.1/Kernel.html#method-i-__dir__
@@ -94,10 +274,10 @@ module ConvenientService
94
274
 
95
275
  ##
96
276
  # Returns Convenient Service lib folder.
97
- # For example, it may return something like: `/Users/user/.asdf/installs/ruby/2.7.0/lib/ruby/gems/2.7.0/gems/convenient_service-0.16.0/lib`.
277
+ # For example, it may return something like: `/Users/marian/.asdf/installs/ruby/2.7.0/lib/ruby/gems/2.7.0/gems/convenient_service-1.0.0/lib`.
98
278
  #
99
279
  # @api private
100
- #
280
+ # @since 1.0.0
101
281
  # @return [Pathname]
102
282
  #
103
283
  def lib_root
@@ -105,11 +285,11 @@ module ConvenientService
105
285
  end
106
286
 
107
287
  ##
108
- # Returns Convenient Service Examples folder.
109
- # For example, it may return something like: `/Users/user/.asdf/installs/ruby/2.7.0/lib/ruby/gems/2.7.0/gems/convenient_service-0.16.0/lib/convenient_service/examples`.
288
+ # Returns Convenient Service examples folder.
289
+ # For example, it may return something like: `/Users/marian/.asdf/installs/ruby/2.7.0/lib/ruby/gems/2.7.0/gems/convenient_service-1.0.0/lib/convenient_service/examples`.
110
290
  #
111
291
  # @api private
112
- #
292
+ # @since 1.0.0
113
293
  # @return [Pathname]
114
294
  #
115
295
  def examples_root
@@ -117,11 +297,11 @@ module ConvenientService
117
297
  end
118
298
 
119
299
  ##
120
- # Returns Convenient Service Specs folder.
121
- # For example, it may return something like: `/Users/user/.asdf/installs/ruby/2.7.0/lib/ruby/gems/2.7.0/gems/convenient_service-0.16.0/spec`.
300
+ # Returns Convenient Service specs folder.
301
+ # For example, it may return something like: `/Users/marian/.asdf/installs/ruby/2.7.0/lib/ruby/gems/2.7.0/gems/convenient_service-1.0.0/spec`.
122
302
  #
123
303
  # @api private
124
- #
304
+ # @since 1.0.0
125
305
  # @return [Pathname]
126
306
  #
127
307
  def spec_root
@@ -129,19 +309,51 @@ module ConvenientService
129
309
  end
130
310
 
131
311
  ##
132
- # @api public
312
+ # Returns Convenient Service backtrace cleaner (has similar interface to Rails v8.0.2 backtrace cleaner).
313
+ # Useful for debugging Convenient Service internals.
133
314
  #
315
+ # @api public
316
+ # @since 1.0.0
134
317
  # @return [ConvenientService::Support::BacktraceCleaner]
135
318
  #
319
+ # @see https://api.rubyonrails.org/v8.0.2/classes/ActiveSupport/BacktraceCleaner.html
320
+ # @see https://github.com/rails/rails/blob/v8.0.2/activesupport/lib/active_support/backtrace_cleaner.rb
321
+ #
322
+ # @example How to remove all backtrace cleaner filters?
323
+ # ConvenientService.backtrace_cleaner.remove_filters!
324
+ #
325
+ # @example How to remove all backtrace cleaner silencers?
326
+ # ConvenientService.backtrace_cleaner.remove_silencers!
327
+ #
328
+ # @example How to add backtrace cleaner stdlib silencer?
329
+ # ConvenientService.backtrace_cleaner.add_stdlib_silencer
330
+ #
331
+ # @example How to add backtrace cleaner Convenient Service silencer?
332
+ # ConvenientService.backtrace_cleaner.add_convenient_service_silencer
333
+ #
334
+ # @example Hot to clean exception backtrace?
335
+ # begin
336
+ # 16 / 0
337
+ # rescue => exception
338
+ # end
339
+ #
340
+ # ConvenientService.backtrace_cleaner.clean(exception.backtrace)
341
+ #
136
342
  def backtrace_cleaner
137
343
  @backtrace_cleaner ||= Support::BacktraceCleaner.new
138
344
  end
139
345
 
140
346
  ##
347
+ # Raises Convenient Service exceptions.
348
+ # Cleans exception backtrace with `ConvenientService.backtrace_cleaner.clean` before delegating to `Kernel.raise`.
349
+ #
141
350
  # @api public
351
+ # @since 1.0.0
142
352
  # @param original_exception [StandardError]
143
353
  # @raise [StandardError]
144
354
  #
355
+ # @note Expected to be used from Convenient Service plugins.
356
+ #
145
357
  # @internal
146
358
  # NOTE: `rescue ::StandardError => exception` is the same as `rescue => exception`.
147
359
  #
@@ -152,10 +364,16 @@ module ConvenientService
152
364
  end
153
365
 
154
366
  ##
367
+ # Re-raises Convenient Service exceptions.
368
+ # Cleans exception backtrace with `ConvenientService.backtrace_cleaner.clean` before delegating to `Kernel.raise`.
369
+ #
155
370
  # @api public
371
+ # @since 1.0.0
156
372
  # @return [Object] Can be any type.
157
373
  # @raise [StandardError]
158
374
  #
375
+ # @note Expected to be used from Convenient Service plugins.
376
+ #
159
377
  # @internal
160
378
  # NOTE: `rescue ::StandardError => exception` is the same as `rescue => exception`.
161
379
  #