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.
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
- Include HookMeUp in your class
- 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
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
hook_me_up :some_method, :before => lambda{ |sender, *args| sender.do_something(args) },
:after => lambda{ |sender, *args, result| sender.do_something_else(result) }
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
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| ... }
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...
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
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
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request