RubyGems Navigation menu

pluggability 0.5.0

Pluggability is a toolkit for creating plugins. It provides a mixin that extends your class with methods to load and instantiate its subclasses by name. So instead of: require 'acme/adapter/png' png_adapter = Acme::Adapter::PNG.new( 'file.png' ) you can do: require 'acme/adapter' png_adapter = Acme::Adapter.create( :png, 'file.png' ) A full example of where this might be useful is in a program which generates output with a 'driver' object, which provides a unified interface but generates different kinds of output. First the abstract base class, which is extended with Pluggability: # in mygem/driver.rb: require 'pluggability' require 'mygem' unless defined?( MyGem ) class MyGem::Driver extend Pluggability plugin_prefixes "mygem/drivers" end We can have one driver that outputs PDF documents: # mygem/drivers/pdf.rb: require 'mygem/driver' unless defined?( MyGem::Driver ) class MyGem::Driver::PDF < Driver ...implementation... end and another that outputs plain ascii text: #mygem/drivers/ascii.rb: require 'mygem/driver' unless defined?( MyGem::Driver ) class MyGem::Driver::ASCII < Driver ...implementation... end Now the driver is configurable by the end-user, who can just set it by its short name: require 'mygem' config[:driver_type] #=> "pdf" driver = MyGem::Driver.create( config[:driver_type] ) driver.class #=> MyGem::Driver::PDF # You can also pass arguments to the constructor, too: ascii_driver = MyGem::Driver.create( :ascii, :columns => 80 )

Versions:

  1. 0.5.0 - January 19, 2018 (25 KB)
  2. 0.4.3 - March 04, 2015 (24 KB)
  3. 0.4.2 - March 04, 2015 (24 KB)
  4. 0.4.0 - January 08, 2014 (22.5 KB)
  5. 0.3.0 - September 26, 2013 (22.5 KB)
Show all versions (10 total)

Runtime Dependencies (1):

Authors:

  • Martin Chase, Michael Granger

Owners:

10ffad34687d0045466c257cb92ff2b304cdb622ce0aac272b61f23f373e2abf

SHA 256 checksum:

812ed81c00745b02fa20de0d99b7208c82e74ee317f65935b069887c8ce1111d

Total downloads 18,188

For this version 4,602

Gemfile:
= Copy to clipboard Copied!

install:
=

License:

BSD-3-Clause

Required Ruby Version: >= 2.3.4

Links: