ActiveSupport :: Class Method Callbacks

Can I use ActiveSupport::Callbacks methods for a class? 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 this is possible, which I could not install, this, of course, is not what ActiveSupport::Callbacks was designed ActiveSupport::Callbacks . It is best to create an instance object without saving in #handle .

+4
source share
1 answer

While the whole idea is a bit inconvenient IMHO, you already put define_callbacks in the wrong place. You should have put it in eigenclass, for example.

eval <-EOF class <self; define_callbacks ..; Eof

and then you can run the command "send: extend, ActiveSupport :: Callbacks". It might even work.

But I suggest you think about it and decide on a standard implementation where callbacks are called on the instance.

+2
source

Source: https://habr.com/ru/post/1439515/


All Articles