A simple API for adding before, after, and around callbacks to methods.
Think of it as
It's a gem. Install it:
$ gem install hook
Or add it to your Gemfile:
Letting people hook around a method in your object is as simple as:
class SomeObject include Hook +hook def some_method; end end
Now, anyone can hook into your method:
SomeObject.hook_before(:some_method) do |some_object| #... end SomeObject.hook_after(:some_method) do |some_object| #... end SomeObject.hook_around(:some_method) do |some_object| #... end
some_object is the instance of
some_method executed on.
Basically, whenever anyone calls the
some_method method on a
instance, here's what happens:
around some_method hooks run before some_method hooks run some_method method runs after some_method hooks run around some_method hooks run
The hook library allows you to remove configured callbacks for either a specific hooked method, or for all hooked methods.
Specific Hooked Method
You can remove all before, after, and around some_method callbacks on your class with the "remove_callbacks!" method:
All Hooked Methods
If you want to remove all configured callbacks for all hooked methods in one fail swoop, you can use the "remove_all_callbacks!" method:
This software is public domain. Since I've relinquished copyright, you can do anything with it. GO WILD.
Submit a pull request. Tests required. Also, by submitting a pull request, you are relinquishing your copyright over your contribution and submitting it to the public domain.