lite-command 3.1.3 → 3.1.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: 39377d9ecb58d9d61ae0c99305d9db08672b7f97eb368194fc209b419cec1d0a
4
- data.tar.gz: 5ec36c8fcb1d68e2a6bd3ae0866f687a9c04469d502dcba0bc7fe092a9b88351
3
+ metadata.gz: 25949bea3778fbe660ae361819879bc36deb461fb37e9efa4ad11012529ae682
4
+ data.tar.gz: 7f9df66b9027c3c7e3cb7b8efaad1508dec68c22b4b63ab27c4ffd123707519e
5
5
  SHA512:
6
- metadata.gz: 521be42ef9eab72ca99c2cd8dfba53ca3156538daf1e7715aff5123aefc6a70d7c4b439962306b8d1d22b87897bed1f54167353f98bea32d83ebf5eaaceffefe
7
- data.tar.gz: 6c33d9b29e925c30b2e5abe06088fcfb0bccbd9cedbd241d76f398a89cfb7a679bf2a59b86ae766ccec69d94496640613d39995e9eca35ac3938d165a8e96eed
6
+ metadata.gz: 9c2ee67da223708a63852d539ab82f7a893214c1016376fc0af4496d224ccdcfef3340b1267f6fec11348ae8d2ede2c87b605494aa2090a8f449c317a3d9613a
7
+ data.tar.gz: 29ec28857fc0e8f29e1ba34a41cae8b44298a44c9477e73a237f5eb708965ba555a4b82edff984b20b61b4f1967ce93586ec74075a8421f6481c353865c0e275
data/CHANGELOG.md CHANGED
@@ -6,6 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [3.1.5] - 2024-10-28
10
+ ### Changed
11
+ - Renamed private `delegate` method to `delegates` to prevent rails clash
12
+
13
+ ## [3.1.4] - 2024-10-27
14
+ ### Added
15
+ - Add exception data to results hash
16
+ ### Changed
17
+ - Relocated some results methods to a runtimes internal module
18
+ - Assign correct fault even when child command is hard called
19
+ - Renamed `fault_exception` to `command_exception`
20
+
9
21
  ## [3.1.3] - 2024-10-26
10
22
  ### Added
11
23
  - Added passing `original_exception` to fault `!` calls
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lite-command (3.1.3)
4
+ lite-command (3.1.5)
5
5
  activemodel
6
6
  ostruct
7
7
 
data/README.md CHANGED
@@ -262,9 +262,18 @@ class DecryptSecretMessage < Lite::Command::Base
262
262
 
263
263
  validates :encrypted_message, length: 10..999
264
264
  validates :version, inclusion: { in: %w[v1 v3 v8], allow_blank: true }
265
+ validate :validate_decrypt_magic_numbers
265
266
 
266
267
  def call
267
- context.decrypted_message = SecretMessage.decrypt(ctx.encrypted_message)
268
+ context.decrypted_message = SecretMessage.decrypt(encrypted_message)
269
+ end
270
+
271
+ private
272
+
273
+ def validate_decrypt_magic_numbers
274
+ return if encrypted_message.starts_with?("~x01~")
275
+
276
+ errors.add(:encrypted_message, :invalid, message: "has invalid magic numbers")
268
277
  end
269
278
 
270
279
  end
@@ -352,6 +361,9 @@ cmd.status #=> "invalid"
352
361
  cmd.reason #=> "Invalid message start value"
353
362
  cmd.metadata #=> { i18n: "gb.invalid_start_value" }
354
363
 
364
+ cmd.original_exception #=> <RuntimeError ...>
365
+ cmd.command_exception #=> <DecryptSecretMessage::Error ...>
366
+
355
367
  cmd.success? #=> false
356
368
  cmd.noop? #=> false
357
369
  cmd.invalid? #=> true
@@ -606,13 +618,15 @@ command.to_hash #=> {
606
618
  #=> outcome: "failure",
607
619
  #=> state: "interrupted",
608
620
  #=> status: "failure",
609
- #=> reason: "[!] command stopped due to failure",
621
+ #=> reason: "Command stopped due to some failure",
610
622
  #=> metadata: {
611
623
  #=> errors: { name: ["is too short"] },
612
624
  #=> i18n_key: "command.failure"
613
625
  #=> },
614
- #=> caused_by: 1,
615
- #=> thrown_by: 1,
626
+ #=> caused_by: 3,
627
+ #=> caused_exception: "[ChildCommand::Failure] something is wrong from within",
628
+ #=> thrown_by: 2,
629
+ #=> thrown_exception: "[FailureCommand::Failure] something is wrong from within",
616
630
  #=> runtime: 0.0123
617
631
  #=> }
618
632
  ```
@@ -11,10 +11,11 @@ module Lite
11
11
 
12
12
  base.include ActiveModel::Validations
13
13
 
14
+ base.include Internals::Runtimes
14
15
  base.include Internals::Attributes
16
+ base.include Internals::Faults
15
17
  base.include Internals::Calls
16
18
  base.include Internals::Executions
17
- base.include Internals::Faults
18
19
  base.include Internals::Results
19
20
 
20
21
  if Lite::Command.configuration.raise_dynamic_faults # rubocop:disable Style/GuardClause
@@ -15,7 +15,7 @@ module Lite
15
15
  if object.respond_to?(:reason)
16
16
  object.reason
17
17
  elsif object.is_a?(StandardError)
18
- "[#{object.class.name}] #{object.message}".chomp(".")
18
+ Utils.pretty_exception(object)
19
19
  else
20
20
  object
21
21
  end
@@ -35,7 +35,7 @@ module Lite
35
35
  Utils.try(object, :thrown_by) || command.caused_by
36
36
  end
37
37
 
38
- def fault_exception
38
+ def command_exception
39
39
  return if command.success?
40
40
 
41
41
  Fault.build(
@@ -12,7 +12,7 @@ module Lite
12
12
  module ClassMethods
13
13
 
14
14
  def required(*attributes, from: :context, **options)
15
- delegate(*attributes, from:)
15
+ delegates(*attributes, from:)
16
16
 
17
17
  validates_each(*attributes, **options) do |command, method_name, _attr_value|
18
18
  next if command.errors.added?(from, :undefined) || command.errors.added?(method_name, :required)
@@ -26,12 +26,12 @@ module Lite
26
26
  end
27
27
 
28
28
  def optional(*attributes, from: :context, **_options)
29
- delegate(*attributes, from:)
29
+ delegates(*attributes, from:)
30
30
  end
31
31
 
32
32
  private
33
33
 
34
- def delegate(*attributes, from: :context)
34
+ def delegates(*attributes, from: :context)
35
35
  attributes.each do |method_name|
36
36
  define_method(method_name) do
37
37
  return unless respond_to?(from)
@@ -75,7 +75,7 @@ module Lite
75
75
  str.nil? || str == reason
76
76
  end
77
77
 
78
- def fault(object, s, m, oe) # rubocop:disable Naming/MethodParameterName
78
+ def fault(object, s, m, exception: nil) # rubocop:disable Naming/MethodParameterName
79
79
  return if s == SUCCESS || status != SUCCESS
80
80
 
81
81
  @status = s
@@ -87,8 +87,8 @@ module Lite
87
87
  @caused_by ||= fault_streamer.caused_by
88
88
  @thrown_by ||= fault_streamer.thrown_by
89
89
 
90
- @fault_exception ||= fault_streamer.fault_exception
91
- @original_exception ||= oe || fault_exception
90
+ @command_exception ||= fault_streamer.command_exception
91
+ @original_exception ||= exception || command_exception
92
92
  end
93
93
 
94
94
  FAULTS.each do |f|
@@ -96,9 +96,9 @@ module Lite
96
96
  define_method(:"#{f}!") do |object, metadata: nil, original_exception: nil|
97
97
  return unless success?
98
98
 
99
- fault(object, f, metadata, original_exception)
99
+ fault(object, f, metadata, exception: original_exception)
100
100
 
101
- raise(fault_exception)
101
+ raise(command_exception)
102
102
  end
103
103
  end
104
104
 
@@ -75,7 +75,7 @@ module Lite
75
75
  around_execution { call }
76
76
  Utils.try(self, :on_success)
77
77
  rescue StandardError => e
78
- fault(e, ERROR, metadata, e)
78
+ fault(e, Utils.try(e, :type) || ERROR, metadata, exception: e)
79
79
  after_execution
80
80
  Utils.try(self, :"on_#{status}", e)
81
81
  ensure
@@ -86,7 +86,7 @@ module Lite
86
86
  around_execution { call }
87
87
  Utils.try(self, :on_success)
88
88
  rescue StandardError => e
89
- fault(e, ERROR, metadata, e)
89
+ fault(e, Utils.try(e, :type) || ERROR, metadata, exception: e)
90
90
  after_execution
91
91
  Utils.try(self, :"on_#{status}", e)
92
92
  raise(e)
@@ -6,7 +6,7 @@ module Lite
6
6
  module Faults
7
7
 
8
8
  def self.included(base)
9
- base.class_eval { attr_reader :fault_exception, :original_exception }
9
+ base.class_eval { attr_reader :command_exception, :original_exception }
10
10
  end
11
11
 
12
12
  def caused_by
@@ -34,7 +34,7 @@ module Lite
34
34
  end
35
35
 
36
36
  def raise!(original: false)
37
- exception = (fault_exception unless original) || original_exception
37
+ exception = (command_exception unless original) || original_exception
38
38
  return if exception.nil?
39
39
 
40
40
  raise(exception)
@@ -1,20 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "securerandom" unless defined?(SecureRandom)
4
-
5
3
  module Lite
6
4
  module Command
7
5
  module Internals
8
6
  module Results
9
7
 
10
- def index
11
- @index ||= context.index ||= 0
12
- end
13
-
14
- def cmd_id
15
- @cmd_id ||= context.cmd_id ||= SecureRandom.uuid
16
- end
17
-
18
8
  def outcome
19
9
  return state if pending? || thrown?
20
10
 
@@ -36,7 +26,9 @@ module Lite
36
26
  reason:,
37
27
  metadata:,
38
28
  caused_by: caused_by&.index,
29
+ caused_exception: Utils.pretty_exception(caused_by&.original_exception),
39
30
  thrown_by: thrown_by&.index,
31
+ thrown_exception: Utils.pretty_exception(thrown_by&.command_exception),
40
32
  runtime:
41
33
  }.compact
42
34
  end
@@ -44,26 +36,6 @@ module Lite
44
36
 
45
37
  private
46
38
 
47
- def assign_execution_cmd_id
48
- @cmd_id = context.cmd_id ||= cmd_id
49
- end
50
-
51
- def increment_execution_index
52
- @index = context.index = index.next
53
- end
54
-
55
- def start_monotonic_time
56
- @start_monotonic_time ||= Utils.monotonic_time
57
- end
58
-
59
- def stop_monotonic_time
60
- @stop_monotonic_time ||= Utils.monotonic_time
61
- end
62
-
63
- def runtime
64
- stop_monotonic_time - start_monotonic_time
65
- end
66
-
67
39
  def append_execution_result
68
40
  results.push(self).sort_by!(&:index)
69
41
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom" unless defined?(SecureRandom)
4
+
5
+ module Lite
6
+ module Command
7
+ module Internals
8
+ module Runtimes
9
+
10
+ def index
11
+ @index ||= context.index ||= 0
12
+ end
13
+
14
+ def cmd_id
15
+ @cmd_id ||= context.cmd_id ||= SecureRandom.uuid
16
+ end
17
+
18
+ private
19
+
20
+ def assign_execution_cmd_id
21
+ @cmd_id = context.cmd_id ||= cmd_id
22
+ end
23
+
24
+ def increment_execution_index
25
+ @index = context.index = index.next
26
+ end
27
+
28
+ def start_monotonic_time
29
+ @start_monotonic_time ||= Utils.monotonic_time
30
+ end
31
+
32
+ def stop_monotonic_time
33
+ @stop_monotonic_time ||= Utils.monotonic_time
34
+ end
35
+
36
+ def runtime
37
+ stop_monotonic_time - start_monotonic_time
38
+ end
39
+
40
+ end
41
+ end
42
+ end
43
+ end
@@ -10,6 +10,12 @@ module Lite
10
10
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
11
11
  end
12
12
 
13
+ def pretty_exception(exception)
14
+ return if exception.nil?
15
+
16
+ "[#{exception.class.name}] #{exception.message}".chomp(".")
17
+ end
18
+
13
19
  def descendant_of?(object, other)
14
20
  object_class = object.respond_to?(:new) ? object : object.class
15
21
  other_class = other.respond_to?(:new) ? other : other.class
@@ -3,7 +3,7 @@
3
3
  module Lite
4
4
  module Command
5
5
 
6
- VERSION = "3.1.3"
6
+ VERSION = "3.1.5"
7
7
 
8
8
  end
9
9
  end
data/lib/lite/command.rb CHANGED
@@ -11,10 +11,11 @@ require "lite/command/utils"
11
11
  require "lite/command/context"
12
12
  require "lite/command/fault"
13
13
  require "lite/command/fault_streamer"
14
+ require "lite/command/internals/runtimes"
14
15
  require "lite/command/internals/attributes"
16
+ require "lite/command/internals/faults"
15
17
  require "lite/command/internals/calls"
16
18
  require "lite/command/internals/executions"
17
- require "lite/command/internals/faults"
18
19
  require "lite/command/internals/results"
19
20
  require "lite/command/base"
20
21
  require "lite/command/step"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lite-command
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.3
4
+ version: 3.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-26 00:00:00.000000000 Z
11
+ date: 2024-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -202,6 +202,7 @@ files:
202
202
  - lib/lite/command/internals/executions.rb
203
203
  - lib/lite/command/internals/faults.rb
204
204
  - lib/lite/command/internals/results.rb
205
+ - lib/lite/command/internals/runtimes.rb
205
206
  - lib/lite/command/sequence.rb
206
207
  - lib/lite/command/step.rb
207
208
  - lib/lite/command/utils.rb