cmdx 0.4.0 → 0.5.0

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: 00e2b2067ace8ed3afdb75bc07e3f3c5ec3ed40233a63cc3a9abc495c3a3199c
4
- data.tar.gz: 053c7a3a6e3c70808b4dba6d916a35419ee0c858145dbace0d83d89e0583d05a
3
+ metadata.gz: 4564456f58ece0df7606a6eee705e3ed5225b66e8cb1b3feae426ae990204fe4
4
+ data.tar.gz: 412ffb7cfdfbbd71da1c34f2a8bb871f9d9c67b7ef89ab7453af969c4bf6340b
5
5
  SHA512:
6
- metadata.gz: 2de61e9b7e43d9e1187b4c6c925c930ae55163a5075ac624f4a9c29595588b10aacd007d64654397dc458a9c704d9175dd884eadafbef614bb8f01182f022cf0
7
- data.tar.gz: 52dcb48c2e9538d33fea460946fde8f25814127aa490895fc4a676f865423d0b915ad1e7c5daae88ebe46e4bff01a36f850fa704bff312fa938ed5e3a7ea879a
6
+ metadata.gz: 9f416759468491771ec5c1ae8985ceef482287680cd494fc4747adea90a570d894a9725958ffd08167cd9c68e59311ea2f4080997ea1e1edf58db12433fad036
7
+ data.tar.gz: 6e0c6a16bfede183fb61633f55e5bd53bed529b75a2d3fcdbb763885f28d2a614c94abc0cabb178c5743bef99e404648f126c31b0378a0b7da5125d2d8b888a1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.5.0] - 2025-03-21
4
+
5
+ ### Added
6
+ - Add `to_a` alias on array of hashes serializers
7
+ - Add `state`, `status`, `outcome`, and `runtime` to run serializer
8
+ - Add `on_[state]` and `on_[status]` based result callback handlers
9
+ - Add `on_executed` state task hook
10
+ - Add `on_good` and `on_bad` status task hook
11
+ ### Changed
12
+ - Changed status and state hook order
13
+
3
14
  ## [0.4.0] - 2025-03-17
4
15
 
5
16
  ### Added
data/docs/hooks.md CHANGED
@@ -41,13 +41,16 @@ The hook methods support the following options:
41
41
  Hook types are executed in the following order:
42
42
 
43
43
  ```ruby
44
- 1. before_execution
45
- 2. on_executing
46
- 3. before_validation
47
- 4. after_validation
48
- 5. on_[success, skipped, failed]
49
- 6. on_[complete, interrupted]
50
- 7. after_execution
44
+ 0. before_execution
45
+ 1. on_executing
46
+ 2. before_validation
47
+ 3. after_validation
48
+ 4. on_[complete, interrupted]
49
+ 5. on_executed
50
+ 6. on_[success, skipped, failed]
51
+ 7. on_good
52
+ 8. on_bad
53
+ 9. after_execution
51
54
  ```
52
55
 
53
56
  > [!IMPORTANT]
@@ -25,6 +25,27 @@ result.interrupted? #=> false
25
25
  result.executed?
26
26
  ```
27
27
 
28
+ ## Handlers
29
+
30
+ Results can be used to trigger state based callbacks. Handlers require a block
31
+ and will have the result available as local variable. Callback handlers can be
32
+ chained and repeated.
33
+
34
+ ```ruby
35
+ result = ProcessOrderTask.call
36
+ result.on_complete { do_work }
37
+
38
+ # - or -
39
+
40
+ ProcessOrderTask
41
+ .call(...)
42
+ .on_executing { do_work }
43
+ .on_executed { |result| $statsd.increment(result.state) }
44
+ ```
45
+
46
+ > [!TIP]
47
+ > Handlers help execute you logical branches without `if/else` blocks.
48
+
28
49
  ---
29
50
 
30
51
  - **Prev:** [Outcomes - Statuses](https://github.com/drexed/cmdx/blob/main/docs/outcomes/statuses.md)
@@ -27,6 +27,27 @@ result.good? #=> true
27
27
  result.bad? #=> true
28
28
  ```
29
29
 
30
+ ## Handlers
31
+
32
+ Results can be used to trigger status based callbacks. Handlers require a block
33
+ and will have the result available as local variable. Callback handlers can be
34
+ chained and repeated.
35
+
36
+ ```ruby
37
+ result = ProcessOrderTask.call
38
+ result.on_success { do_work }
39
+
40
+ # - or -
41
+
42
+ ProcessOrderTask
43
+ .call(...)
44
+ .on_success { do_work }
45
+ .on_bad { |result| $statsd.increment(result.state) }
46
+ ```
47
+
48
+ > [!TIP]
49
+ > Handlers help execute you logical branches without `if/else` blocks.
50
+
30
51
  ---
31
52
 
32
53
  - **Prev:** [Outcomes - Result](https://github.com/drexed/cmdx/blob/main/docs/outcomes/result.md)
@@ -18,6 +18,7 @@ module CMDx
18
18
  def to_h
19
19
  ParametersSerializer.call(self)
20
20
  end
21
+ alias to_a to_h
21
22
 
22
23
  def to_s
23
24
  ParametersInspector.call(self)
data/lib/cmdx/result.rb CHANGED
@@ -26,6 +26,14 @@ module CMDx
26
26
  STATES.each do |s|
27
27
  # eg: executing?
28
28
  define_method(:"#{s}?") { state == s }
29
+
30
+ # eg: on_interrupted { ... }
31
+ define_method(:"on_#{s}") do |&block|
32
+ raise ArgumentError, "a block is required" unless block
33
+
34
+ block.call(self) if send(:"#{s}?")
35
+ self
36
+ end
29
37
  end
30
38
 
31
39
  def executed!
@@ -36,6 +44,13 @@ module CMDx
36
44
  complete? || interrupted?
37
45
  end
38
46
 
47
+ def on_executed(&)
48
+ raise ArgumentError, "a block is required" unless block_given?
49
+
50
+ yield(self) if executed?
51
+ self
52
+ end
53
+
39
54
  def executing!
40
55
  return if executing?
41
56
 
@@ -69,16 +84,38 @@ module CMDx
69
84
  STATUSES.each do |s|
70
85
  # eg: skipped?
71
86
  define_method(:"#{s}?") { status == s }
87
+
88
+ # eg: on_failed { ... }
89
+ define_method(:"on_#{s}") do |&block|
90
+ raise ArgumentError, "a block is required" unless block
91
+
92
+ block.call(self) if send(:"#{s}?")
93
+ self
94
+ end
72
95
  end
73
96
 
74
97
  def good?
75
98
  !failed?
76
99
  end
77
100
 
101
+ def on_good(&)
102
+ raise ArgumentError, "a block is required" unless block_given?
103
+
104
+ yield(self) if good?
105
+ self
106
+ end
107
+
78
108
  def bad?
79
109
  !success?
80
110
  end
81
111
 
112
+ def on_bad(&)
113
+ raise ArgumentError, "a block is required" unless block_given?
114
+
115
+ yield(self) if bad?
116
+ self
117
+ end
118
+
82
119
  def skip!(**metadata)
83
120
  return if skipped?
84
121
 
data/lib/cmdx/run.rb CHANGED
@@ -21,6 +21,7 @@ module CMDx
21
21
  def to_h
22
22
  RunSerializer.call(self)
23
23
  end
24
+ alias to_a to_h
24
25
 
25
26
  def to_s
26
27
  RunInspector.call(self)
@@ -8,6 +8,10 @@ module CMDx
8
8
  def call(run)
9
9
  {
10
10
  id: run.id,
11
+ state: run.state,
12
+ status: run.status,
13
+ outcome: run.outcome,
14
+ runtime: run.runtime,
11
15
  results: run.results.map(&:to_h)
12
16
  }
13
17
  end
data/lib/cmdx/task.rb CHANGED
@@ -8,6 +8,9 @@ module CMDx
8
8
  :after_validation,
9
9
  :before_execution,
10
10
  :after_execution,
11
+ :on_executed,
12
+ :on_good,
13
+ :on_bad,
11
14
  *Result::STATUSES.map { |s| :"on_#{s}" },
12
15
  *Result::STATES.map { |s| :"on_#{s}" }
13
16
  ].freeze
@@ -103,8 +106,12 @@ module CMDx
103
106
  end
104
107
 
105
108
  def after_call
106
- TaskHook.call(self, :"on_#{result.status}")
107
109
  TaskHook.call(self, :"on_#{result.state}")
110
+ TaskHook.call(self, :on_executed) if result.executed?
111
+
112
+ TaskHook.call(self, :"on_#{result.status}")
113
+ TaskHook.call(self, :on_good) if result.good?
114
+ TaskHook.call(self, :on_bad) if result.bad?
108
115
 
109
116
  TaskHook.call(self, :after_execution)
110
117
  end
data/lib/cmdx/version.rb CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  module CMDx
4
4
 
5
- VERSION = "0.4.0"
5
+ VERSION = "0.5.0"
6
6
 
7
7
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cmdx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-03-17 00:00:00.000000000 Z
10
+ date: 2025-03-21 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: bigdecimal