Module: Jetpants::CallbackHandler

Included in:
DB, Host, Pool, Shard, Table
Defined in:
lib/jetpants/callback.rb

Overview

If you include CallbackHandler as a mix-in, it grants the base class support for Jetpants callbacks, as defined here:

If you invoke a method “foo”, Jetpants will first automatically call any “before_foo” methods that exist in the class or its superclasses. You can even define multiple methods named before_foo (in the same class!) and they will each be called. In other words, Jetpants callbacks “stack” instead of overriding each other.

After calling any/all before_foo methods, the foo method is called, followed by all after_foo methods in the same manner.

If any before_foo method raises a CallbackAbortError, subsequent before_foo methods will NOT be called, NOR will foo itself nor any after_foo methods.

If any after_foo method raises a CallbackAbortError, subsequent after_foo methods will NOT be called.

You may preceed the definition of a callback method with “callback_priority 123” to set an explicit priority (higher = called first) for subsequent callbacks. The default priority is 100.

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jetpants/callback.rb', line 28

def self.included(base)
  base.class_eval do
    class << self
      # Set the priority (higher = called first) for any subsequent callbacks defined in the current class.
      def callback_priority(value)
        @callback_priority = value
      end

      def method_added(name)
        # Intercept before_* and after_* methods and create corresponding Callback objects
        if name.to_s.start_with? 'before_', 'after_'
          Callback.new self, name.to_s.split('_', 2)[1].to_sym, name.to_s.split('_', 2)[0].to_sym, @callback_priority
        
        # Intercept redefinitions of methods we've already wrapped, so we can
        # wrap them again
        elsif Callback.wrapped? self, name
          Callback.wrap_method self, name
        end
      end
    end
    
    # Default priority for callbacks is 100
    @callback_priority = 100
  end
end