toolx 0.2.3 → 0.2.4
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 +4 -4
- data/lib/toolx/core/operation/callbacks_wrapper.rb +1 -0
- data/lib/toolx/core/operation/flow.rb +20 -2
- data/lib/toolx/core/operation/params_wrapper.rb +8 -5
- data/lib/toolx/core/operation/response_wrapper.rb +8 -5
- data/lib/toolx/core/operation/simplified_result.rb +44 -6
- data/lib/toolx/core/operation_base.rb +1 -0
- data/lib/toolx/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24ea4449f0a1a046d1eb279ff79a5264023e2d491a29fe065da08779973a517a
|
4
|
+
data.tar.gz: 118adfc3c279eac422329ab3755ee70100c3dafa6b4102d9b9fa5e28c539df64
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf1b7111d44504c36e6b0e8cd1ddccc1afa32852bc591a1440c03ffe65355bbf8f4b814004ca769b9f7d190b34ddaf0586e347e337df51b1626a0d4911796760
|
7
|
+
data.tar.gz: e7083b373203caf0c77ea90f9f1e6dd9459b9d5daa24051c15673c6991baec9c519ad91f3d03de05291de6d0b667a804ca583b88568db1bdca359229d08fe03c
|
@@ -17,9 +17,13 @@ module Toolx::Core::Operation::Flow
|
|
17
17
|
end
|
18
18
|
|
19
19
|
class Executor
|
20
|
+
include ::ActiveSupport::Rescuable
|
20
21
|
ExecutorError = Class.new(::Toolx::Core::Errors::NestedStandardError)
|
21
22
|
OperationAlreadyInitialized = Class.new(ExecutorError)
|
22
23
|
OperationNotInitialized = Class.new(ExecutorError)
|
24
|
+
ActionException = Class.new(::Toolx::Core::Errors::NestedStandardError)
|
25
|
+
OnFailException = Class.new(ActionException)
|
26
|
+
OnSuccessException = Class.new(ActionException)
|
23
27
|
|
24
28
|
def initialize(operation_class)
|
25
29
|
@operation_class = operation_class
|
@@ -175,7 +179,9 @@ module Toolx::Core::Operation::Flow
|
|
175
179
|
execute_actions
|
176
180
|
self
|
177
181
|
rescue => exception
|
178
|
-
|
182
|
+
# TODO: Rethink
|
183
|
+
raise if exception.is_a?(ExecutorError) || exception.is_a?(OnSuccessException)
|
184
|
+
# rescue_with_handler(exception) || raise
|
179
185
|
@exception = exception
|
180
186
|
execute_actions
|
181
187
|
self
|
@@ -214,7 +220,19 @@ module Toolx::Core::Operation::Flow
|
|
214
220
|
end
|
215
221
|
|
216
222
|
def execute_actions
|
217
|
-
success?
|
223
|
+
if success?
|
224
|
+
begin
|
225
|
+
execute_action_kind(:success)
|
226
|
+
rescue => e
|
227
|
+
raise OnSuccessException, "Error in success action: #{e.message}", e.backtrace
|
228
|
+
end
|
229
|
+
else
|
230
|
+
begin
|
231
|
+
execute_action_kind(:fail)
|
232
|
+
rescue => e
|
233
|
+
raise OnFailException, "Error in fail action: #{e.message}", e.backtrace
|
234
|
+
end
|
235
|
+
end
|
218
236
|
self
|
219
237
|
end
|
220
238
|
end
|
@@ -1,18 +1,21 @@
|
|
1
1
|
require 'dry-struct'
|
2
|
+
require_relative '../errors/nested_standard_error'
|
2
3
|
|
3
4
|
module Toolx::Core::Operation::ParamsWrapper
|
5
|
+
ParamsArgumentError = Class.new(::Toolx::Core::Errors::NestedStandardError)
|
6
|
+
|
4
7
|
def initialize(*args, **kwargs)
|
5
8
|
params = args.first.nil? ? kwargs : args.first
|
6
9
|
if self.class.const_defined?(:Params, false) && !params.is_a?(self.class.const_get(:Params, false))
|
7
10
|
begin
|
8
11
|
params = self.class.const_get(:Params, false).new(params.deep_symbolize_keys)
|
9
12
|
rescue Dry::Struct::Error => e
|
10
|
-
|
11
|
-
#{
|
12
|
-
|
13
|
+
message = <<~MSG
|
14
|
+
Expected #{self.class.name} keyword args: #{self.class.const_get(:Params, false)}
|
15
|
+
Got: #{params.inspect}
|
16
|
+
Message: #{e.message}
|
13
17
|
MSG
|
14
|
-
|
15
|
-
raise err
|
18
|
+
raise ParamsArgumentError.new(message, e)
|
16
19
|
end
|
17
20
|
end
|
18
21
|
@params = params
|
@@ -1,6 +1,9 @@
|
|
1
1
|
require 'dry-struct'
|
2
|
+
require_relative '../errors/nested_standard_error'
|
2
3
|
|
3
4
|
module Toolx::Core::Operation::ResponseWrapper
|
5
|
+
ResponseArgumentError = Class.new(::Toolx::Core::Errors::NestedStandardError)
|
6
|
+
|
4
7
|
def perform(*args, **params)
|
5
8
|
response = if args.present? || params.present?
|
6
9
|
super
|
@@ -12,12 +15,12 @@ module Toolx::Core::Operation::ResponseWrapper
|
|
12
15
|
begin
|
13
16
|
response = self.class.const_get(:Response).new(response.to_h.deep_symbolize_keys)
|
14
17
|
rescue Dry::Struct::Error => e
|
15
|
-
|
16
|
-
#{
|
17
|
-
|
18
|
+
message = <<~MSG
|
19
|
+
Expected #{self.class.name} keyword args: #{self.class.const_get(:Response, false)}
|
20
|
+
Got: #{params.inspect}
|
21
|
+
Message: #{e.message}
|
18
22
|
MSG
|
19
|
-
|
20
|
-
raise err
|
23
|
+
raise ResponseArgumentError.new(message, e)
|
21
24
|
end
|
22
25
|
end
|
23
26
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require_relative 'params_wrapper'
|
2
|
+
|
1
3
|
module Toolx::Core::Operation::SimplifiedResult
|
2
4
|
def simplified_result(attr_name)
|
3
5
|
# This module allows defining a simplified accessor for the result of an Operation.
|
@@ -19,10 +21,29 @@ module Toolx::Core::Operation::SimplifiedResult
|
|
19
21
|
#
|
20
22
|
# This is useful for operations that return a single value
|
21
23
|
# and should provide a concise and readable API.
|
22
|
-
define_singleton_method(:[]) do
|
24
|
+
define_singleton_method(:[]) do |*args_array, **kwargs|
|
23
25
|
# new(**args).perform.public_send(attr_name)
|
24
|
-
|
25
|
-
|
26
|
+
begin
|
27
|
+
if args_array.empty?
|
28
|
+
# Regular keyword arguments: Code[source: 'PL']
|
29
|
+
new(**kwargs).perform.public_send(attr_name)
|
30
|
+
else
|
31
|
+
# Handle single argument like Code[:OK]
|
32
|
+
arg = args_array.first
|
33
|
+
|
34
|
+
if arg.is_a?(Hash)
|
35
|
+
# Hash argument: Code[{source: 'PL'}]
|
36
|
+
new(**arg).perform.public_send(attr_name)
|
37
|
+
else
|
38
|
+
# Non-hash argument like Symbol: Code[:OK]
|
39
|
+
# Try to convert to {source: :OK} format
|
40
|
+
if const_defined?(:Params, false) && (first_param = const_get(:Params, false).schema.keys.first)
|
41
|
+
new(**{first_param => arg}).perform.public_send(attr_name)
|
42
|
+
else
|
43
|
+
raise TypeError, "Cannot convert #{arg.class} to parameters hash"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
26
47
|
rescue TypeError, ArgumentError => e
|
27
48
|
required = if const_defined?(:Params, false)
|
28
49
|
const_get(:Params, false).schema.keys.join(', ')
|
@@ -30,12 +51,29 @@ module Toolx::Core::Operation::SimplifiedResult
|
|
30
51
|
'Operation params are wrongly assigned or not defined'
|
31
52
|
end
|
32
53
|
|
33
|
-
|
54
|
+
message = <<~MSG
|
34
55
|
Expected #{self.name} keyword args: #{required}
|
35
|
-
Got: #{
|
36
|
-
#{e.message}
|
56
|
+
Got: #{(args_array.first || kwargs).inspect}
|
57
|
+
Message: #{e.message}
|
37
58
|
MSG
|
59
|
+
|
60
|
+
raise Toolx::Core::Operation::ParamsWrapper::ParamsArgumentError.new(message, e)
|
38
61
|
end
|
62
|
+
# begin
|
63
|
+
# new(**args).perform.public_send(attr_name)
|
64
|
+
# rescue TypeError, ArgumentError => e
|
65
|
+
# required = if const_defined?(:Params, false)
|
66
|
+
# const_get(:Params, false).schema.keys.join(', ')
|
67
|
+
# else
|
68
|
+
# 'Operation params are wrongly assigned or not defined'
|
69
|
+
# end
|
70
|
+
#
|
71
|
+
# raise ArgumentError, <<~MSG
|
72
|
+
# Expected #{self.name} keyword args: #{required}
|
73
|
+
# Got: #{args.inspect}
|
74
|
+
# #{e.message}
|
75
|
+
# MSG
|
76
|
+
# end
|
39
77
|
end
|
40
78
|
|
41
79
|
class << self
|
data/lib/toolx/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: toolx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pawel Niemczyk
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: statesman
|
@@ -335,7 +335,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
335
335
|
- !ruby/object:Gem::Version
|
336
336
|
version: '0'
|
337
337
|
requirements: []
|
338
|
-
rubygems_version: 3.6.
|
338
|
+
rubygems_version: 3.6.9
|
339
339
|
specification_version: 4
|
340
340
|
summary: Toolx is a set of tools for Ruby on Rails applications that simplifies common
|
341
341
|
tasks and enhances development experience.
|