Class: CMDx::Chain
Overview
Manages a collection of task execution results in a thread-safe manner. Chains provide a way to track related task executions and their outcomes within the same execution context.
Constant Summary collapse
- THREAD_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 thread.
-
.current ⇒ Chain?
Retrieves the current chain for the current thread.
-
.current=(chain) ⇒ Chain
Sets the current chain for the current thread.
Instance Method Summary collapse
-
#dry_run? ⇒ Boolean
Returns whether the chain is running in dry-run mode.
-
#initialize(dry_run: false) ⇒ Chain
constructor
Creates a new chain with a unique identifier and empty results collection.
-
#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 |
# File 'lib/cmdx/chain.rb', line 42 def initialize(dry_run: false) @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.
106 107 108 109 110 111 112 |
# File 'lib/cmdx/chain.rb', line 106 def build(result, dry_run: false) raise TypeError, "must be a CMDx::Result" unless result.is_a?(Result) self.current ||= new(dry_run:) current.results << result current end |
.clear ⇒ nil
Clears the current chain for the current thread.
87 88 89 |
# File 'lib/cmdx/chain.rb', line 87 def clear Thread.current[THREAD_KEY] = nil end |
.current ⇒ Chain?
Retrieves the current chain for the current thread.
61 62 63 |
# File 'lib/cmdx/chain.rb', line 61 def current Thread.current[THREAD_KEY] end |
.current=(chain) ⇒ Chain
Sets the current chain for the current thread.
75 76 77 |
# File 'lib/cmdx/chain.rb', line 75 def current=(chain) Thread.current[THREAD_KEY] = chain end |
Instance Method Details
#dry_run? ⇒ Boolean
Returns whether the chain is running in dry-run mode.
124 125 126 |
# File 'lib/cmdx/chain.rb', line 124 def dry_run? !!@dry_run end |
#to_h ⇒ Hash
Converts the chain to a hash representation.
141 142 143 144 145 146 147 |
# File 'lib/cmdx/chain.rb', line 141 def to_h { id:, dry_run: dry_run?, results: results.map(&:to_h) } end |