Git Product home page Git Product logo

hook_me_up's Introduction

HookMeUp

This gem lets you create hook methods for any method of any class.
You can create a :before and :after hooks for any method you like.

Installation

Add this line to your application's Gemfile:

gem 'hook_me_up'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hook_me_up

Usage

  1. Include HookMeUp in your class
  2. call hook_me_up with a method name or array of methods and specify :before hook, :after hook or both.

NOTE: hook_me_up call must come after your method definitions

class SomeClass
  include HookMeUp

	def some_method
	end

	def some_other_method
	end

	def before_hook
	end

	def after_hook
	end

	hook_me_up [:some_method, :some_other_method], :before => :before_hook, :after => :after_hook
end

Disabling hooks for a specific call

You can disable a hooks on specific calls with the following options:

  • :no_hook
  • :no_before_hook
  • :no_after_hook
some_method(args, no_hook: true) # No hooks will be called
some_method(args, no_before_hook: true) # The before hook will be skipped
some_method(args, no_after_hook: true)	# The After hook will be skipped

###You can pass lambda to the hooks instead of methods

hook_me_up :some_method, :before => lambda{ |sender, *args| sender.do_something(args) },
		:after => lambda{ |sender, *args, result| sender.do_something_else(result) }

Arguments

When called with method names

The :before hook is passed with an optional *args, which are the arguments passed to the original method:

def before(*args)
end

The :after hook is passed with optional two arguments:

def after(*args, result)
end

Where result is the result of the original method

When called with lamdba

There is an additional, mandatory, argument: sender, which is the class instance of the original method:

hook_me_up :some_method, :before => lambda{ |sender, *args| ... },
	:after => lambda{ |sender, @args, result| ... }

Examples (Controller)

The following two examples show how to use hook_me_up in a controller, which actually does the same as before_filter; this is just for the sake of demonstration and can be done with models, 'regular' classes and so on...

With lambda

class HomeController < ApplicationController
	include HookMeUp

	def index
		render :text => "hello there #{params[:name]}"
	end

	hook_me_up :index, 
		:before => lambda{ |controller, *args| controller.params[:name] = controller.current_user.name }
end

With method name

class HomeController < ApplicationController
	include HookMeUp

	def index
		render :text => "hello there #{params[:name]}"
	end

	hook_me_up :index, 
		:before => :do_this_before

	private

		def do_this_before
			params[:name] = current_user.name
		end
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

hook_me_up's People

Contributors

psychocandy avatar

Watchers

 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.