Skip to content

Repository files navigation

motion-callback

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.

Installation

Add this line to your application's Gemfile:

gem 'callback'

And then execute:

$ bundle

Or install it yourself as:

$ gem install motion-callback

Usage

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}end

In 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}end

A 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}end

Thanks To

Emmanuel Oga

and this Gist, which is essentially the entire Gem.

https://gist.github.com/EmmanuelOga/1417762

Note also this blog post and the ensuing discussion.

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

About

Multi-branch callbacks

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages