Class: FeduxOrgStdlib::GemPlugins::PluginManager

Inherits:
Object
  • Object
show all
Defined in:
lib/fedux_org_stdlib/gem_plugins/plugin_manager.rb

Overview

Plugin Manager

To use the plugin manager you should build the plugins for your application as rubygems. How you name the plugins is up to you, but it is recommended to use something like ‘your-application-<plugin>`, where `your-application-` is the prefix (please mind the dash at the end).

Please make sure you create a class for your plugin manager to make it work. Otherwise give the prefix via parameter - see below.

# -- main.rb --
# main module
module YourApplication
  # The manager uses "#{self.name.underscore.gsub(/\//, '-')}-" as prefix
  @plugin_manager = PluginManager.new

  class << self
    attr_reader :plugin_manager

    def load_plugins
      self.plugin_manager.load_plugins
    end
  end
end

At some place you need to load your plugins.

Examples:

PluginManager-class


module YourApplication
  class PluginManager < FeduxOrgStdlib::GemPlugins::PluginManager
  end
end

Default prefix

Use different prefix

# -- main.rb --
# main module
module YourApplication
  # The manager uses 'asdf-' as prefix
  @plugin_manager = PluginManager.new(prefix: 'asdf-')

  [...]
end

Load plugins in your application


# -- runner.rb --

YourApplication.load_plugins

Instance Method Summary collapse

Constructor Details

#initialize(prefix: __plugin_prefix, whitelist: [], blacklist: []) ⇒ PluginManager

Returns a new instance of PluginManager.



71
72
73
74
75
76
77
78
# File 'lib/fedux_org_stdlib/gem_plugins/plugin_manager.rb', line 71

def initialize(prefix: __plugin_prefix, whitelist: [], blacklist: [])
  @prefix    = Regexp.new(prefix)
  @whitelist = Array(whitelist)
  @blacklist = Array(blacklist)

  # needs variables above
  @plugins   = locate_plugins
end

Instance Method Details

#disable_plugin(name) ⇒ Object

Disable a plugin



81
82
83
84
85
86
87
# File 'lib/fedux_org_stdlib/gem_plugins/plugin_manager.rb', line 81

def disable_plugin(name)
  plugin = plugins.find(NoPlugin.new(name)) { |p| p.name == name }

  return if plugin.blank?

  plugin.disable
end

#enable_plugin(name) ⇒ Object

Enable a plugin



90
91
92
93
94
95
96
# File 'lib/fedux_org_stdlib/gem_plugins/plugin_manager.rb', line 90

def enable_plugin(name)
  plugin = plugins.find(NoPlugin.new(name)) { |p| p.name == name }

  return if plugin.blank?

  plugin.enable
end

#load_pluginsObject

Require all enabled plugins, disabled plugins are skipped.



99
100
101
# File 'lib/fedux_org_stdlib/gem_plugins/plugin_manager.rb', line 99

def load_plugins
  each_enabled_plugin { |p| p.activate }
end