Coder Social home page Coder Social logo

pluggable's Introduction

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.

pluggable's People

Contributors

wizardwerdna avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.