Coder Social home page Coder Social logo

plugins's Introduction

Gem Version Code Climate Test Coverage Build Status Inch CI

Plugins

Easily add plugins to any library. Plugins takes care of calling before, after and around callbacks for you. All you need to do is wrap your blocks of interest in a Plugins#perform block. Add any number of plugins and any number of callbacks within each plugin.

Installation

Add this to your Gemfile:

gem 'plugins'

Then run

bundle

Or you can install it manually:

gem install plugins

Basic Usage

Define your component

In Plugins, whatever code you're writing that will have plugin support is called a "component". The Plugins::ComponentMixin simplifies integrating with the Plugins gem, although it's not required that you use it.

require 'plugins'
class ComponentWithPlugins
  include Plugins::ComponentMixin

  ##
  # Allow users to use the :logging symbol to reference the LoggingPlugin
  plugin_loader { |plugin|
    case plugin
    when :logging
      LoggingPlugin
    end
  }

  def do_something(*args)
    plugins.perform(:do_something, *args) { |*args|
      puts "Doing something: #{args.inspect}"
    }
  end
end

Define your plugins

class LoggingPlugin < Plugins::Plugin
  def initialize(plugins, file_name=nil)
    super(plugins)
    raise "Logging to a file is't supported yet!" if file_name
  end

  before_do_something do |*args|
    # Any instance methods can be called within callback blocks.
    log("Sending these args: #{args.inspect}")
  end

  around_do_something do |*args|
    begin
      do_something # Magic method to call code being performed
    rescue Exception => e
      log("Exception occurred! #{e.inspect}")
    end
  end

  after_do_something do |*args|
    log("Finished with args: #{args.inspect}")
  end

  private
  def log(message)
    puts "Logging Message: #{message}"
  end
end

Add plugins to your component

component = ComponentWithPlugins.new
component.plugin(:logging) # Utilizes your plugin_loader block.

# This would also work, too.
# But don't both because plugins can be added multiple times!
# component.plugin(LoggingPlugin)

# Alternatively, pass additional args to the Plugin initializer:
# component.plugin(:logging, 'file_name') # LoggingPlugin will raise an exception

component.do_something(1, :two, 'three')
# Should output:
# Logging Message: Sending these args: [1, :two, "three"]
# Doing something: [1, :two, "three"]
# Logging Message: Finished with args: [1, :two, "three"]

User's can also define their own custom plugins:

component.plugin {
  after_do_something {
    puts "Custom plugin!"
  }
}

component.do_something("hello world")
# Should output:
# Logging Message: Sending these args: ["hello world"]
# Doing something: ["hello world"]
# Custom plugin!
# Logging Message: Finished with args: ["hello world"]

Details

Defining a Plugin

There are two ways to define a plugin.

  1. Extending Plugins::Plugin
  2. Plugins::Plugin.create

When defining a plugin, you can define any arbitrary before, around and after callbacks. You can also define multiple callbacks of the same type. They will be executed in reverse order that you defined them.

Extending Plugins::Plugin

require 'plugins'

class CustomPlugin < Plugins::Plugin
  before_i_do_something do |*args|
    puts "I'm about to do something..."
  end
  before_i_do_something { puts "This one will be executed first" }

  around_i_do_something do |*args|
    puts "And you won't like it..."

    perform_i_do_something # Just `i_do_something` will also work.

    puts "But too bad..."
  end

  after_i_do_something do |*args|
    puts "Because I just did it."
    helper_method
  end

  # All *instance* methods will be available.
  def helper_method
    puts "Running helper method"
  end
end

Plugin.create

You can also create on-the-fly plugins:

custom_plugin = Plugins::Plugin.create do
  before_brush_teeth do
    floss
  end

  after_brush_teeth do
    spit
  end
end

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.