Allows for multi-branch callbacks from a single method. This allows for blocks of code to be invoked in response to asynchronous events triggered in the called method.
Add this line to your application's Gemfile:
gem 'callback'
And then execute:
$ bundle
Or install it yourself as:
$ gem install motion-callback
Using callbacks is really easy. Just add it to your Gemfile. Then in your code do this:
do_something_complicateddo |on|
on.success{# take actions based on success}on.failure{# crash and burn gracefully}on.indeterminate_state{# act really confused}endIn your do_something_complicated method, write code like this:
defdo_something_complicated(&block)some_asynchronous_stuff{|status|
casestatuswhen'error'thenblock.callCallback.new(:error)when'success'thenblock.callCallback.new(:success)when'???'thenblock.callCallback.new(:indeterminate_state)end}endA better idea of why this is interesting is code that reaches out to a remote for asynchronously-retrieved data like this:
check_already_logged_indo |on|
on.success{transition_to_authenticated_user_section}on.failure{logindo |on|
on.success{transition_to_authenticated_user_section}on.failure{alert_user_to_screwup}end}endand this Gist, which is essentially the entire Gem.
https://gist.github.com/EmmanuelOga/1417762
Note also this blog post and the ensuing discussion.
- 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