Module: GollyUtils::Callbacks
- Defined in:
- lib/golly-utils/callbacks.rb
Overview
A very simple callback mechanism for use within a single class heirarchy.
It is primiarily meant to be used as a replacement for method overriding in external subclasses; the problem with that approach being a) it's unclear with methods are required/overrides, and b) if the wrong method name is used there is no early feedback - the erronously named method will simply never be invoked and the super-method will not receive the intended modification.
It allows:
- A class to define named callback point.
- Subclasses to supply callbacks to specific points by name.
- Ability to run all callbacks for a given callback point.
Unlike Rails' callbacks implementation, this deliberately doesn't provide before/after/around functionality, nor a chain-like structure where the return value of one callback can affect the determinism of other callbacks being invoked.
Usage
- In your superclass:
- Include Callbacks.
- Use ClassMethods#define_callbacks in the class definition.
- Call InstanceMethods#run_callbacks in your code.
- In subclasses:
- Supply a callback by declaring the callback name in the class definition, followed by a block of code.
Defined Under Namespace
Modules: ClassMethods, InstanceMethods, ModuleMethods