module EagleClaw::Callbacks
Public Instance Methods
register(context, meth = nil, &block)
click to toggle source
Register a callback for a given context.
@param [Object] context
an object (such as a symbol or list of symbols) which refers to a certain callback context.
@param [optional, Symbol] meth the name of the callback method.
@overload register(context, :method_name)
Register a method as a callback.
@overload register(context, &block)
Register a block as a callback.
@example Registering a method as a callback
class MyCLS include Callbacks register(:preprocessing, :setup_db) def setup_db @database = DB.connect("username", "password") end end
@example Registering a block as a callback
class MyCLS include Callbacks register :preprocessing do @database = DB.connect("username", "password") end end
# File lib/eagleclaw/callbacks.rb, line 36 def register(context, meth = nil, &block) callback = block_given? ? block : meth ((@callbacks ||= {})[context] ||= []) << callback end
run_callbacks(context, recipient = self)
click to toggle source
Run the callbacks for a given context.
@param context @param [optional, Object] recipient the object to run methods/procs on. @return [nil]
# File lib/eagleclaw/callbacks.rb, line 47 def run_callbacks(context, recipient = self) (@callbacks[context] || []).each { |callback| run_proc(callback, recipient) } nil end
Private Instance Methods
run_proc(meth, recipient = self)
click to toggle source
Run a given method or ‘Proc` on an instance.
# File lib/eagleclaw/callbacks.rb, line 56 def run_proc(meth, recipient = self) if meth.is_a? Symbol recipient.send(meth) else recipient.instance_eval(&meth) end end