Class: LangGraphRB::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/langgraph_rb/edge.rb

Overview

Helper class for building conditional routing

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



106
107
108
# File 'lib/langgraph_rb/edge.rb', line 106

def initialize
  @conditions = []
end

Class Method Details

.build(&block) ⇒ Object



102
103
104
# File 'lib/langgraph_rb/edge.rb', line 102

def self.build(&block)
  new.tap { |r| r.instance_eval(&block) }
end

Instance Method Details

#call(state, context = nil) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/langgraph_rb/edge.rb', line 120

def call(state, context = nil)
  @conditions.each do |condition, destination|
    result = case condition.arity
            when 0
              condition.call
            when 1
              condition.call(state)
            else
              condition.call(state, context)
            end
    
    return destination if result
  end

  @default || raise("No matching condition and no default specified")
end

#otherwise(destination) ⇒ Object



115
116
117
118
# File 'lib/langgraph_rb/edge.rb', line 115

def otherwise(destination)
  @default = destination
  self
end

#to_procObject



137
138
139
# File 'lib/langgraph_rb/edge.rb', line 137

def to_proc
  method(:call).to_proc
end

#when(condition, destination) ⇒ Object



110
111
112
113
# File 'lib/langgraph_rb/edge.rb', line 110

def when(condition, destination)
  @conditions << [condition, destination]
  self
end