Class: CMDx::Chain

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cmdx/chain.rb

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChain

Creates a new chain with a unique identifier and empty results collection.



42
43
44
45
# File 'lib/cmdx/chain.rb', line 42

def initialize
  @id = Identifier.generate
  @results = []
end

Instance Attribute Details

#idString (readonly)

Returns the unique identifier for this chain.

Examples:

chain.id # => "abc123xyz"

Returns:

  • (String)

    The chain identifier



22
23
24
# File 'lib/cmdx/chain.rb', line 22

def id
  @id
end

#resultsArray<Result> (readonly)

Returns the collection of execution results in this chain.

Examples:

chain.results # => [#<Result>, #<Result>]

Returns:

  • (Array<Result>)

    Array of task results



32
33
34
# File 'lib/cmdx/chain.rb', line 32

def results
  @results
end

Class Method Details

.build(result) ⇒ Chain

Builds or extends the current chain by adding a result. Creates a new chain if none exists, otherwise appends to the current one.

Examples:

result = task.execute
chain = Chain.build(result)
puts "Chain size: #{chain.size}"

Parameters:

  • result (Result)

    The task execution result to add

Returns:

  • (Chain)

    The current chain (newly created or existing)

Raises:

  • (TypeError)

    If result is not a CMDx::Result instance



105
106
107
108
109
110
111
# File 'lib/cmdx/chain.rb', line 105

def build(result)
  raise TypeError, "must be a CMDx::Result" unless result.is_a?(Result)

  self.current ||= new
  current.results << result
  current
end

.clearnil

Clears the current chain for the current thread.

Examples:

Chain.clear

Returns:

  • (nil)

    Always returns nil



86
87
88
# File 'lib/cmdx/chain.rb', line 86

def clear
  Thread.current[THREAD_KEY] = nil
end

.currentChain?

Retrieves the current chain for the current thread.

Examples:

chain = Chain.current
if chain
  puts "Current chain: #{chain.id}"
end

Returns:

  • (Chain, nil)

    The current chain or nil if none exists



60
61
62
# File 'lib/cmdx/chain.rb', line 60

def current
  Thread.current[THREAD_KEY]
end

.current=(chain) ⇒ Chain

Sets the current chain for the current thread.

Examples:

Chain.current = my_chain

Parameters:

  • chain (Chain)

    The chain to set as current

Returns:

  • (Chain)

    The set chain



74
75
76
# File 'lib/cmdx/chain.rb', line 74

def current=(chain)
  Thread.current[THREAD_KEY] = chain
end

Instance Method Details

#to_hHash

Converts the chain to a hash representation.

Examples:

chain_hash = chain.to_h
puts chain_hash[:id]
puts chain_hash[:results].size

Parameters:

  • return (Hash)

    a customizable set of options

Returns:

  • (Hash)

    Hash containing chain id and serialized results



128
129
130
131
132
133
# File 'lib/cmdx/chain.rb', line 128

def to_h
  {
    id: id,
    results: results.map(&:to_h)
  }
end

#to_sString

Converts the chain to a string representation.

Examples:

puts chain.to_s

Returns:

  • (String)

    Formatted string representation of the chain



143
144
145
# File 'lib/cmdx/chain.rb', line 143

def to_s
  Utils::Format.to_str(to_h)
end