lite-command 3.1.0 → 3.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 953b62c2ba61091b949e8fe3a673bc98be6a516afcea85192ea356e356aaf84e
4
- data.tar.gz: adfb9bf29183586b33f8b32b801afadfbcf890398827e002dd794692bb708c9e
3
+ metadata.gz: 6eb09ff348ac355723d82848c69c1673d7ffa2782b3bb0061b16ccbe205b9529
4
+ data.tar.gz: 514ffc0ce29de277bd0969c29a51884d2509fd46b69e5cc6f85ded44fba4e827
5
5
  SHA512:
6
- metadata.gz: b618d0bda8cdbdc2335a16900e00addbdcf0a71e4ee6170c907d3b1e8110415990191d4bc9a9b5a0765582d2ce83df758d4b9109114fa33f04ec0ca27e388524
7
- data.tar.gz: 6a606bcd18ad5c97920e67557bc946f8e38446c83fdb2155a298530e338cf1f033e6388b411f7229919be6a70eb6c45510b8c1a1c95f9eeed0beabaa143550fa
6
+ metadata.gz: 00d47aa6fc59fa96895311bd78be8dcf3b283da16197f6973eb5db4100b61ef6303ab65673b695dfd51cfbc64ae63eca9b70adc0eba46763a25597fa1c1e6ba0
7
+ data.tar.gz: 2e60eaa36e8b86cec861bc67bfa3bee12292c8edf8c92503e4339128f69f467421f94a4b53ee1a8a96a467cb25617cb6918d553419bbb6b1fbd23c817b2227dd
data/CHANGELOG.md CHANGED
@@ -6,6 +6,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [3.1.2] - 2024-10-26
10
+ ### Changed
11
+ - Make state set methods private
12
+ - Add returns for invalid state and status transitions
13
+
14
+ ## [3.1.1] - 2024-10-25
15
+ ### Changed
16
+ - Add option to raise original or fault exception
17
+
9
18
  ## [3.1.0] - 2024-10-25
10
19
  ### Added
11
20
  - Added `raise!` method to reraise soft call errors
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lite-command (3.1.0)
4
+ lite-command (3.1.2)
5
5
  activemodel
6
6
  ostruct
7
7
 
@@ -136,7 +136,7 @@ GEM
136
136
  rubocop-ast (>= 1.31.1, < 2.0)
137
137
  rubocop-rake (0.6.0)
138
138
  rubocop (~> 1.0)
139
- rubocop-rspec (3.1.0)
139
+ rubocop-rspec (3.2.0)
140
140
  rubocop (~> 1.61)
141
141
  ruby-progressbar (1.13.0)
142
142
  securerandom (0.3.1)
data/README.md CHANGED
@@ -26,6 +26,7 @@ Or install it yourself as:
26
26
  * [Usage](#usage)
27
27
  * [Execution](#execution)
28
28
  * [Dynamic Faults](#dynamic-faults)
29
+ * [Raising Faults](#raising-faults)
29
30
  * [Context](#context)
30
31
  * [Attributes](#attributes)
31
32
  * [Validations](#validations)
@@ -88,21 +89,15 @@ end
88
89
 
89
90
  Executing a command can be done as an instance or class call. It returns the command instance
90
91
  in a frozen state. These will never call will never raise an execption, but will be kept track
91
- of in its internal state. You can raise the offending exception using `raise!`.
92
+ of in its internal state.
92
93
 
93
94
  ```ruby
94
- cmd = DecryptSecretMessage.call(...)
95
+ DecryptSecretMessage.call(...)
95
96
  # - or -
96
- cmd = DecryptSecretMessage.new(...).call
97
+ DecryptSecretMessage.new(...).call
97
98
 
98
99
  # On success, fault or exception:
99
- cmd #=> <DecryptSecretMessage ...>
100
-
101
- # On fault:
102
- cmd.raise! #=> raises Lite::Command::Fault
103
-
104
- # On exception:
105
- cmd.raise! #=> raises StandardError
100
+ #=> <DecryptSecretMessage ...>
106
101
  ```
107
102
 
108
103
  > [!TIP]
@@ -127,6 +122,29 @@ DecryptSecretMessage.new(...).call!
127
122
  #=> raises StandardError
128
123
  ```
129
124
 
125
+ ### Raising Faults
126
+
127
+ Sometimes its suitable to raise the offending soft call command fault later
128
+ in a call stack. Use the `raise!` method to reraise the fault or original
129
+ error (if they differ). `original: false` is the default.
130
+
131
+ ```ruby
132
+ cmd = DecryptSecretMessage.call(...)
133
+ Apm.track_stat("DecryptSecretMessage.called")
134
+ # other stuff...
135
+
136
+ # On success:
137
+ cmd.raise! #=> nil
138
+
139
+ # On fault:
140
+ cmd.raise!(original: false) #=> raises Lite::Command::Fault
141
+ cmd.raise!(original: true) #=> raises Lite::Command::Fault
142
+
143
+ # On exception:
144
+ cmd.raise!(original: false) #=> raises Lite::Command::Error
145
+ cmd.raise!(original: true) #=> raises StandardError
146
+ ```
147
+
130
148
  ### Dynamic Faults
131
149
 
132
150
  Dynamic faults are custom faults named after your command. This is especially
@@ -11,6 +11,20 @@ module Lite
11
11
  @object = object
12
12
  end
13
13
 
14
+ def reason
15
+ if object.respond_to?(:reason)
16
+ object.reason
17
+ elsif object.is_a?(StandardError)
18
+ "[#{object.class.name}] #{object.message}".chomp(".")
19
+ else
20
+ object
21
+ end
22
+ end
23
+
24
+ def metadata
25
+ Utils.try(object, :metadata) || command.metadata
26
+ end
27
+
14
28
  def caused_by
15
29
  Utils.try(object, :caused_by) || command
16
30
  end
@@ -21,18 +35,15 @@ module Lite
21
35
  Utils.try(object, :thrown_by) || command.caused_by
22
36
  end
23
37
 
24
- def metadata
25
- Utils.try(object, :metadata) || command.metadata
26
- end
38
+ def fault_exception
39
+ return if command.success?
27
40
 
28
- def reason
29
- if object.respond_to?(:reason)
30
- object.reason
31
- elsif object.is_a?(StandardError)
32
- "[#{object.class.name}] #{object.message}".chomp(".")
33
- else
34
- object
35
- end
41
+ Fault.build(
42
+ command.status.capitalize,
43
+ command,
44
+ object,
45
+ dynamic: command.send(:raise_dynamic_faults?)
46
+ )
36
47
  end
37
48
 
38
49
  end
@@ -59,7 +59,7 @@ module Lite
59
59
  end
60
60
 
61
61
  def bad?(reason = nil)
62
- ![SUCCESS, NOOP].include?(status) && reason?(reason)
62
+ !ok?(reason)
63
63
  end
64
64
 
65
65
  FAULTS.each do |f|
@@ -75,22 +75,27 @@ module Lite
75
75
  str.nil? || str == reason
76
76
  end
77
77
 
78
- def fault(object, status, metadata)
79
- @status = status
80
- @metadata = metadata
78
+ def fault(object, s, m) # rubocop:disable Naming/MethodParameterName
79
+ return if s == SUCCESS || status != SUCCESS
81
80
 
82
- down_stream = Lite::Command::FaultStreamer.new(self, object)
83
- @reason ||= down_stream.reason
84
- @metadata ||= down_stream.metadata
85
- @caused_by ||= down_stream.caused_by
86
- @thrown_by ||= down_stream.thrown_by
81
+ @status = s
82
+ @metadata = m
83
+
84
+ fault_streamer = Lite::Command::FaultStreamer.new(self, object)
85
+ @reason ||= fault_streamer.reason
86
+ @metadata ||= fault_streamer.metadata
87
+ @caused_by ||= fault_streamer.caused_by
88
+ @thrown_by ||= fault_streamer.thrown_by
89
+ @fault_exception ||= fault_streamer.fault_exception
87
90
  end
88
91
 
89
92
  FAULTS.each do |f|
90
93
  # eg: invalid!("idk") or failure!(fault) or error!("idk", { error_key: "some.error" })
91
94
  define_method(:"#{f}!") do |object, metadata = nil|
95
+ return unless success?
96
+
92
97
  fault(object, f, metadata)
93
- raise Lite::Command::Fault.build(f.capitalize, self, object, dynamic: raise_dynamic_faults?)
98
+ raise(fault_exception)
94
99
  end
95
100
  end
96
101
 
@@ -24,13 +24,28 @@ module Lite
24
24
  STATES.each do |s|
25
25
  # eg: executing?
26
26
  define_method(:"#{s}?") { state == s }
27
-
28
- # eg: interrupted!
29
- define_method(:"#{s}!") { @state = s }
30
27
  end
31
28
 
32
29
  private
33
30
 
31
+ def executing!
32
+ return unless pending?
33
+
34
+ @state = EXECUTING
35
+ end
36
+
37
+ def complete!
38
+ return if executed?
39
+
40
+ @state = COMPLETE
41
+ end
42
+
43
+ def interrupted!
44
+ return if executed?
45
+
46
+ @state = INTERRUPTED
47
+ end
48
+
34
49
  def before_execution
35
50
  increment_execution_index
36
51
  assign_execution_cmd_id
@@ -60,8 +75,8 @@ module Lite
60
75
  around_execution { call }
61
76
  Utils.try(self, :on_success)
62
77
  rescue StandardError => e
63
- @exception = e
64
- fault(e, ERROR, metadata) unless e.is_a?(Lite::Command::Fault)
78
+ @original_exception = e
79
+ fault(e, ERROR, metadata)
65
80
  after_execution
66
81
  Utils.try(self, :"on_#{status}", e)
67
82
  ensure
@@ -72,8 +87,8 @@ module Lite
72
87
  around_execution { call }
73
88
  Utils.try(self, :on_success)
74
89
  rescue StandardError => e
75
- @exception = e
76
- fault(e, ERROR, metadata) unless e.is_a?(Lite::Command::Fault)
90
+ @original_exception = e
91
+ fault(e, ERROR, metadata)
77
92
  after_execution
78
93
  Utils.try(self, :"on_#{status}", e)
79
94
  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 :exception }
9
+ base.class_eval { attr_reader :fault_exception, :original_exception }
10
10
  end
11
11
 
12
12
  def caused_by
@@ -33,10 +33,11 @@ module Lite
33
33
  fault? && !caused_fault?
34
34
  end
35
35
 
36
- def raise!
36
+ def raise!(original: false)
37
+ exception = (fault_exception unless original) || original_exception
37
38
  return if exception.nil?
38
39
 
39
- raise exception
40
+ raise(exception)
40
41
  end
41
42
 
42
43
  private
@@ -3,7 +3,7 @@
3
3
  module Lite
4
4
  module Command
5
5
 
6
- VERSION = "3.1.0"
6
+ VERSION = "3.1.2"
7
7
 
8
8
  end
9
9
  end
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.0
4
+ version: 3.1.2
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-25 00:00:00.000000000 Z
11
+ date: 2024-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel