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>`. For the plugin to be found, it needs to include the so called plugin file `your-application-extension.rb`.

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(/\//, '-')}-plugin.rb" as plugin-file
  @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 plugin file

# -- main.rb --
# main module
module YourApplication
  # The manager uses 'asdf-plugin1.rb' to find plugins
  @plugin_manager = PluginManager.new(plugin_file: 'asdf-plugin1.rb')

  [...]
end

Load plugins in your application


# -- runner.rb --

YourApplication.load_plugins

Instance Method Summary collapse

Constructor Details

#initialize(creator: Plugin, plugin_file: File.join('lib', __plugin_prefix + 'plugin.rb')) ⇒ PluginManager

Returns a new instance of PluginManager.



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

def initialize(creator: Plugin, plugin_file: File.join('lib', __plugin_prefix + 'plugin.rb'))
  @creator     = creator
  @plugin_file = plugin_file

  # needs variables above
  @plugins   = plugins_in_rubygems_path
end

Instance Method Details

#activate_plugin(*names) ⇒ Object

Enable a plugin



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

def activate_plugin(*names)
  names.flatten.each do |n|
    plugin = plugins.find(NoPlugin.new(n)) { |p| p.name == n }

    return if plugin.blank?

    plugin.activate
  end
end

#to_sObject

String representation



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fedux_org_stdlib/gem_plugins/plugin_manager.rb', line 93

def to_s
  data = plugins.sort.reduce([]) do |a, e|
    a << {
      name: e.name,
      version: e.version,
      author: e.author,
      homepage: e.homepage,
      activated: e.active?,
      require_file: e.require_file
    }
  end

  List.new(data).to_s(
    fields: [:name, :version, :author, :homepage, :activated, :require_file]
  )
end