mmcgrana / simple_callbacks

Generic callback functionality

This URL has Read+Write access

mmcgrana (author)
Thu Jul 24 19:40:49 -0700 2008
name age message
file README.textile Loading commit data...
directory lib/
file simple_callbacks.gemspec
directory spec/
README.textile

SimpleCallbacks

Generic callback functionality

Overview

Declare callbacks with the define_callback macro:

class Demo include SimpleCallbacks define_callback :before_save define_callback :after_save end

Once callbacks are declared, add actions to their respective callback action
lists by using the class methods named after the callback. You can use :if
and :unless for granular controller over when callbacks are run:

class Demo before_save :check_validity before_save :perform_denormalization after_save :audit_changes, :if => :auditable? end

You can then invoke he callbacks on instances of the class. Invoking a
callback will run all of its applicable actions in the order they were
declared:

demo = Demo.new demo.invoke_callback(:before_save) demo.inner_save demo.invoke_callback(:after_save)

You can introspect on the declared callbacks by calling .callbacks on the
class in question,

Demo.callbacks {:before_save => [[:check_validity, nil], [:perform_denormalization, nil]], :after_save => [[:audit_changes, {:if => :auditable?}]]}

SimpleCallbacks purposely does not support sharing callback queues up the inheritance
hierarchy, as we believe this introduces more confusion that benefits.