A mixin for classes requiring plugins
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.document
.gitignore
LICENSE
README.rdoc
Rakefile
VERSION
pluggable.gemspec

README.rdoc

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

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