Small update.
Robin pointed up that I was doing something stupid with the git submodules inside middleclass-extras (the middleclass repository url was not the 'public' one, so it worked allright on my machine but no one else could get it correctly - duh). That is fixed now.
I've also just finished doing a change to the Callbacks module.
This one has been by far the most difficult one to nail, but it is reasonably working now so I wanted to share it with the world.
The callbacks module allows you to 'hook' functions to existing functions.
Quick look:
Code: Select all
Human = class('Human'):include(Callbacks)
function Human:brushTeeth() print('Brushing teeth') end
function Human:sleep() print('ZZZ') end
Human:addCallback('before', 'sleep', 'brushTeeth') -- the last param can also be a function
local peter = Human:new()
peter:sleep() -- normal call, prints ZZZ
peter:sleepWithCallbacks() -- prints 'Brushing teeth' and then 'ZZZ'
I hope it is easy enough to understand. Now the nice part: it works ok with inheritance:
Code: Select all
Soldier = class('Soldier', Human) -- subclass of human
function Soldier:haveNightmare() print('Having nightmare') end
Soldier:addCallback('before', 'sleep', function(soldier) print('Putting gun under pillow') end) -- add another callback before
Soldier:addCallback('after', 'sleep', 'haveNightmare') -- and also one callback after
local huck = Soldier:new()
huck:sleepWithCallbacks() -- prints 'Putting gun under pillow', 'Brushing teeth', 'ZZZ', 'Having nightmare'
Callbacks can also be used to interrupt or cancel the execution of a method: any callback returning false will stop the execution of the 'xxxWithCallbacks' method and immediately return false.
Also, initialize and destroy are special: they are always executed with callbacks. This is very useful for implementing fun stuff; the Apply module, for example,
uses the Callbacks module to 'register' and 'unregister' items from the 'class list' automatically, without the user having to worry about it. Which is cool.
So yeah, last weekend was supposed to be the 'start pumping up documentation' weekend, but at the end I decided to continue doing this fun stuff.
I'm even considering making another change - getting rid of the xxxWithCallbacks and adding a xxxWithoutCallbacks instead. Which option do you prefer?