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.



23
24
25
26
# File 'lib/ruby_pi/tools/registry.rb', line 23

def initialize
  @tools = {}
  @mutex = Mutex.new
end

Instance Method Details

#allArray<RubyPi::Tools::Definition>

Returns all registered tool definitions.

Returns:



86
87
88
# File 'lib/ruby_pi/tools/registry.rb', line 86

def all
  @tools.values
end

#by_category(category) ⇒ Array<RubyPi::Tools::Definition>

Returns all tools that belong to the given category.

Parameters:

  • category (Symbol, String)

    The category to filter by.

Returns:



78
79
80
81
# File 'lib/ruby_pi/tools/registry.rb', line 78

def by_category(category)
  cat = category.to_sym
  @tools.values.select { |tool| tool.category == cat }
end

#find(name) ⇒ RubyPi::Tools::Definition?

Finds a tool by name.

Parameters:

  • name (String, Symbol)

    The name of the tool to look up.

Returns:



55
56
57
# File 'lib/ruby_pi/tools/registry.rb', line 55

def find(name)
  @tools[name.to_sym]
end

#inspectString

Provides a human-readable string representation.

Returns:

  • (String)

    Summary of the registry contents.



115
116
117
# File 'lib/ruby_pi/tools/registry.rb', line 115

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

#namesArray<Symbol>

Returns the names of all registered tools.

Returns:

  • (Array<Symbol>)

    An array of tool name symbols.



93
94
95
# File 'lib/ruby_pi/tools/registry.rb', line 93

def names
  @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 and a warning is emitted to stderr.

Parameters:

Returns:

Raises:

  • (ArgumentError)

    If the argument is not a Definition.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ruby_pi/tools/registry.rb', line 36

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)
      warn "RubyPi::Tools::Registry: overwriting existing tool '#{tool.name}'"
    end
    @tools[tool.name] = tool
  end

  tool
end

#registered?(name) ⇒ Boolean

Checks whether a tool with the given name is registered.

Parameters:

  • name (String, Symbol)

    The tool name to check.

Returns:

  • (Boolean)

    true if the tool exists in the registry.



108
109
110
# File 'lib/ruby_pi/tools/registry.rb', line 108

def registered?(name)
  @tools.key?(name.to_sym)
end

#sizeInteger

Returns the number of registered tools.

Returns:

  • (Integer)

    The count of tools.



100
101
102
# File 'lib/ruby_pi/tools/registry.rb', line 100

def size
  @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.

Parameters:

  • names (Array<String, Symbol>)

    The tool names to include.

Returns:



65
66
67
68
69
70
71
72
# File 'lib/ruby_pi/tools/registry.rb', line 65

def subset(names)
  sub = Registry.new
  names.each do |name|
    tool = find(name)
    sub.register(tool) if tool
  end
  sub
end