Class: Cyclid::API::Plugins::Registry
- Inherits:
-
Object
- Object
- Cyclid::API::Plugins::Registry
- Defined in:
- app/cyclid/plugin_registry.rb
Overview
Intelligent system-wide registry of available plugins with helper methods to find them again
Instance Method Summary collapse
-
#all(type = nil) ⇒ Object
Return a list of all plugins of a certain type.
-
#find(name, type) ⇒ Object
Find a plugin from the registry.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#register(plugin) ⇒ Object
Add a plugin to the registry.
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
25 26 27 28 |
# File 'app/cyclid/plugin_registry.rb', line 25 def initialize @plugins = [] @types = [] end |
Instance Method Details
#all(type = nil) ⇒ Object
Return a list of all plugins of a certain type
65 66 67 68 69 70 71 |
# File 'app/cyclid/plugin_registry.rb', line 65 def all(type = nil) list = [] @plugins.each do |plugin| list << plugin if plugin.superclass == type or type.nil? end return list end |
#find(name, type) ⇒ Object
Find a plugin from the registry
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/cyclid/plugin_registry.rb', line 41 def find(name, type) object_type = nil # Convert a human name to a type, if required if type.is_a? String @types.each do |registered_type| next unless registered_type[:human] == type object_type = registered_type[:type] break end else object_type = type end raise "couldn't map plugin type #{type}" if object_type.nil? @plugins.each do |plugin| return plugin if plugin.name == name && plugin.superclass == object_type end return nil end |
#register(plugin) ⇒ Object
Add a plugin to the registry
31 32 33 34 35 36 37 38 |
# File 'app/cyclid/plugin_registry.rb', line 31 def register(plugin) # XXX Perform sanity checks @plugins << plugin # Maintain a human<->type mapping human_name = plugin.human_name @types << { human: human_name, type: plugin.superclass } end |