Class: RubyPi::Tools::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_pi/tools/registry.rb

Instance Method Summary collapse

Constructor Details

#initializeRegistry

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

#allArray<RubyPi::Tools::Definition>

Returns all registered tool definitions. Thread-safe.

Returns:



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.

Parameters:

  • category (Symbol, String)

    The category to filter by.

Returns:



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.

Parameters:

  • name (String, Symbol)

    The name of the tool to look up.

Returns:



62
63
64
# File 'lib/ruby_pi/tools/registry.rb', line 62

def find(name)
  @mutex.synchronize { @tools[name.to_sym] }
end

#inspectString

Provides a human-readable string representation.

Returns:

  • (String)

    Summary of the registry contents.



125
126
127
# File 'lib/ruby_pi/tools/registry.rb', line 125

def inspect
  "#<RubyPi::Tools::Registry tools=#{names.inspect}>"
end

#namesArray<Symbol>

Returns the names of all registered tools. Thread-safe.

Returns:

  • (Array<Symbol>)

    An array of tool name symbols.



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).

Parameters:

Returns:

Raises:

  • (ArgumentError)

    If the argument is not a Definition.



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.

Parameters:

  • name (String, Symbol)

    The tool name to check.

Returns:

  • (Boolean)

    true if the tool exists in the registry.



118
119
120
# File 'lib/ruby_pi/tools/registry.rb', line 118

def registered?(name)
  @mutex.synchronize { @tools.key?(name.to_sym) }
end

#sizeInteger

Returns the number of registered tools. Thread-safe.

Returns:

  • (Integer)

    The count of tools.



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.

Parameters:

  • names (Array<String, Symbol>)

    The tool names to include.

Returns:



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