blob: 571ca1bb2d244c67784f81cc3c5229f2e2fab0a5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# frozen_string_literal: true
module Dispatch
module Tools
class Registry
def initialize
@tools = {}
end
def register(tool_definition)
name = tool_definition.name
raise DuplicateToolError, "Tool '#{name}' is already registered" if @tools.key?(name)
@tools[name] = tool_definition
self
end
def get(name)
@tools[name]
end
def fetch(name)
@tools.fetch(name) do
raise ToolNotFoundError, "Tool '#{name}' not found"
end
end
def has?(name)
@tools.key?(name)
end
def tools
@tools.values
end
def tool_names
@tools.keys
end
def to_a
@tools.values.map(&:to_tool_definition)
end
def subset(*names)
new_registry = self.class.new
names.each do |name|
new_registry.register(fetch(name))
end
new_registry
end
def size
@tools.size
end
def empty?
@tools.empty?
end
end
end
end
|