Class: CMDx::Chain
Overview
Manages a collection of task execution results in a thread and fiber safe manner. Chains provide a way to track related task executions and their outcomes within the same execution context.
Constant Summary collapse
- CONCURRENCY_KEY =
:cmdx_chain
Instance Attribute Summary collapse
-
#id ⇒ String
readonly
Returns the unique identifier for this chain.
-
#results ⇒ Array<Result>
readonly
Returns the collection of execution results in this chain.
Class Method Summary collapse
-
.build(result, dry_run: false) ⇒ Chain
Builds or extends the current chain by adding a result.
-
.clear ⇒ nil
Clears the current chain for the current execution context.
-
.current ⇒ Chain?
Retrieves the current chain for the current execution context.
-
.current=(chain) ⇒ Chain
Sets the current chain for the current execution context.
Instance Method Summary collapse
-
#dry_run? ⇒ Boolean
Returns whether the chain is running in dry-run mode.
-
#freeze ⇒ Chain
Freezes the chain and its internal results to prevent modifications.
-
#index(result) ⇒ Integer?
Thread-safe lookup of a result’s position in the chain.
-
#initialize(dry_run: false) ⇒ Chain
constructor
Creates a new chain with a unique identifier and empty results collection.
-
#push(result) ⇒ Array<Result>
Thread-safe append of a result to the chain.
-
#to_h ⇒ Hash
Converts the chain to a hash representation.
-
#to_s ⇒ String
Converts the chain to a string representation.
Constructor Details
#initialize(dry_run: false) ⇒ Chain
Creates a new chain with a unique identifier and empty results collection.
42 43 44 45 46 47 |
# File 'lib/cmdx/chain.rb', line 42 def initialize(dry_run: false) @mutex = Mutex.new @id = Identifier.generate @results = [] @dry_run = !!dry_run end |
Instance Attribute Details
#id ⇒ String (readonly)
Returns the unique identifier for this chain.
22 23 24 |
# File 'lib/cmdx/chain.rb', line 22 def id @id end |
#results ⇒ Array<Result> (readonly)
Returns the collection of execution results in this chain.
32 33 34 |
# File 'lib/cmdx/chain.rb', line 32 def results @results end |
Class Method Details
.build(result, dry_run: false) ⇒ Chain
Builds or extends the current chain by adding a result. Creates a new chain if none exists, otherwise appends to the current one.
107 108 109 110 111 112 113 |
# File 'lib/cmdx/chain.rb', line 107 def build(result, dry_run: false) raise TypeError, "must be a CMDx::Result" unless result.is_a?(Result) self.current ||= new(dry_run:) current.push(result) current end |
.clear ⇒ nil
Clears the current chain for the current execution context.
88 89 90 |
# File 'lib/cmdx/chain.rb', line 88 def clear thread_or_fiber[CONCURRENCY_KEY] = nil end |
.current ⇒ Chain?
Retrieves the current chain for the current execution context.
62 63 64 |
# File 'lib/cmdx/chain.rb', line 62 def current thread_or_fiber[CONCURRENCY_KEY] end |
.current=(chain) ⇒ Chain
Sets the current chain for the current execution context.
76 77 78 |
# File 'lib/cmdx/chain.rb', line 76 def current=(chain) thread_or_fiber[CONCURRENCY_KEY] = chain end |
Instance Method Details
#dry_run? ⇒ Boolean
Returns whether the chain is running in dry-run mode.
164 165 166 |
# File 'lib/cmdx/chain.rb', line 164 def dry_run? !!@dry_run end |
#freeze ⇒ Chain
Freezes the chain and its internal results to prevent modifications.
177 178 179 180 |
# File 'lib/cmdx/chain.rb', line 177 def freeze results.freeze super end |
#index(result) ⇒ Integer?
Thread-safe lookup of a result’s position in the chain.
152 153 154 |
# File 'lib/cmdx/chain.rb', line 152 def index(result) @mutex.synchronize { @results.index(result) } end |
#push(result) ⇒ Array<Result>
Thread-safe append of a result to the chain. Caches the result’s index to avoid repeated O(n) lookups.
138 139 140 141 142 143 |
# File 'lib/cmdx/chain.rb', line 138 def push(result) @mutex.synchronize do result.instance_variable_set(:@chain_index, @results.size) @results << result end end |
#to_h ⇒ Hash
Converts the chain to a hash representation.
195 196 197 198 199 200 201 |
# File 'lib/cmdx/chain.rb', line 195 def to_h { id:, dry_run: dry_run?, results: results.map(&:to_h) } end |