toolx 0.2.3 → 0.2.5

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: ab4f3af6bd6512a1a0db4eb3f0d24b0ab12d07b46150bf481862fd7c0b0a76ee
4
- data.tar.gz: d1f3c32d678d96285cb683c146372437ae2a00000483019876bec3e0d53cc3a1
3
+ metadata.gz: b226bf96b643cb84becf9703ed3089a2fd3fb6d5dd7c48ca854f6e5afaae214a
4
+ data.tar.gz: a7592f19082757e2b4d9a623b4c3f2c0ec3464d73c58fab50106f396ccff2bb2
5
5
  SHA512:
6
- metadata.gz: 91a3d2d5726133924461dadcde633c1121b94b694c94f7b27842edf7acbea8bc2549fe1c0232df5484c59a13cfa1687a43eb86b55153a5bb1c9c3f8afd8139af
7
- data.tar.gz: 06ac13ddeebfb93c702daf6e809f76a248622ed1d9036561f4927b19439ef9553493c2850bf015a90aa5b265ea2c31402d8150b1db5fcecd0774cec250218e0e
6
+ metadata.gz: 9ec23d6768ccbe3c019f711eca31f9b2d6642284d882593a3e3e02d10a263a3402738c5faef494efaad55124ba2377134f5ed929d16205e4c708f75e54bbbc32
7
+ data.tar.gz: 8c1ad828692754eac3d1fef3fd4f38b1d9f9c3b002bca3d11805e7fe2f1ffe8720c74ac563cbd20393145c45ccde132712daa0f6bf3e1d7c172114751ae82ce2
@@ -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
@@ -17,9 +17,19 @@ 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
+ OnSuccessException = Class.new(ActionException)
26
+ OnFailException = Class.new(ActionException) do
27
+ attr_reader :operation_exception
28
+ def initialize(msg = nil, nested = $ERROR_INFO, operation_exception = nil)
29
+ super(msg, nested)
30
+ @operation_exception = operation_exception
31
+ end
32
+ end
23
33
 
24
34
  def initialize(operation_class)
25
35
  @operation_class = operation_class
@@ -175,7 +185,9 @@ module Toolx::Core::Operation::Flow
175
185
  execute_actions
176
186
  self
177
187
  rescue => exception
178
- raise if exception.is_a?(ExecutorError)
188
+ # TODO: Rethink
189
+ raise if exception.is_a?(ExecutorError) || exception.is_a?(OnSuccessException)
190
+ # rescue_with_handler(exception) || raise
179
191
  @exception = exception
180
192
  execute_actions
181
193
  self
@@ -214,7 +226,19 @@ module Toolx::Core::Operation::Flow
214
226
  end
215
227
 
216
228
  def execute_actions
217
- success? ? execute_action_kind(:success) : execute_action_kind(:fail)
229
+ if success?
230
+ begin
231
+ execute_action_kind(:success)
232
+ rescue => e
233
+ raise OnSuccessException, "Error in success action: #{e.message}", e.backtrace
234
+ end
235
+ else
236
+ begin
237
+ execute_action_kind(:fail)
238
+ rescue => e
239
+ raise OnFailException.new("Error in fail action: #{e.message}", e, exception)
240
+ end
241
+ end
218
242
  self
219
243
  end
220
244
  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.3'
4
+ VERSION = '0.2.5'.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.3
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pawel Niemczyk
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-06-30 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.