pluggable
Pluggable is a mixin for classes requiring plugins. A pluggable class, Klass, has a public function, plugins, returning an array-like object that holds all subclasses of Klass::Plugin.
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. 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”
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.