Module: Poro::Context::CallbackMethods
- Included in:
- Poro::Context
- Defined in:
- lib/poro/context.rb
Overview
A mixin to support callbacks. There are three kinds of callbacks:
- Events
-
Events are callbacks that are passed a handle to the object when a particular kind of event has occured. These may destructively edit objects.
- Transform
-
Transforms are callbacks where each handler is passed the result of the previous transform, and may return any value. The issuing object then uses the final value in some way.
- Filters
-
Calls each callback in sequence, pasing in the issuing object. Terminates execution on the first callback that is “false” (as determined by an if statement), or when there are no callbacks left. Gives the issuing object the result of the last block.
Contexts issue the following event callbacks:
- :before_save
-
Called before save; passes the object that is going to be saved.
- :after_save
-
Called after save; passes the object that was saved.
- :before_remove
-
Called before removing an object from persistent storage; passes the object that will be removed.
- :after_remove
-
Called after removing an object from persistent storage; passes the object that was removed.
- :after_fech
-
Called after an object is fetched from the persistent store; passes the object that was fetched.
- :after_convert_to_plain_object
-
Called after an object is converted to a plain object from the persistent store but before it is used; passes the plain object.
- :after_convert_to_data
-
Called after an object is converted to the persistent store’s data structure but before it is used; passes the data store’s data structure.
Contexts issue the following transform callbacks:
- :before_convert_to_plain_object
-
Called just before a context converts persistent store data to a plain ruby object; is passed the persistent store data object; the result is what is converted.
In most cases it is better to use the
after_convert_to_plain_object
callback event. - :before_convert_to_data
-
Called just before a context converts a plain ruby object to persistent store data; is passed the plain ruby object; the result is what is converted.
In most cases it is better to use the
before_convert_to_plain_object
callback event.
Instance Method Summary collapse
-
#callbacks(event) ⇒ Object
Return the raw array of callbacks.
-
#clear_callbacks(event) ⇒ Object
Clear all callbacks for a given event.
-
#register_callback(event, &block) ⇒ Object
Register a callback for a given event.
Instance Method Details
#callbacks(event) ⇒ Object
Return the raw array of callbacks. This can be manipulated if more straightforward methods don’t do the trick, but usually this is a consequence of trying to solve the problem wrong.
While usually a kind of Proc, callbacks may be any object that responds to call.
519 520 521 522 523 524 |
# File 'lib/poro/context.rb', line 519 def callbacks(event) @event_callbacks ||= {} key = event.to_sym @event_callbacks[key] ||= [] return @event_callbacks[key] end |
#clear_callbacks(event) ⇒ Object
Clear all callbacks for a given event.
This can be dangerous because
534 535 536 |
# File 'lib/poro/context.rb', line 534 def clear_callbacks(event) callbacks(event).clear end |
#register_callback(event, &block) ⇒ Object
Register a callback for a given event.
527 528 529 |
# File 'lib/poro/context.rb', line 527 def register_callback(event, &block) callbacks(event) << block end |