Class: RubyPi::Tools::Registry
- Inherits:
-
Object
- Object
- RubyPi::Tools::Registry
- Defined in:
- lib/ruby_pi/tools/registry.rb
Instance Method Summary collapse
-
#all ⇒ Array<RubyPi::Tools::Definition>
Returns all registered tool definitions.
-
#by_category(category) ⇒ Array<RubyPi::Tools::Definition>
Returns all tools that belong to the given category.
-
#find(name) ⇒ RubyPi::Tools::Definition?
Finds a tool by name.
-
#initialize ⇒ Registry
constructor
Creates a new, empty Registry.
-
#inspect ⇒ String
Provides a human-readable string representation.
-
#names ⇒ Array<Symbol>
Returns the names of all registered tools.
-
#register(tool) ⇒ RubyPi::Tools::Definition
Registers a tool definition in the registry.
-
#registered?(name) ⇒ Boolean
Checks whether a tool with the given name is registered.
-
#size ⇒ Integer
Returns the number of registered tools.
-
#subset(names) ⇒ RubyPi::Tools::Registry
Returns a new Registry containing only the tools with the given names.
Constructor Details
#initialize ⇒ Registry
Creates a new, empty Registry.
24 25 26 27 |
# File 'lib/ruby_pi/tools/registry.rb', line 24 def initialize @tools = {} @mutex = Mutex.new end |
Instance Method Details
#all ⇒ Array<RubyPi::Tools::Definition>
Returns all registered tool definitions. Thread-safe.
96 97 98 |
# File 'lib/ruby_pi/tools/registry.rb', line 96 def all @mutex.synchronize { @tools.values } end |
#by_category(category) ⇒ Array<RubyPi::Tools::Definition>
Returns all tools that belong to the given category. Thread-safe.
86 87 88 89 90 91 |
# File 'lib/ruby_pi/tools/registry.rb', line 86 def by_category(category) cat = category.to_sym @mutex.synchronize do @tools.values.select { |tool| tool.category == cat } end end |
#find(name) ⇒ RubyPi::Tools::Definition?
Finds a tool by name. Thread-safe.
62 63 64 |
# File 'lib/ruby_pi/tools/registry.rb', line 62 def find(name) @mutex.synchronize { @tools[name.to_sym] } end |
#inspect ⇒ String
Provides a human-readable string representation.
125 126 127 |
# File 'lib/ruby_pi/tools/registry.rb', line 125 def inspect "#<RubyPi::Tools::Registry tools=#{names.inspect}>" end |
#names ⇒ Array<Symbol>
Returns the names of all registered tools. Thread-safe.
103 104 105 |
# File 'lib/ruby_pi/tools/registry.rb', line 103 def names @mutex.synchronize { @tools.keys } end |
#register(tool) ⇒ RubyPi::Tools::Definition
Registers a tool definition in the registry.
If a tool with the same name already exists, it will be overwritten. A debug-level log message is emitted if a logger is configured; otherwise the overwrite is silent (no warn to stderr).
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ruby_pi/tools/registry.rb', line 38 def register(tool) unless tool.is_a?(RubyPi::Tools::Definition) raise ArgumentError, "Expected a RubyPi::Tools::Definition, got #{tool.class}" end @mutex.synchronize do if @tools.key?(tool.name) # Use the configured logger at debug level instead of unconditional # warn to stderr, which is noisy in production environments. logger = RubyPi.configuration.logger if logger logger.debug("RubyPi::Tools::Registry: overwriting existing tool '#{tool.name}'") end end @tools[tool.name] = tool end tool end |
#registered?(name) ⇒ Boolean
Checks whether a tool with the given name is registered. Thread-safe.
118 119 120 |
# File 'lib/ruby_pi/tools/registry.rb', line 118 def registered?(name) @mutex.synchronize { @tools.key?(name.to_sym) } end |
#size ⇒ Integer
Returns the number of registered tools. Thread-safe.
110 111 112 |
# File 'lib/ruby_pi/tools/registry.rb', line 110 def size @mutex.synchronize { @tools.size } end |
#subset(names) ⇒ RubyPi::Tools::Registry
Returns a new Registry containing only the tools with the given names.
Tools that are not found in this registry are silently skipped. Thread-safe.
73 74 75 76 77 78 79 80 |
# File 'lib/ruby_pi/tools/registry.rb', line 73 def subset(names) sub = Registry.new names.each do |name| tool = find(name) sub.register(tool) if tool end sub end |