PluginFactory
PluginFactory is a Ruby module for adding plugin-like behavior to classes which include it. A class which is extended by PluginFactory can be used as a factory for its derivatives, capable of searching for and loading them by name. This is useful when you have an abstract base class which defines an interface and basic functionality for a part of a larger system, and a collection of subclasses which implement the interface for different underlying functionality.
An example of where this might be useful is in a program which talks to a database. To avoid coupling it to a specific database, you use a Driver class which encapsulates your program's interaction with the database behind a useful interface. Now you can create a concrete implementation of the Driver class for each kind of database you wish to talk to. If you make the base Driver class a PluginFactory, too, you can add new drivers simply by dropping them in a directory and using the Driver's create method to instantiate them:
Synopsis
in driver.rb
require "pluginfactory" class Driver include PluginFactory def self::derivativeDirs ["drivers"] end end
in drivers/mysql.rb
require 'driver' class MysqlDriver < Driver ...implementation... end
in /usr/lib/ruby/1.8/PostgresDriver.rb
require 'driver' class PostgresDriver < Driver ...implementation... end
elsewhere
require 'driver' config[:driver_type] #=> "mysql" driver = Driver::create( config[:driver_type] ) driver.class #=> MysqlDriver pgdriver = Driver::create( "PostGresDriver" )
Downloads
- RubyGem: pluginfactory-1.0.3.gem (33.00 k)
- Tar+Bzip2: pluginfactory-1.0.3.tar.bz2 (27.81 k)
- Tar+Gzip: pluginfactory-1.0.3.tar.gz (31.17 k)
- Zip: pluginfactory-1.0.3.zip (37.12 k)
Documentation
- You can view the API documentation generated from the source code with RDoc.
- For a complete list of local wiki pages, see TitleIndex.
Trac
This project is managed using Trac, an excellent Open Source project tracker. For more information, see:
- TracGuide -- Built-in Documentation
- The Trac project -- Trac Open Source Project
- Trac FAQ -- Frequently Asked Questions
- TracSupport -- Trac Support
