Is it possible to use ActiveSupport::Callbacks
on class methods? More specifically, in the module below, where would you put include ActiveSupport::Callbacks
to make define_callbacks
and run_callbacks
share the same state?
module Handler
extend ActiveSupport::Concern
# If you put it here, define_callbacks is defined but run_callbacks is not
# include ActiveSupport::Callbacks
included do
class_eval do
define_callbacks :handle
end
end
module ClassMethods
# If you put it here, run_callbacks is defined but define_callbacks is not
# include ActiveSupport::Callbacks
def handle(task)
run_callbacks :handle do
p task
end
end
end
end
class HandlerExample
include Handler
end
Update
If it is possible, which I have not been able to ascertain, it certainly is not something ActiveSupport::Callbacks
was designed for. Best to create a stateless instance object in #handle
.
While the whole idea is a bit awkward IMHO, you're already putting define_callbacks in the wrong place.. You should have put it in the eigenclass, e.g.
eval << -EOF class << self ; define_callbacks.. ; EOF
and then you can do "send :extend, ActiveSupport::Callbacks". That might even work.
But I suggest that you re-think it, and settle with a standard implementation, where the callbacks are invoked on the instance.