A pattern to keep the core small, adding features by having a plugin system. Define a module called Plugins
, store future plugins in a module-level instance variable.
Register plugin is adding the plugin name and module to the @plugins
hash. Load plugin by looking up if the plugin exists in @plugins
hash.
module Plugins
PluginNotFoundError = Class.new(StandardError)
@plugins = {}
def self.register_plugin(name, modudle)
@plugins[name] = modudle
end
def self.load_plugin(name)
plugins_cache = @plugins
if plugin != plugins_cache[name]
require "plugins/#{name}"
raise PluginNotFoundError, "Plugin #{name} did not register correctly." unless plugin = plugins_cache[name]
end
plugin
end
end
Replace @plugins = {}
to a thread-safe cache to ensure thread safety.
@plugins = Cache.new