cmdx 1.7.3 → 1.7.5
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 +15 -1
- data/lib/cmdx/context.rb +20 -0
- data/lib/cmdx/errors.rb +25 -3
- data/lib/cmdx/result.rb +8 -3
- data/lib/cmdx/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0238294eeb6dd614280c7c386832d6780ea4005879c1162e7cacc4f97c54d5fd'
|
4
|
+
data.tar.gz: 50c49904dbc84ffa7451a585e443c061b71576498ad00504ee87ae2efff07945
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7bac1596b901e86412d241f81c2c5fa331d2bf05b63c0ef3ee3691dd832c30fa8a187e6d8d4788fe9f3d1760bb8f7b1da62c2701a9a86bac6d9a4b5116f3e490
|
7
|
+
data.tar.gz: 8794c3049d206178d68a4e9446d001668ee8b3a7902569dd1f66d05fe10b67f9cbc59e5264286ebfb075681415f430a3d69c40f181fdbe6f541caf4ec7fb4e56
|
data/CHANGELOG.md
CHANGED
@@ -4,7 +4,21 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
-
## [
|
7
|
+
## [UNRELEASED]
|
8
|
+
|
9
|
+
## [1.7.5] - 2025-09-10
|
10
|
+
|
11
|
+
### Added
|
12
|
+
- Added `fetch_or_store` method to context
|
13
|
+
- Added `ctx` alias for context in result
|
14
|
+
- Added `ok?` alias for `good?` in result
|
15
|
+
- Added deconstruction values in result
|
16
|
+
|
17
|
+
## [1.7.4] - 2025-09-03
|
18
|
+
|
19
|
+
### Added
|
20
|
+
- Added errors delegation from result object
|
21
|
+
- Added `full_messages` and `to_hash` methods to errors
|
8
22
|
|
9
23
|
## [1.7.3] - 2025-09-03
|
10
24
|
|
data/lib/cmdx/context.rb
CHANGED
@@ -108,6 +108,26 @@ module CMDx
|
|
108
108
|
table.fetch(key.to_sym, ...)
|
109
109
|
end
|
110
110
|
|
111
|
+
# Fetches a value from the context by key, or stores and returns a default value if not found.
|
112
|
+
#
|
113
|
+
# @param key [String, Symbol] the key to fetch or store
|
114
|
+
# @param value [Object] the default value to store if key is not found
|
115
|
+
#
|
116
|
+
# @yield [key] a block to compute the default value to store
|
117
|
+
#
|
118
|
+
# @return [Object] the existing value if key is found, otherwise the stored default value
|
119
|
+
#
|
120
|
+
# @example
|
121
|
+
# context = Context.new(name: "John")
|
122
|
+
# context.fetch_or_store(:name, "Default") # => "John" (existing value)
|
123
|
+
# context.fetch_or_store(:age, 25) # => 25 (stored and returned)
|
124
|
+
# context.fetch_or_store(:city) { |key| "Unknown #{key}" } # => "Unknown city" (stored and returned)
|
125
|
+
def fetch_or_store(key, value = nil)
|
126
|
+
table.fetch(key.to_sym) do
|
127
|
+
table[key.to_sym] = block_given? ? yield : value
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
111
131
|
# Merges the given arguments into the current context, modifying it in place.
|
112
132
|
#
|
113
133
|
# @param args [Hash, Object] arguments to merge into the context
|
data/lib/cmdx/errors.rb
CHANGED
@@ -48,6 +48,18 @@ module CMDx
|
|
48
48
|
!messages[attribute].empty?
|
49
49
|
end
|
50
50
|
|
51
|
+
# Convert errors to a hash format with arrays of fullmessages.
|
52
|
+
#
|
53
|
+
# @return [Hash{Symbol => Array<String>}] Hash with attribute keys and message arrays
|
54
|
+
#
|
55
|
+
# @example
|
56
|
+
# errors.full_messages # => { email: ["email must be valid format", "email cannot be blank"] }
|
57
|
+
def full_messages
|
58
|
+
messages.each_with_object({}) do |(attribute, messages), hash|
|
59
|
+
hash[attribute] = messages.map { |message| "#{attribute} #{message}" }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
51
63
|
# Convert errors to a hash format with arrays of messages.
|
52
64
|
#
|
53
65
|
# @return [Hash{Symbol => Array<String>}] Hash with attribute keys and message arrays
|
@@ -58,6 +70,18 @@ module CMDx
|
|
58
70
|
messages.transform_values(&:to_a)
|
59
71
|
end
|
60
72
|
|
73
|
+
# Convert errors to a hash format with optional full messages.
|
74
|
+
#
|
75
|
+
# @param full [Boolean] Whether to include full messages with attribute names
|
76
|
+
# @return [Hash{Symbol => Array<String>}] Hash with attribute keys and message arrays
|
77
|
+
#
|
78
|
+
# @example
|
79
|
+
# errors.to_hash # => { email: ["must be valid format", "cannot be blank"] }
|
80
|
+
# errors.to_hash(true) # => { email: ["email must be valid format", "email cannot be blank"] }
|
81
|
+
def to_hash(full = false)
|
82
|
+
full ? full_messages : to_h
|
83
|
+
end
|
84
|
+
|
61
85
|
# Convert errors to a human-readable string format.
|
62
86
|
#
|
63
87
|
# @return [String] Formatted error messages joined with periods
|
@@ -65,9 +89,7 @@ module CMDx
|
|
65
89
|
# @example
|
66
90
|
# errors.to_s # => "email must be valid format. email cannot be blank"
|
67
91
|
def to_s
|
68
|
-
|
69
|
-
messages.each { |message| memo << "#{attribute} #{message}" }
|
70
|
-
end.join(". ")
|
92
|
+
full_messages.values.flatten.join(". ")
|
71
93
|
end
|
72
94
|
|
73
95
|
end
|
data/lib/cmdx/result.rb
CHANGED
@@ -33,7 +33,8 @@ module CMDx
|
|
33
33
|
|
34
34
|
attr_reader :task, :state, :status, :metadata, :reason, :cause
|
35
35
|
|
36
|
-
def_delegators :task, :context, :chain
|
36
|
+
def_delegators :task, :context, :chain, :errors
|
37
|
+
alias ctx context
|
37
38
|
|
38
39
|
# @param task [CMDx::Task] The task instance this result represents
|
39
40
|
#
|
@@ -185,6 +186,7 @@ module CMDx
|
|
185
186
|
def good?
|
186
187
|
!failed?
|
187
188
|
end
|
189
|
+
alias ok? good?
|
188
190
|
|
189
191
|
# @param block [Proc] Block to execute conditionally
|
190
192
|
#
|
@@ -430,13 +432,13 @@ module CMDx
|
|
430
432
|
|
431
433
|
# @param keys [Array] Array of keys to deconstruct
|
432
434
|
#
|
433
|
-
# @return [Array] Array containing state and
|
435
|
+
# @return [Array] Array containing state, status, reason, cause, and metadata
|
434
436
|
#
|
435
437
|
# @example
|
436
438
|
# state, status = result.deconstruct
|
437
439
|
# puts "State: #{state}, Status: #{status}"
|
438
440
|
def deconstruct(*)
|
439
|
-
[state, status]
|
441
|
+
[state, status, reason, cause, metadata]
|
440
442
|
end
|
441
443
|
|
442
444
|
# @param keys [Array] Array of keys to deconstruct
|
@@ -454,7 +456,10 @@ module CMDx
|
|
454
456
|
{
|
455
457
|
state: state,
|
456
458
|
status: status,
|
459
|
+
reason: reason,
|
460
|
+
cause: cause,
|
457
461
|
metadata: metadata,
|
462
|
+
outcome: outcome,
|
458
463
|
executed: executed?,
|
459
464
|
good: good?,
|
460
465
|
bad: bad?
|
data/lib/cmdx/version.rb
CHANGED