Call Me Back
Synopsis
Automatically provide dead-simple ActiveSupport callbacks to your instance methods.
Preface
This gem is a small extension of ActiveSupport available features. In fact, as referred at this this link, creating callbacks for custom methods induces modify your existent method code source. This gem just automates this task.
Why?
When I work in Ruby On Rails projects, I often use native callbacks, provided by ActiveSupport, such as before_save
or after_create
. But sometimes I want to use this kind of hook methods for my methods. Of course, there is a way to perform this, explained in the previous link. This gem just simplifies the way you can do this.
Installation
Just type:
gem install callmeback
Or add it to your gem file:
gem 'callmeback'
Usage
Basic
Add include Callmeback to your class:
class Example
include Callmeback
before :foo => :bar
def initialize
callmeback!
@result = []
end
def foo
@result << 'foo'
end
def
@result << 'bar'
end
end
example = Example.new
example.foo #=> ['bar','foo']
Because your methods must be redefined to perform custom callbacks, you need to call #callmeback!
on initialization.
You can use before
, after
and around
, as if you use the suggested method in the ActiveSupport::Callbacks
documentation. To perform the around
callbacks, define them like:
around :foo => :wrapped_foo
def wrapped_foo
yield
end
ActiveRecord and Mongoid
You may want to use Callmeback on your ActiveRecord or Mongoid models. I recommend you not to redefine the initialize
method. Instead please use the after_initialize
callback. Here is an example using Mongoid:
class Example
include Mongoid::Document
include Callmeback
after_initialize do
callmeback!
end
...
Array of callbacks
You can pass an array of callback method symbol:
before :foo => [:bar, :bar]
# => ['bar', 'bar', 'foo']
Blocks
You also can pass blocks:
before :foo do
end
after :foo do
end
around :foo do |&block|
block.call
end
Combinations
You can combine them with methods:
before :foo => [:bar, :bar]
after :foo do
end
Array of binded methods
You can pass an array of the methods you want:
before [:foo, :bar] => [:other_method]
Regex
You can run a callback for every method that matches a given regex:
before /^fo.$/ => [:bar, :bar]
How is it work?
First, this gem module defines the class methods .before
, .after
, .around
in your class. Then when you instanciate your class, the gem overrides your binded methods and wraps them to bind your custom callbacks.
Contributing to callmeback
-
Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.
-
Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.
-
Fork the project.
-
Start a feature/bugfix branch.
-
Commit and push until you are happy with your contribution.
-
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright
Copyright © 2012 Sébastien Azimi. See LICENSE.txt for further details.