4

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.

Sim
  • 13,147
  • 9
  • 66
  • 95

1 Answers1

2

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.

Roman
  • 13,100
  • 2
  • 47
  • 63
  • I did try putting them in the eigenclass and that did not work either. Looking at object IDs it seems that the way `ActiveSupport::Concern` and `ActiveSupport::Callbacks` interact does not work well for class methods. Earlier today I arrived at the same conclusion as what you suggested and created an instance object for the handler. It's a waste, because there is no state outside of the task but it's cleaner and less bug-prone than me building a callback implementation that works well for class methods. – Sim Oct 14 '12 at 03:54