Class: Agents::Dispatcher
Direct Known Subclasses
Instance Attribute Summary collapse
-
#agents ⇒ Object
readonly
Returns the value of attribute agents.
Attributes inherited from Agent
Instance Method Summary collapse
-
#agent_for_request(request:) ⇒ Object
Default behavior is a random agent.
- #handle(request:) ⇒ Object
-
#initialize(agents: [], **args) ⇒ Dispatcher
constructor
A new instance of Dispatcher.
- #register(agent) ⇒ Object
- #unregister(agent) ⇒ Object
Constructor Details
#initialize(agents: [], **args) ⇒ Dispatcher
Returns a new instance of Dispatcher.
4 5 6 7 |
# File 'lib/agents/dispatcher.rb', line 4 def initialize(agents: [], **args) super(**args) @agents = agents end |
Instance Attribute Details
#agents ⇒ Object (readonly)
Returns the value of attribute agents.
3 4 5 |
# File 'lib/agents/dispatcher.rb', line 3 def agents @agents end |
Instance Method Details
#agent_for_request(request:) ⇒ Object
Default behavior is a random agent. Use a subclass to specify behavior.
23 24 25 26 27 |
# File 'lib/agents/dispatcher.rb', line 23 def agent_for_request(request:) return UnhandleableRequestAgent.new if agents.empty? @agents.shuffle.first end |
#handle(request:) ⇒ Object
17 18 19 20 |
# File 'lib/agents/dispatcher.rb', line 17 def handle(request:) agent = agent_for_request(request: request) agent.handle(request: request) end |
#register(agent) ⇒ Object
9 10 11 |
# File 'lib/agents/dispatcher.rb', line 9 def register(agent) @agents << agent end |
#unregister(agent) ⇒ Object
13 14 15 |
# File 'lib/agents/dispatcher.rb', line 13 def unregister(agent) @agents.delete(agent) end |