toolx 0.2.2 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9155a4cd82106186e76da35529769fffb623cd797e5f2475835830a4c835655
4
- data.tar.gz: cb4c8e54f4ffc0b163cdb5e9774bb17e5b759600d7e8a79b719f1067aa041783
3
+ metadata.gz: 24ea4449f0a1a046d1eb279ff79a5264023e2d491a29fe065da08779973a517a
4
+ data.tar.gz: 118adfc3c279eac422329ab3755ee70100c3dafa6b4102d9b9fa5e28c539df64
5
5
  SHA512:
6
- metadata.gz: 3e5c3dd888e0962f0f89d7cb14f4e11a2ddc84a3695814ca0a9e4a23ec5ebc63694d2593ad3ef63e3ea9b6e444ac02a4d0b259b71a4ced70b5b8c0557c9795ad
7
- data.tar.gz: a553bd9dc0cdc781153ea8c8d25773900d0448bff3b380c235cb42e2c66b1cee85af7dbfa6a8def379f0cb0c89fb1298eae489dc7d12fb9ec6f44be82da9f92e
6
+ metadata.gz: cf1b7111d44504c36e6b0e8cd1ddccc1afa32852bc591a1440c03ffe65355bbf8f4b814004ca769b9f7d190b34ddaf0586e347e337df51b1626a0d4911796760
7
+ data.tar.gz: e7083b373203caf0c77ea90f9f1e6dd9459b9d5daa24051c15673c6991baec9c519ad91f3d03de05291de6d0b667a804ca583b88568db1bdca359229d08fe03c
@@ -1,4 +1,5 @@
1
1
  module Toolx::Core::Operation::CallbacksWrapper
2
+ # When class is loaded twice all callbacks are duplicated.
2
3
  def perform(...)
3
4
  run_callbacks :perform do
4
5
  super
@@ -1,3 +1,5 @@
1
+ require_relative '../errors/nested_standard_error'
2
+
1
3
  module Toolx::Core::Operation::Flow
2
4
  def self.included(base)
3
5
  base.send(:extend, ClassMethods)
@@ -15,9 +17,13 @@ module Toolx::Core::Operation::Flow
15
17
  end
16
18
 
17
19
  class Executor
18
- ExecutorError = Class.new(StandardError)
20
+ include ::ActiveSupport::Rescuable
21
+ ExecutorError = Class.new(::Toolx::Core::Errors::NestedStandardError)
19
22
  OperationAlreadyInitialized = Class.new(ExecutorError)
20
23
  OperationNotInitialized = Class.new(ExecutorError)
24
+ ActionException = Class.new(::Toolx::Core::Errors::NestedStandardError)
25
+ OnFailException = Class.new(ActionException)
26
+ OnSuccessException = Class.new(ActionException)
21
27
 
22
28
  def initialize(operation_class)
23
29
  @operation_class = operation_class
@@ -173,7 +179,9 @@ module Toolx::Core::Operation::Flow
173
179
  execute_actions
174
180
  self
175
181
  rescue => exception
176
- raise if exception.is_a?(ExecutorError)
182
+ # TODO: Rethink
183
+ raise if exception.is_a?(ExecutorError) || exception.is_a?(OnSuccessException)
184
+ # rescue_with_handler(exception) || raise
177
185
  @exception = exception
178
186
  execute_actions
179
187
  self
@@ -192,7 +200,11 @@ module Toolx::Core::Operation::Flow
192
200
  action = actions[kind]
193
201
 
194
202
  if action.is_a?(Symbol) && bind_object
195
- arity = bind_object.method(action).arity
203
+ arity = begin
204
+ bind_object.method(action).arity
205
+ rescue NameError => e
206
+ raise ExecutorError, "Action '#{action}' not found in #{bind_object.class.name}. Details: #{e.message}"
207
+ end
196
208
  case arity
197
209
  when 0 then bind_object.send(action)
198
210
  when 1 then bind_object.send(action, response)
@@ -205,15 +217,22 @@ module Toolx::Core::Operation::Flow
205
217
  else action.call(response, self)
206
218
  end
207
219
  end
208
-
209
- # bind_object.send(action, self) if action.is_a?(Symbol) && bind_object
210
- # action.call(self) if action.is_a?(Proc)
211
- rescue NameError => e
212
- raise ExecutorError, "Action '#{action}' not found in #{bind_object.class.name}" if bind_object
213
220
  end
214
221
 
215
222
  def execute_actions
216
- success? ? execute_action_kind(:success) : execute_action_kind(:fail)
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
217
236
  self
218
237
  end
219
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
- err = ArgumentError.new <<~MSG
11
- #{e.message}
12
- Provided: #{params.inspect}\n
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
- err.set_backtrace(e.backtrace)
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
- err = ArgumentError.new <<~MSG
16
- #{e.message}
17
- Provided: #{response.inspect}\n
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
- err.set_backtrace(e.backtrace)
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 |args = {}|
24
+ define_singleton_method(:[]) do |*args_array, **kwargs|
23
25
  # new(**args).perform.public_send(attr_name)
24
- begin
25
- new(**args).perform.public_send(attr_name)
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
- raise ArgumentError, <<~MSG
54
+ message = <<~MSG
34
55
  Expected #{self.name} keyword args: #{required}
35
- Got: #{args.inspect}
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
@@ -1,4 +1,5 @@
1
1
  class Toolx::Core::OperationBase
2
2
  def initialize(*, **) end
3
3
  def perform; end
4
+ def fail!(ex) = raise ex
4
5
  end
data/lib/toolx/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Toolx
4
- VERSION = '0.2.2'
4
+ VERSION = '0.2.4'.freeze
5
5
  end
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.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: 2025-06-29 00:00:00.000000000 Z
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.3
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.