Class: Flexo::PluginManager
- Inherits:
-
Object
- Object
- Flexo::PluginManager
- Defined in:
- lib/flexo/pluginmanager.rb
Overview
Here’s the interesting parts. This class is responsible for… Managing plugins! Who would’ve thought that…
Instance Attribute Summary collapse
-
#plugins_available ⇒ Object
readonly
Returns the value of attribute plugins_available.
-
#plugins_loaded ⇒ Object
readonly
Returns the value of attribute plugins_loaded.
Instance Method Summary collapse
-
#find_plugins ⇒ Object
Goes through the folders sent to initialize and looks for anything resembling a plugin.
-
#get_plugin_class(name) ⇒ Object
Find a plugin and its class by its name.
-
#initialize(mutex) ⇒ PluginManager
constructor
As usual.
-
#load_plugin(name) ⇒ Object
Load a plugin.
-
#unload_plugin(plugin) ⇒ Object
Unloads a plugin.
Constructor Details
#initialize(mutex) ⇒ PluginManager
As usual. Set up some things. Create some arrays, some hashes, get a mutex. All that
13 14 15 16 17 18 19 20 |
# File 'lib/flexo/pluginmanager.rb', line 13 def initialize(mutex) @manager = Flexo::Manager.instance @plugins_available = [] @plugins_loaded = CaselessHash.new @plugins_path = @manager.config['plugin.path'].to_a find_plugins end |
Instance Attribute Details
#plugins_available ⇒ Object (readonly)
Returns the value of attribute plugins_available.
9 10 11 |
# File 'lib/flexo/pluginmanager.rb', line 9 def plugins_available @plugins_available end |
#plugins_loaded ⇒ Object (readonly)
Returns the value of attribute plugins_loaded.
8 9 10 |
# File 'lib/flexo/pluginmanager.rb', line 8 def plugins_loaded @plugins_loaded end |
Instance Method Details
#find_plugins ⇒ Object
Goes through the folders sent to initialize and looks for anything resembling a plugin. Then shove them into a hash so that we can find them later
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/flexo/pluginmanager.rb', line 25 def find_plugins @manager.logger.debug " Going to search for plugins (in #{@plugins_path.join(',')}" @plugins_path.each do |dir| Dir["#{dir}/*.rb"].each do |file| begin require file rescue => e @manager.logger.warn(e.) next end end ObjectSpace.each_object(Class) do |klass| if klass < Flexo::Plugin @manager.logger.debug("#{klass} looks like a valid Flexo-plugin") unless klass::NAME raise PluginNameError, "The plugin hasn't specified a name!" end if @plugins_available.any? { |plugin| plugin::NAME.downcase == klass::NAME.downcase } raise PluginConflictError, "The plugin name #{klass::NAME} is already being used by another plugin." end @plugins_available << klass @plugins_available.uniq! end end end end |
#get_plugin_class(name) ⇒ Object
Find a plugin and its class by its name
57 58 59 60 61 |
# File 'lib/flexo/pluginmanager.rb', line 57 def get_plugin_class(name) @plugins_available.find { |plugin| plugin::NAME.downcase == name.downcase } end |
#load_plugin(name) ⇒ Object
Load a plugin
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/flexo/pluginmanager.rb', line 64 def load_plugin(name) @manager.logger.debug("Going to load plugin #{name}") klass = get_plugin_class(name) raise Flexo::PluginNotFoundError, "The #{name} plugin could not be found." unless klass if !@plugins_loaded.key?(klass::NAME) begin klass::DEPENDS.each do |dependency| if(dependency[0..4] == "ruby-") begin require dependency[5..-1] rescue LoadError raise PluginDependencyError, "Plugin requires #{dependency[5..-1]}, which can't be found" end next end dep = get_plugin_class(dependency) if !@plugins_loaded.key?(dep::NAME) load_plugin(dep::NAME) end end plugin = klass.new(@plugins_loaded) rescue Flexo::PluginError => e @manager.logger.error "Plugin encountered an error (#{e.class}): #{e.message}" @manager.logger.error e.backtrace rescue Flexo::PluginDependencyError => e @manager.logger.error e. return nil rescue Flexo::PluginNotFoundError => e @manager.logger.warn e. return nil end @manager.mutex { @plugins_loaded[klass::NAME] = plugin } @manager.logger.debug " Loaded #{plugin.class}" else @manager.logger.debug " Already loaded #{klass.class}" plugin = @plugins_loaded[klass::NAME] end plugin end |
#unload_plugin(plugin) ⇒ Object
Unloads a plugin
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/flexo/pluginmanager.rb', line 111 def unload_plugin(plugin) @manager.logger.debug "Going to unload plugin #{plugin}" klass = @plugins_loaded[plugin] @manager.mutex { @plugins_loaded.delete(plugin) } klass.unload # Every file we load ends up in $". Require checks there, and if it's there, # it doesn't reload the file. So to force tthe removal of the plugin, # we remove the class from the object space, and from the array @plugins_path.each do |p| file = File.(File.join(p, "#{plugin.downcase}.rb")) $".delete file end Object.send :remove_const, klass.name if klass end |