Project

pluggable

0.0
No commit activity in last 3 years
No release in over 3 years
Pluggable classes are automatically registered from classes that subclass from <classname>::Plugin, and can use tools for managing, installing and delegating methods therefrom.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
 Project Readme

pluggable¶ ↑

Pluggable is a mixin for classes requiring plugins. Including Pluggable adds several methods:

install_plugins

Should generally be called when initializing an instance of the Pluggable class.

plugins

After initialization, returns a list of plugin instances

For example:

require ‘rubygems’ require ‘pluggable’ class Test include Pluggable def initialize; install_plugins; end def process; plugins.map {|plugin| plugin.process}; end end

class Plugin1 < Test::Plugin def process; “foo”; end end

class Plugin2 < Test::Plugin def process; “bar”; end end Test.new.process # => [“foo”, “bar”]

It may be convenient to have public methods of plugins delegated to from the plugins object, which may in turn be delgated to by the Pluggable class in various ways. This is accomplished with the class method delegate_plugin_public_methods_except, which should be called after loading all of the plugins. For example:

require ‘rubygems’ require ‘pluggable’ class Test include Pluggable def initialize; install_plugins; end def process; plugins.map {|plugin| plugin.process}; end end

class Plugin1 < Test::Plugin def foo; “foo”; end def process; foo; end end

class Plugin2 < Test::Plugin def bar; “bar”; end def process; bar; end end

Test.delegate_plugin_public_methods_except :process Test.new.process # => [“foo”, “bar”] Test.new.plugins.foo # => “foo” Test.new.plugins.bar # => “bar” Finally, it may be useful to establish an API for plugins in the form of a traditional Ruby module. The module may be included into the plugins using the class method *plugin_install_methods, which may be called in the Pluggable class definition. For example:

require ‘rubygems’ require ‘pluggable’ module PluginAPI def initialize(one, two, three); end def first; “first”; end def second; private_second; end module ClassMethods def class_first; “class_first”; end end private def private_second; “second”; end def self.included(klass) klass.extend ClassMethods end end

class Test include Pluggable def initialize; install_plugins(:one, :two, :three); end # arity must match initialize methods for all plugins def process; plugins.map {|plugin| plugin.process}; end private def method_missing symbol, *args plugins.send(symbol, *args) end end require ‘plugin_definitions’

Test.new.plugins.first.first # => “first” Test.new.plugins.first.second # => “second” Test.new.plugins.first.class.class_first # => “class_first” And of course, the plugins may override the API definitions.

Note on Patches/Pull Requests¶ ↑

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2009 Andrew C. Greenberg. See LICENSE for details.