convenient_service 0.25.0 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a077e3ef95d8ad18dbc79399c5513f29a17491111557cfad7b446eda2bec5a8
4
- data.tar.gz: 07e8481191d8a8fe51b7995d5507147755ee2676cfe51b9c5a5b11e42f5a47e2
3
+ metadata.gz: 31bdaa61be77d7c69da22c3baaf05b545d60606fbf45e9a7baac33929e845544
4
+ data.tar.gz: 7fa4432c8ce4b57a43b3bbcc9c8ea18d5002b69fc00db81da0cba78c9ac8ba54
5
5
  SHA512:
6
- metadata.gz: 268d3a56e8ea1e0161dde099762d3d91dcb8c85b005d2fc45e6ab9e8e622a7da15b4c6d70c45809dd8cd83787cbb042ffe71f4aaef369be4562f18d2b6b22340
7
- data.tar.gz: deeb6b2c67a294a98fee2fdb409d653b4674c83c22f9e8ef413014ef42d1dfe8127e3aaecc85d020a206350607f2276e1f09575f5bb9e0888e4927a73697e513
6
+ metadata.gz: 8195321c43354288cd9d99a74a3384dbc552ca4845d0fa58ef347a34e21cab420c284a4bed10ea62d71885c02cff5e3999b9956facd50d8ea58928eb74e7cf67
7
+ data.tar.gz: 9d4007218ddf5eb3061a57f6038af8596c7e98bf8dc4791acf7e0ef66e1a0b9987d36689e508747eeef54c3fa7f4291374a500b032a5944e03c677a26feb0110
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # @author Marian Kostyk <mariankostyk13895@gmail.com>
5
+ # @license LGPLv3 <https://www.gnu.org/licenses/lgpl-3.0.html>
6
+ ##
7
+
8
+ module ConvenientService
9
+ module Common
10
+ module Plugins
11
+ module TriesToConvertResultDuckToResult
12
+ class Middleware < MethodChainMiddleware
13
+ intended_for :result, entity: any_entity
14
+
15
+ ##
16
+ # @return [ConvenientService::Service::Plugins::HasJSendResult::Entities::Result]
17
+ #
18
+ # @internal
19
+ # NOTE: When original result is a result duck and it has `NoMethodError` exception, that exception should be raised just like any other exception. See specs for details.
20
+ #
21
+ # NOTE: `exception.receiver` is available on `NameError` descendants (like `NoMethodError`), but NOT on `StandardError`.
22
+ # - https://ruby-doc.org/core-2.7.1/NoMethodError.html
23
+ # - https://ruby-doc.org/core-2.7.1/NameError.html#method-i-receiver
24
+ # - https://ruby-doc.org/core-2.7.1/StandardError.html
25
+ #
26
+ def next(...)
27
+ original_object = chain.next(...)
28
+
29
+ result_duck, result = try_convert(original_object)
30
+
31
+ result_duck ? result : original_object
32
+ end
33
+
34
+ private
35
+
36
+ ##
37
+ # @param original_object [Object] Can be any type.
38
+ # @return [Array]
39
+ # @raise [NoMethodError]
40
+ #
41
+ def try_convert(original_object)
42
+ result = original_object.result
43
+
44
+ [true, result]
45
+ rescue ::NoMethodError => exception
46
+ raise exception if original_object.respond_to?(:result)
47
+
48
+ [false, nil]
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # @author Marian Kostyk <mariankostyk13895@gmail.com>
5
+ # @license LGPLv3 <https://www.gnu.org/licenses/lgpl-3.0.html>
6
+ ##
7
+
8
+ require_relative "tries_to_convert_result_duck_to_result/middleware"
@@ -14,6 +14,7 @@ require_relative "plugins/caches_return_value"
14
14
  require_relative "plugins/can_be_copied"
15
15
  require_relative "plugins/can_have_user_provided_entity"
16
16
  require_relative "plugins/ensures_negated_j_send_result"
17
+ require_relative "plugins/tries_to_convert_result_duck_to_result"
17
18
  require_relative "plugins/can_have_callbacks"
18
19
  require_relative "plugins/can_have_not_passed_arguments"
19
20
  require_relative "plugins/can_utilize_finite_loop"
@@ -29,6 +29,7 @@ module ConvenientService
29
29
  :result_parents_trace,
30
30
  :code_review_automation,
31
31
  :short_syntax,
32
+ :duck_typing,
32
33
  :type_safety,
33
34
  :exception_services_trace,
34
35
  :per_instance_caching,
@@ -58,6 +59,7 @@ module ConvenientService
58
59
  :result_parents_trace,
59
60
  :code_review_automation,
60
61
  :short_syntax,
62
+ :duck_typing,
61
63
  :type_safety,
62
64
  :exception_services_trace,
63
65
  :per_instance_caching,
@@ -134,6 +136,7 @@ module ConvenientService
134
136
  use ConvenientService::Plugins::Service::RaisesOnNotResultReturnValue::Middleware if options.enabled?(:type_safety)
135
137
  use ConvenientService::Plugins::Service::RescuesResultUnhandledExceptions::Middleware if options.enabled?(:fault_tolerance)
136
138
  use ConvenientService::Plugins::Common::CleansExceptionBacktrace::Middleware if options.enabled?(:backtrace_cleaner)
139
+ use ConvenientService::Plugins::Common::TriesToConvertResultDuckToResult::Middleware if options.enabled?(:duck_typing)
137
140
  use ConvenientService::Plugins::Service::HasJSendResultParamsValidations::UsingActiveModelValidations::Middleware if options.enabled?(:active_model_validations)
138
141
  use ConvenientService::Plugins::Service::HasJSendResultParamsValidations::UsingDryValidation::Middleware if options.enabled?(:dry_validation)
139
142
  use ConvenientService::Plugins::Service::CanHaveConnectedSteps::Middleware if options.enabled?(:essential)
@@ -146,6 +149,7 @@ module ConvenientService
146
149
  use ConvenientService::Plugins::Service::RaisesOnNotResultReturnValue::Middleware if options.enabled?(:type_safety)
147
150
  use ConvenientService::Plugins::Service::RescuesResultUnhandledExceptions::Middleware if options.enabled?(:fault_tolerance)
148
151
  use ConvenientService::Plugins::Common::CleansExceptionBacktrace::Middleware if options.enabled?(:backtrace_cleaner)
152
+ use ConvenientService::Plugins::Common::TriesToConvertResultDuckToResult::Middleware if options.enabled?(:duck_typing)
149
153
  end
150
154
 
151
155
  middlewares :regular_result do
@@ -160,27 +164,30 @@ module ConvenientService
160
164
  use ConvenientService::Plugins::Common::CachesReturnValue::Middleware if options.enabled?(:per_instance_caching)
161
165
  use ConvenientService::Plugins::Service::CollectsServicesInException::Middleware if options.enabled?(:exception_services_trace)
162
166
  use ConvenientService::Plugins::Service::RaisesOnNotResultReturnValue::Middleware if options.enabled?(:type_safety)
163
- # use ConvenientService::Plugins::Service::RescuesResultUnhandledExceptions::Middleware if options.enabled?(:fault_tolerance) # TODO: Dedicted `rescue`?
167
+ # use ConvenientService::Plugins::Service::RescuesResultUnhandledExceptions::Middleware if options.enabled?(:fault_tolerance) # TODO: Dedicated `rescue`?
164
168
  use ConvenientService::Plugins::Common::CleansExceptionBacktrace::Middleware if options.enabled?(:backtrace_cleaner)
165
169
  use ConvenientService::Plugins::Service::CanHaveFallbacks::Middleware.with(status: :failure) if options.enabled?(:fallbacks)
170
+ use ConvenientService::Plugins::Common::TriesToConvertResultDuckToResult::Middleware if options.enabled?(:duck_typing)
166
171
  end
167
172
 
168
173
  middlewares :fallback_error_result do
169
174
  use ConvenientService::Plugins::Common::CachesReturnValue::Middleware if options.enabled?(:per_instance_caching)
170
175
  use ConvenientService::Plugins::Service::CollectsServicesInException::Middleware if options.enabled?(:exception_services_trace)
171
176
  use ConvenientService::Plugins::Service::RaisesOnNotResultReturnValue::Middleware if options.enabled?(:type_safety)
172
- # use ConvenientService::Plugins::Service::RescuesResultUnhandledExceptions::Middleware if options.enabled?(:fault_tolerance) # TODO: Dedicted `rescue`?
177
+ # use ConvenientService::Plugins::Service::RescuesResultUnhandledExceptions::Middleware if options.enabled?(:fault_tolerance) # TODO: Dedicated `rescue`?
173
178
  use ConvenientService::Plugins::Common::CleansExceptionBacktrace::Middleware if options.enabled?(:backtrace_cleaner)
174
179
  use ConvenientService::Plugins::Service::CanHaveFallbacks::Middleware.with(status: :error) if options.enabled?(:fallbacks)
180
+ use ConvenientService::Plugins::Common::TriesToConvertResultDuckToResult::Middleware if options.enabled?(:duck_typing)
175
181
  end
176
182
 
177
183
  middlewares :fallback_result do
178
184
  use ConvenientService::Plugins::Common::CachesReturnValue::Middleware if options.enabled?(:per_instance_caching)
179
185
  use ConvenientService::Plugins::Service::CollectsServicesInException::Middleware if options.enabled?(:exception_services_trace)
180
186
  use ConvenientService::Plugins::Service::RaisesOnNotResultReturnValue::Middleware if options.enabled?(:type_safety)
181
- # use ConvenientService::Plugins::Service::RescuesResultUnhandledExceptions::Middleware if options.enabled?(:fault_tolerance) # TODO: Dedicted `rescue`?
187
+ # use ConvenientService::Plugins::Service::RescuesResultUnhandledExceptions::Middleware if options.enabled?(:fault_tolerance) # TODO: Dedicated `rescue`?
182
188
  use ConvenientService::Plugins::Common::CleansExceptionBacktrace::Middleware if options.enabled?(:backtrace_cleaner)
183
189
  use ConvenientService::Plugins::Service::CanHaveFallbacks::Middleware.with(status: nil) if options.enabled?(:fallbacks)
190
+ use ConvenientService::Plugins::Common::TriesToConvertResultDuckToResult::Middleware if options.enabled?(:duck_typing)
184
191
  end
185
192
 
186
193
  middlewares :success do
@@ -35,8 +35,13 @@ module ConvenientService
35
35
  extra_kwargs.dig(:exceptions, :handled)
36
36
  end
37
37
 
38
+ ##
39
+ # @return [ConvenientService::Service::Plugins::HasJSendResult::Entities::Result]
40
+ # @raise [ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanBeFromHandledException::Exceptions::FromExceptionOnNotErrorResult]
41
+ #
38
42
  def from_exception(exception, **kwargs)
39
43
  ::ConvenientService.raise Exceptions::FromExceptionOnNotErrorResult.new(result: self) unless status.unsafe_error?
44
+
40
45
  data =
41
46
  if Service::Plugins::HasJSendResult.default_error_data == unsafe_data.to_h
42
47
  {handled_exception: exception}
@@ -103,6 +103,13 @@ module ConvenientService
103
103
  internals.cache[:jsend_attributes]
104
104
  end
105
105
 
106
+ ##
107
+ # @return [ConvenientService::Service::Plugins::HasJSendResult::Entities::Result]
108
+ #
109
+ def result
110
+ self
111
+ end
112
+
106
113
  ##
107
114
  # @param status [Symbol]
108
115
  # @return [ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Entities::Status]
@@ -12,12 +12,16 @@ module ConvenientService
12
12
  class Middleware < MethodChainMiddleware
13
13
  intended_for any_method, entity: :service
14
14
 
15
+ ##
16
+ # @return [ConvenientService::Service::Plugins::HasJSendResult::Entities::Result]
17
+ # @raise [ConvenientService::Service::Plugins::RaisesOnNotResultReturnValue::Exceptions::ReturnValueNotKindOfResult]
18
+ #
15
19
  def next(...)
16
- original_result = chain.next(...)
20
+ result = chain.next(...)
17
21
 
18
- return original_result if Service::Plugins::HasJSendResult.result?(original_result)
22
+ return result if Service::Plugins::HasJSendResult.result?(result)
19
23
 
20
- ::ConvenientService.raise Exceptions::ReturnValueNotKindOfResult.new(service: entity, result: original_result, method: method)
24
+ ::ConvenientService.raise Exceptions::ReturnValueNotKindOfResult.new(service: entity, result: result, method: method)
21
25
  end
22
26
  end
23
27
  end
@@ -13,5 +13,5 @@ module ConvenientService
13
13
  # @since 1.0.0
14
14
  # @return [String]
15
15
  #
16
- VERSION = "0.25.0"
16
+ VERSION = "0.26.0"
17
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: convenient_service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.25.0
4
+ version: 0.26.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marian Kostyk
@@ -108,6 +108,8 @@ files:
108
108
  - lib/convenient_service/common/plugins/has_j_send_result_duck_short_syntax/concern.rb
109
109
  - lib/convenient_service/common/plugins/has_memoization/using_memo_wise.rb
110
110
  - lib/convenient_service/common/plugins/has_memoization/using_memo_wise/concern.rb
111
+ - lib/convenient_service/common/plugins/tries_to_convert_result_duck_to_result.rb
112
+ - lib/convenient_service/common/plugins/tries_to_convert_result_duck_to_result/middleware.rb
111
113
  - lib/convenient_service/config.rb
112
114
  - lib/convenient_service/config/commands.rb
113
115
  - lib/convenient_service/config/commands/normalize_options.rb
@@ -912,7 +914,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
912
914
  - !ruby/object:Gem::Version
913
915
  version: '0'
914
916
  requirements: []
915
- rubygems_version: 4.0.3
917
+ rubygems_version: 4.0.20
916
918
  specification_version: 4
917
919
  summary: Ruby Service Objects with Steps and more.
918
920
  test_files: []