lite-command 3.0.1 → 3.1.1

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: ca95adc16c60158c9a6b826dc9b87f826dc0a81574fd85b996147bbc9ba7c7f2
4
- data.tar.gz: 26d5c68abb7572aa5c887f324fe138c4eb282eb00308dcda83059ace331d2c1b
3
+ metadata.gz: 6239fda55d099d36d3fc4c171ecf6eff2cd56bb5ffc4536dde420da339d0b5c9
4
+ data.tar.gz: a7c3949c9313a479f16f4cf709730f8262576f4d899069240073c52923c9aac3
5
5
  SHA512:
6
- metadata.gz: 73217979c23fff74cf8a9f4965627e3c69bb3d6b74b3b815cb70af3ad766a4ecd412acfb11e96dfc777427eca9dd113c1782e0e407838836d165e1f716f0e241
7
- data.tar.gz: ec3f313e195d97d5c22f8187d78dc0b328288eaa4f2c57de72edce22949b4c75825f63842df7a8141059161efefb1ab45f6457c2b47d2e33efe6fa136b8e7ed7
6
+ metadata.gz: 6330c34c333948919685049c4eaefc5913ab364b09f28322ea6283aa73b3906715cc063b1b61192e231d5582206e0ae91be506fba09035317711dd56ad7f7ae2
7
+ data.tar.gz: 69a682501448fb31fd0ad5709fc23f5f8b5dabc3c9a01486c4848a1d01f887523ab3fee3e7c2acdf6861b181bc1347dbee9bdccd6af3f1dc21321a6585a0a0f4
data/.rubocop.yml CHANGED
@@ -42,8 +42,6 @@ Metrics/PerceivedComplexity:
42
42
  Enabled: false
43
43
  Naming/MemoizedInstanceVariableName:
44
44
  Enabled: false
45
- Performance/AncestorsInclude:
46
- Enabled: false
47
45
  RSpec/AnyInstance:
48
46
  Enabled: false
49
47
  RSpec/DescribedClass:
data/CHANGELOG.md CHANGED
@@ -6,9 +6,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
- ## [3.0.1] - 2024-10-25
9
+ ## [3.1.1] - 2024-10-25
10
+ ### Changed
11
+ - Add option to raise original or fault exception
12
+
13
+ ## [3.1.0] - 2024-10-25
10
14
  ### Added
11
- - Added improved equality check
15
+ - Added `raise!` method to reraise soft call errors
16
+ ### Changed
17
+ - Fixed leakage of configuration option changes in spec
18
+
19
+ ## [3.0.1] - 2024-10-25
20
+ ### Changed
21
+ - Changed equality check for better matching
12
22
 
13
23
  ## [3.0.0] - 2024-10-15
14
24
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lite-command (3.0.1)
4
+ lite-command (3.1.1)
5
5
  activemodel
6
6
  ostruct
7
7
 
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)
@@ -95,7 +96,7 @@ DecryptSecretMessage.call(...)
95
96
  # - or -
96
97
  DecryptSecretMessage.new(...).call
97
98
 
98
- # On success, fault and exception:
99
+ # On success, fault or exception:
99
100
  #=> <DecryptSecretMessage ...>
100
101
  ```
101
102
 
@@ -121,6 +122,29 @@ DecryptSecretMessage.new(...).call!
121
122
  #=> raises StandardError
122
123
  ```
123
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
+
124
148
  ### Dynamic Faults
125
149
 
126
150
  Dynamic faults are custom faults named after your command. This is especially
@@ -30,11 +30,7 @@ module Lite
30
30
  end
31
31
 
32
32
  def self.===(item)
33
- klass0 = respond_to?(:new) ? self : self.class
34
- klass1 = item.respond_to?(:new) ? item : item.class
35
- return true if klass0 == klass1
36
-
37
- klass0.ancestors.include?(klass1) || klass1.ancestors.include?(klass0)
33
+ Utils.descendant_of?(self, item) || Utils.descendant_of?(item, self)
38
34
  end
39
35
 
40
36
  def type
@@ -42,8 +38,7 @@ module Lite
42
38
  end
43
39
 
44
40
  def ===(item)
45
- klass = item.respond_to?(:new) ? item : item.class
46
- is_a?(klass)
41
+ Utils.descendant_of?(self, item)
47
42
  end
48
43
 
49
44
  end
@@ -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
@@ -84,13 +84,15 @@ module Lite
84
84
  @metadata ||= down_stream.metadata
85
85
  @caused_by ||= down_stream.caused_by
86
86
  @thrown_by ||= down_stream.thrown_by
87
+
88
+ @fault_exception ||= down_stream.fault_exception
87
89
  end
88
90
 
89
91
  FAULTS.each do |f|
90
92
  # eg: invalid!("idk") or failure!(fault) or error!("idk", { error_key: "some.error" })
91
93
  define_method(:"#{f}!") do |object, metadata = nil|
92
94
  fault(object, f, metadata)
93
- raise Lite::Command::Fault.build(f.capitalize, self, object, dynamic: raise_dynamic_faults?)
95
+ raise(fault_exception)
94
96
  end
95
97
  end
96
98
 
@@ -60,6 +60,7 @@ module Lite
60
60
  around_execution { call }
61
61
  Utils.try(self, :on_success)
62
62
  rescue StandardError => e
63
+ @original_exception = e
63
64
  fault(e, ERROR, metadata) unless e.is_a?(Lite::Command::Fault)
64
65
  after_execution
65
66
  Utils.try(self, :"on_#{status}", e)
@@ -71,6 +72,7 @@ module Lite
71
72
  around_execution { call }
72
73
  Utils.try(self, :on_success)
73
74
  rescue StandardError => e
75
+ @original_exception = e
74
76
  fault(e, ERROR, metadata) unless e.is_a?(Lite::Command::Fault)
75
77
  after_execution
76
78
  Utils.try(self, :"on_#{status}", e)
@@ -5,6 +5,10 @@ module Lite
5
5
  module Internals
6
6
  module Faults
7
7
 
8
+ def self.included(base)
9
+ base.class_eval { attr_reader :fault_exception, :original_exception }
10
+ end
11
+
8
12
  def caused_by
9
13
  return if success?
10
14
 
@@ -29,6 +33,11 @@ module Lite
29
33
  fault? && !caused_fault?
30
34
  end
31
35
 
36
+ def raise!(original: false)
37
+ exception = (fault_exception unless original) || original_exception
38
+ raise(exception) unless exception.nil?
39
+ end
40
+
32
41
  private
33
42
 
34
43
  def throw!(command)
@@ -6,6 +6,13 @@ module Lite
6
6
 
7
7
  module_function
8
8
 
9
+ def descendant_of?(object, other)
10
+ object_class = object.respond_to?(:new) ? object : object.class
11
+ other_class = other.respond_to?(:new) ? other : other.class
12
+
13
+ !!(object_class <= other_class)
14
+ end
15
+
9
16
  def try(object, method_name, *args, include_private: true)
10
17
  return unless object.respond_to?(method_name, include_private)
11
18
 
@@ -3,7 +3,7 @@
3
3
  module Lite
4
4
  module Command
5
5
 
6
- VERSION = "3.0.1"
6
+ VERSION = "3.1.1"
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.0.1
4
+ version: 3.1.1
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