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.



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

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



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

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



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

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.



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

def load_plugins
  each_enabled_plugin(&:activate)
end

#to_sObject

String representation



105
106
107
108
109
110
111
# File 'lib/fedux_org_stdlib/gem_plugins/plugin_manager.rb', line 105

def to_s
  data = plugins.sort.reduce([]) { |a, e| a << { name: e.name, enabled: e.enabled?, gem_name: e.gem_name, required_file: e.required_file } }

  List.new(data).to_s(
    fields: [:name, :gem_name, :enabled, :required_file]
  )
end