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.
23 24 25 26 |
# File 'lib/ruby_pi/tools/registry.rb', line 23 def initialize @tools = {} @mutex = Mutex.new end |
Instance Method Details
#all ⇒ Array<RubyPi::Tools::Definition>
Returns all registered tool definitions.
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.
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.
55 56 57 |
# File 'lib/ruby_pi/tools/registry.rb', line 55 def find(name) @tools[name.to_sym] end |
#inspect ⇒ String
Provides a human-readable string representation.
115 116 117 |
# File 'lib/ruby_pi/tools/registry.rb', line 115 def inspect "#<RubyPi::Tools::Registry tools=#{names.inspect}>" end |
#names ⇒ Array<Symbol>
Returns the names of all registered tools.
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.
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.
108 109 110 |
# File 'lib/ruby_pi/tools/registry.rb', line 108 def registered?(name) @tools.key?(name.to_sym) end |
#size ⇒ Integer
Returns the number of registered 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.
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 |