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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1a841431a363de96b59d32abaa1bc733c04046a5d2f33b1c544e85b8b89cdcd9
4
- data.tar.gz: 11dd1cd8c8e19cc8d58a631f94f647eb4d7a6b2effe2591d04eaea4c693ecdee
3
+ metadata.gz: b21cf35a72b7a37760d250919ee052e3800bfe09083709e9d9f65593612d8826
4
+ data.tar.gz: b2b55e457b95ce878aab140537ba1f28aa5310c4e257d0e2b11948ed9ee7f9c2
5
5
  SHA512:
6
- metadata.gz: ae2440d1e33ddf601c42996a513f66771fb06af24a6f60534329b3337335bb714e3be2e9fd5b43d5f71e26ff8d2bc5bbe7bc9a07d275b259491858f6f8864e9b
7
- data.tar.gz: 873edb0d145a80033180e0a33072dd80da38512511d718af17e4e91ddc65d90118bd63d8a341d22278a1d026751b0cf867df708dafde21be54fc54ef0c821f47
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lite-command (2.0.2)
4
+ lite-command (2.0.3)
5
5
  ostruct
6
6
 
7
7
  GEM
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 `dnf` if a fault is thrown by a
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
- - `dnf`
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.
@@ -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
- class #{base}::Fault < Lite::Command::Fault; end
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
@@ -5,22 +5,18 @@ module Lite
5
5
 
6
6
  class Fault < StandardError
7
7
 
8
- attr_reader :caused_by, :thrown_by, :reason
8
+ attr_reader :reason, :caused_by, :thrown_by
9
9
 
10
- def initialize(caused_by, thrown_by, reason)
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 fault_name
23
- @fault_name ||= fault_klass.downcase
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 = "noop",
8
+ NOOP = "noop",
9
9
  INVALID = "invalid",
10
10
  FAILURE = "failure",
11
- ERROR = "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(fault_or_string)
61
- define_method(:"#{f}") do |fault_or_string|
62
- derive_fault_from(fault_or_string)
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!(fault_or_string)
67
- define_method(:"#{f}!") do |fault_or_string|
68
- send(:"#{f}", fault_or_string)
69
- raise_fault(Lite::Command.const_get(f.capitalize), fault_or_string)
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 = "pending",
8
- EXECUTING = "executing",
9
- COMPLETE = "complete",
10
- DNF = "dnf"
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
- fn = e.respond_to?(:fault_name) ? e.fault_name : ERROR
19
+ f = e.respond_to?(:type) ? e.type : ERROR
20
20
 
21
- send(:"#{fn}", e)
21
+ send(:"#{f}", e)
22
22
  after_execution
23
- send(:"on_#{fn}", e)
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
- dnf? || complete?
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: dnf!
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? ? dnf! : complete!
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(fault_or_string)
41
- (fault_or_string.caused_by if fault_or_string.respond_to?(:caused_by)) || self
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(fault_or_string)
45
- if fault_or_string.respond_to?(:executed?) && fault_or_string.executed?
46
- fault_or_string
44
+ def derive_thrown_by_from(object)
45
+ if object.respond_to?(:executed?) && object.executed?
46
+ object
47
47
  else
48
- (fault_or_string.thrown_by if fault_or_string.respond_to?(:thrown_by)) || caused_by
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(fault_or_string)
53
- if fault_or_string.respond_to?(:reason)
54
- fault_or_string.reason
55
- elsif fault_or_string.respond_to?(:message)
56
- "[#{fault_or_string.class.name}] #{fault_or_string.message}".chomp(".")
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
- fault_or_string
58
+ object
59
59
  end
60
60
  end
61
61
 
62
- def derive_fault_from(fault_or_string)
63
- @caused_by ||= derive_caused_by_from(fault_or_string)
64
- @thrown_by ||= derive_thrown_by_from(fault_or_string)
65
- @reason ||= derive_reason_from(fault_or_string)
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
@@ -3,7 +3,7 @@
3
3
  module Lite
4
4
  module Command
5
5
 
6
- VERSION = "2.0.2"
6
+ VERSION = "2.0.3"
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: 2.0.2
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-29 00:00:00.000000000 Z
11
+ date: 2024-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ostruct