psychic is a metaprogramming module which inject a listener interface into any object. it was first intented to be used to make ordinary arrays listenable, but of course may be used for other purposes.
for example: a = [:a,:b,:c] Psychic.connect(a,:delete, {|obj,method,value,*args,&proc| puts “#{method}->#objobj.inspect”} a.delete :a #will call proc when delete is called.
> delete->:a
#(the proc is called following the method) #parameters to the proc are as follows: #object -> the object which is being listened to. #method -> name of the method which was called #value -> value returned by method #args -> arguements to method #proc -> the proc passed to the method (if applicable)
Psychic can remove procs as well, but you need to retain the reference to the proc so you must call it in a different style:
p = proc {|obj,method,*args,&proc|} Psychic.connect(object,method_names,.., &p) #note that the proc can be injected into many methods at once.
#now, disconnect Psychic like this: Psychic.disconnect(object,method_names,.., &p)
#now the proc p will no longer be called multiple blocks can be injected into the same methods by repeated calls to Psychic.connect()
also, I have included a convience method which wraps all the methods which change the state of String,Array, and Hash
Psychic.connect_mutable(object,&proc)
this is a easy way to create a listener who is notified of changes of state of a Array, String or Hash.
this is my first gem. any questions/suggestions/encouragement/requests please email. [email protected]
cheers!