lite-command 2.0.2 → 2.0.3
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 +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +1 -1
- data/README.md +2 -2
- data/lib/lite/command/base.rb +8 -9
- data/lib/lite/command/fault.rb +5 -9
- data/lib/lite/command/internals/callable.rb +9 -9
- data/lib/lite/command/internals/executable.rb +11 -14
- data/lib/lite/command/internals/faultable.rb +24 -29
- data/lib/lite/command/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b21cf35a72b7a37760d250919ee052e3800bfe09083709e9d9f65593612d8826
|
4
|
+
data.tar.gz: b2b55e457b95ce878aab140537ba1f28aa5310c4e257d0e2b11948ed9ee7f9c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8ab004280e1c00026dacc92a7d652dc2859c68f6648e31210ccfa1c16a1aba9ba31e57843910bf90d4b94677e8cd8c4c651989c2e2f1983556f33ff786e6514
|
7
|
+
data.tar.gz: aed6da43c95926071114ef9692b17c4fa2b5a3215629e473c8bd70a552fdd624f189095cfa1744da6aa427cbcf80cfb6adb6db81c5b754f4ff81bfee500d046d
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [2.0.3] - 2024-09-30
|
10
|
+
### Changed
|
11
|
+
- Simplify error building
|
12
|
+
- Reduced recalling error since we can just throw it once
|
13
|
+
- Rename `fault_name` to `type`
|
14
|
+
|
9
15
|
## [2.0.2] - 2024-09-29
|
10
16
|
### Added
|
11
17
|
- faultable module
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -94,7 +94,7 @@ command.context.result #=> 8
|
|
94
94
|
|
95
95
|
#### States
|
96
96
|
State represents the state of the executable code. Once `execute`
|
97
|
-
is ran, it will always `complete` or `
|
97
|
+
is ran, it will always `complete` or `interrupted` if a fault is thrown by a
|
98
98
|
child command.
|
99
99
|
|
100
100
|
- `pending`
|
@@ -103,7 +103,7 @@ child command.
|
|
103
103
|
- Command objects actively executing code.
|
104
104
|
- `complete`
|
105
105
|
- Command objects that executed to completion.
|
106
|
-
- `
|
106
|
+
- `interrupted`
|
107
107
|
- Command objects that could NOT be executed to completion.
|
108
108
|
This could be as a result of a fault/exception on the
|
109
109
|
object itself or one of its children.
|
data/lib/lite/command/base.rb
CHANGED
@@ -13,16 +13,15 @@ module Lite
|
|
13
13
|
base.include Lite::Command::Internals::Resultable
|
14
14
|
|
15
15
|
base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
16
|
-
# eg: Users::ResetPassword::Fault
|
17
|
-
|
16
|
+
# eg: Users::ResetPassword::Fault < Lite::Command::Fault
|
17
|
+
#{base}::Fault = Class.new(Lite::Command::Fault)
|
18
|
+
|
19
|
+
# eg: Users::ResetPassword::Noop < Users::ResetPassword::Fault
|
20
|
+
#{base}::Noop = Class.new(#{base}::Fault)
|
21
|
+
#{base}::Invalid = Class.new(#{base}::Fault)
|
22
|
+
#{base}::Failure = Class.new(#{base}::Fault)
|
23
|
+
#{base}::Error = Class.new(#{base}::Fault)
|
18
24
|
RUBY
|
19
|
-
|
20
|
-
FAULTS.each do |f|
|
21
|
-
base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
22
|
-
# eg: Users::ResetPassword::Noop < Users::ResetPassword::Fault
|
23
|
-
class #{base}::#{f.capitalize} < #{base}::Fault; end
|
24
|
-
RUBY
|
25
|
-
end
|
26
25
|
end
|
27
26
|
|
28
27
|
attr_reader :context
|
data/lib/lite/command/fault.rb
CHANGED
@@ -5,22 +5,18 @@ module Lite
|
|
5
5
|
|
6
6
|
class Fault < StandardError
|
7
7
|
|
8
|
-
attr_reader :
|
8
|
+
attr_reader :reason, :caused_by, :thrown_by
|
9
9
|
|
10
|
-
def initialize(caused_by, thrown_by
|
10
|
+
def initialize(reason, caused_by, thrown_by)
|
11
11
|
super(reason)
|
12
12
|
|
13
|
+
@reason = reason
|
13
14
|
@caused_by = caused_by
|
14
15
|
@thrown_by = thrown_by
|
15
|
-
@reason = reason
|
16
|
-
end
|
17
|
-
|
18
|
-
def fault_klass
|
19
|
-
@fault_klass ||= self.class.name.split("::").last
|
20
16
|
end
|
21
17
|
|
22
|
-
def
|
23
|
-
@
|
18
|
+
def type
|
19
|
+
@type ||= self.class.name.split("::").last.downcase
|
24
20
|
end
|
25
21
|
|
26
22
|
end
|
@@ -5,10 +5,10 @@ module Lite
|
|
5
5
|
|
6
6
|
STATUSES = [
|
7
7
|
SUCCESS = "success",
|
8
|
-
NOOP
|
8
|
+
NOOP = "noop",
|
9
9
|
INVALID = "invalid",
|
10
10
|
FAILURE = "failure",
|
11
|
-
ERROR
|
11
|
+
ERROR = "error"
|
12
12
|
].freeze
|
13
13
|
FAULTS = (STATUSES - [SUCCESS]).freeze
|
14
14
|
|
@@ -57,16 +57,16 @@ module Lite
|
|
57
57
|
private
|
58
58
|
|
59
59
|
FAULTS.each do |f|
|
60
|
-
# eg: error(
|
61
|
-
define_method(:"#{f}") do |
|
62
|
-
derive_fault_from(
|
60
|
+
# eg: error(object)
|
61
|
+
define_method(:"#{f}") do |object|
|
62
|
+
derive_fault_from(object)
|
63
63
|
@status = f
|
64
64
|
end
|
65
65
|
|
66
|
-
# eg: invalid!(
|
67
|
-
define_method(:"#{f}!") do |
|
68
|
-
send(:"#{f}",
|
69
|
-
|
66
|
+
# eg: invalid!(object)
|
67
|
+
define_method(:"#{f}!") do |object|
|
68
|
+
send(:"#{f}", object)
|
69
|
+
raise fault(f.capitalize, object)
|
70
70
|
end
|
71
71
|
|
72
72
|
# eg: on_noop(exception)
|
@@ -4,10 +4,10 @@ module Lite
|
|
4
4
|
module Command
|
5
5
|
|
6
6
|
STATES = [
|
7
|
-
PENDING
|
8
|
-
EXECUTING
|
9
|
-
COMPLETE
|
10
|
-
|
7
|
+
PENDING = "pending",
|
8
|
+
EXECUTING = "executing",
|
9
|
+
COMPLETE = "complete",
|
10
|
+
INTERRUPTED = "interrupted"
|
11
11
|
].freeze
|
12
12
|
|
13
13
|
module Internals
|
@@ -16,21 +16,18 @@ module Lite
|
|
16
16
|
def execute
|
17
17
|
around_execution { call }
|
18
18
|
rescue StandardError => e
|
19
|
-
|
19
|
+
f = e.respond_to?(:type) ? e.type : ERROR
|
20
20
|
|
21
|
-
send(:"#{
|
21
|
+
send(:"#{f}", e)
|
22
22
|
after_execution
|
23
|
-
send(:"on_#{
|
23
|
+
send(:"on_#{f}", e)
|
24
24
|
end
|
25
25
|
|
26
26
|
def execute!
|
27
27
|
around_execution { call }
|
28
28
|
rescue StandardError => e
|
29
29
|
after_execution
|
30
|
-
|
31
|
-
raise(e) unless raise_dynamic_faults? && e.is_a?(Lite::Command::Fault)
|
32
|
-
|
33
|
-
raise_dynamic_fault(e)
|
30
|
+
raise(e)
|
34
31
|
end
|
35
32
|
|
36
33
|
def state
|
@@ -38,14 +35,14 @@ module Lite
|
|
38
35
|
end
|
39
36
|
|
40
37
|
def executed?
|
41
|
-
|
38
|
+
complete? || interrupted?
|
42
39
|
end
|
43
40
|
|
44
41
|
STATES.each do |s|
|
45
42
|
# eg: executing?
|
46
43
|
define_method(:"#{s}?") { state == s }
|
47
44
|
|
48
|
-
# eg:
|
45
|
+
# eg: interrupted!
|
49
46
|
define_method(:"#{s}!") { @state = s }
|
50
47
|
end
|
51
48
|
|
@@ -60,7 +57,7 @@ module Lite
|
|
60
57
|
end
|
61
58
|
|
62
59
|
def after_execution
|
63
|
-
fault? ?
|
60
|
+
fault? ? interrupted! : complete!
|
64
61
|
on_after_execution
|
65
62
|
stop_monotonic_time
|
66
63
|
append_execution_result
|
@@ -37,51 +37,46 @@ module Lite
|
|
37
37
|
send(:"#{command.status}!", command)
|
38
38
|
end
|
39
39
|
|
40
|
-
def derive_caused_by_from(
|
41
|
-
(
|
40
|
+
def derive_caused_by_from(object)
|
41
|
+
(object.caused_by if object.respond_to?(:caused_by)) || self
|
42
42
|
end
|
43
43
|
|
44
|
-
def derive_thrown_by_from(
|
45
|
-
if
|
46
|
-
|
44
|
+
def derive_thrown_by_from(object)
|
45
|
+
if object.respond_to?(:executed?) && object.executed?
|
46
|
+
object
|
47
47
|
else
|
48
|
-
(
|
48
|
+
(object.thrown_by if object.respond_to?(:thrown_by)) || caused_by
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
def derive_reason_from(
|
53
|
-
if
|
54
|
-
|
55
|
-
elsif
|
56
|
-
"[#{
|
52
|
+
def derive_reason_from(object)
|
53
|
+
if object.respond_to?(:reason)
|
54
|
+
object.reason
|
55
|
+
elsif object.respond_to?(:message)
|
56
|
+
"[#{object.class.name}] #{object.message}".chomp(".")
|
57
57
|
else
|
58
|
-
|
58
|
+
object
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
-
def derive_fault_from(
|
63
|
-
@caused_by ||= derive_caused_by_from(
|
64
|
-
@thrown_by ||= derive_thrown_by_from(
|
65
|
-
@reason ||= derive_reason_from(
|
66
|
-
end
|
67
|
-
|
68
|
-
# eg: Lite::Command::Noop.new(...)
|
69
|
-
def raise_fault(klass, thrower)
|
70
|
-
exception = klass.new(caused_by, self, reason)
|
71
|
-
exception.set_backtrace(thrower.backtrace) if thrower.respond_to?(:backtrace)
|
72
|
-
raise(exception)
|
73
|
-
end
|
74
|
-
|
75
|
-
# eg: Users::ResetPassword::Noop.new(...)
|
76
|
-
def raise_dynamic_fault(exception)
|
77
|
-
fault_klass = self.class.const_get(exception.fault_klass)
|
78
|
-
raise_fault(fault_klass, exception)
|
62
|
+
def derive_fault_from(object)
|
63
|
+
@caused_by ||= derive_caused_by_from(object)
|
64
|
+
@thrown_by ||= derive_thrown_by_from(object)
|
65
|
+
@reason ||= derive_reason_from(object)
|
79
66
|
end
|
80
67
|
|
81
68
|
def raise_dynamic_faults?
|
82
69
|
false
|
83
70
|
end
|
84
71
|
|
72
|
+
# eg: Lite::Command::Noop.new(...) or Users::ResetPassword::Noop.new(...)
|
73
|
+
def fault(type, thrower)
|
74
|
+
klass = raise_dynamic_faults? ? self.class : Lite::Command
|
75
|
+
fault = klass.const_get(type.to_s).new(reason, caused_by, self)
|
76
|
+
fault.set_backtrace(thrower.backtrace) if thrower.respond_to?(:backtrace)
|
77
|
+
fault
|
78
|
+
end
|
79
|
+
|
85
80
|
end
|
86
81
|
end
|
87
82
|
end
|
data/lib/lite/command/version.rb
CHANGED
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: 2.0.
|
4
|
+
version: 2.0.3
|
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-09-
|
11
|
+
date: 2024-09-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ostruct
|