Class: RobotLab::ToolManifest
- Inherits:
-
Object
- Object
- RobotLab::ToolManifest
- Includes:
- Enumerable
- Defined in:
- lib/robot_lab/tool_manifest.rb
Overview
Registry of tools with lookup by name
ToolManifest provides a collection interface for managing multiple tools, with methods for lookup, iteration, and conversion to various formats.
Class Method Summary collapse
-
.from_hash(hash) ⇒ ToolManifest
Create manifest from hash of tool definitions.
Instance Method Summary collapse
-
#[](name) ⇒ Tool?
Get a tool by name.
-
#add(tool) ⇒ self
(also: #<<)
Add a tool to the manifest.
-
#clear ⇒ self
Clear all tools.
-
#each {|Tool| ... } ⇒ Object
Iterate over tools.
-
#empty? ⇒ Boolean
Check if manifest is empty.
-
#fetch(name) ⇒ Tool
Get a tool by name, raising if not found.
-
#include?(name) ⇒ Boolean
(also: #has?)
Check if a tool exists.
-
#initialize(tools = []) ⇒ ToolManifest
constructor
Creates a new ToolManifest instance.
-
#merge(other) ⇒ self
Merge another manifest or array of tools.
-
#names ⇒ Array<String>
Get all tool names.
-
#remove(name) ⇒ Tool?
Remove a tool from the manifest.
-
#replace(tools) ⇒ self
Replace all tools.
-
#size ⇒ Integer
(also: #count, #length)
Number of tools.
-
#to_h ⇒ Hash<String, Hash>
Converts the manifest to a hash representation.
-
#to_json(*args) ⇒ String
Converts the manifest to JSON.
-
#values ⇒ Array<Tool>
(also: #all, #to_a)
Get all tools.
Constructor Details
#initialize(tools = []) ⇒ ToolManifest
Creates a new ToolManifest instance.
23 24 25 26 |
# File 'lib/robot_lab/tool_manifest.rb', line 23 def initialize(tools = []) @tools = {} Array(tools).each { |tool| add(tool) } end |
Class Method Details
.from_hash(hash) ⇒ ToolManifest
Create manifest from hash of tool definitions
206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/robot_lab/tool_manifest.rb', line 206 def self.from_hash(hash) tools = hash.map do |name, config| Tool.create( name: name, description: config[:description], parameters: config[:parameters], &config[:handler] ) end new(tools) end |
Instance Method Details
#[](name) ⇒ Tool?
Get a tool by name
58 59 60 |
# File 'lib/robot_lab/tool_manifest.rb', line 58 def [](name) @tools[name.to_s] end |
#add(tool) ⇒ self Also known as: <<
Add a tool to the manifest
33 34 35 36 |
# File 'lib/robot_lab/tool_manifest.rb', line 33 def add(tool) @tools[tool.name] = tool self end |
#clear ⇒ self
Clear all tools
153 154 155 156 |
# File 'lib/robot_lab/tool_manifest.rb', line 153 def clear @tools.clear self end |
#each {|Tool| ... } ⇒ Object
Iterate over tools
145 146 147 |
# File 'lib/robot_lab/tool_manifest.rb', line 145 def each(&block) @tools.values.each(&block) end |
#empty? ⇒ Boolean
Check if manifest is empty
137 138 139 |
# File 'lib/robot_lab/tool_manifest.rb', line 137 def empty? @tools.empty? end |
#fetch(name) ⇒ Tool
Get a tool by name, raising if not found
68 69 70 71 72 |
# File 'lib/robot_lab/tool_manifest.rb', line 68 def fetch(name) @tools.fetch(name.to_s) do raise ToolNotFoundError, "Tool not found: #{name}. Available tools: #{names.join(', ')}" end end |
#include?(name) ⇒ Boolean Also known as: has?
Check if a tool exists
79 80 81 |
# File 'lib/robot_lab/tool_manifest.rb', line 79 def include?(name) @tools.key?(name.to_s) end |
#merge(other) ⇒ self
Merge another manifest or array of tools
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/robot_lab/tool_manifest.rb', line 174 def merge(other) case other when ToolManifest other.each { |tool| add(tool) } when Array other.each { |tool| add(tool) } when Tool add(other) end self end |
#names ⇒ Array<String>
Get all tool names
93 94 95 |
# File 'lib/robot_lab/tool_manifest.rb', line 93 def names @tools.keys end |
#remove(name) ⇒ Tool?
Remove a tool from the manifest
49 50 51 |
# File 'lib/robot_lab/tool_manifest.rb', line 49 def remove(name) @tools.delete(name.to_s) end |
#replace(tools) ⇒ self
Replace all tools
163 164 165 166 167 |
# File 'lib/robot_lab/tool_manifest.rb', line 163 def replace(tools) clear Array(tools).each { |tool| add(tool) } self end |
#size ⇒ Integer Also known as: count, length
Number of tools
119 120 121 |
# File 'lib/robot_lab/tool_manifest.rb', line 119 def size @tools.size end |
#to_h ⇒ Hash<String, Hash>
Converts the manifest to a hash representation.
189 190 191 |
# File 'lib/robot_lab/tool_manifest.rb', line 189 def to_h @tools.transform_values(&:to_h) end |
#to_json(*args) ⇒ String
Converts the manifest to JSON.
197 198 199 |
# File 'lib/robot_lab/tool_manifest.rb', line 197 def to_json(*args) to_h.to_json(*args) end |
#values ⇒ Array<Tool> Also known as: all, to_a
Get all tools
101 102 103 |
# File 'lib/robot_lab/tool_manifest.rb', line 101 def values @tools.values end |