Module: Wonkavision::Plugins::Callbacks::ClassMethods

Defined in:
lib/wonkavision/plugins/callbacks.rb

Instance Method Summary collapse

Instance Method Details

#define_wonkavision_callbacks(*callbacks) ⇒ Object

The ODM library may already have taken care of mixing in callback functioanlity, in which case we’ll just use that



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wonkavision/plugins/callbacks.rb', line 20

def define_wonkavision_callbacks(*callbacks)
  callbacks.each do |callback|
    class_eval <<-"end_eval"
      def self.#{callback}(*methods, &block)
        callbacks = CallbackChain.build(:#{callback}, *methods, &block)
        @#{callback}_callbacks ||= CallbackChain.new
        @#{callback}_callbacks.concat callbacks
      end

      def self.#{callback}_callback_chain
        @#{callback}_callbacks ||= CallbackChain.new

        if superclass.respond_to?(:#{callback}_callback_chain)
          CallbackChain.new(
            superclass.#{callback}_callback_chain +
            @#{callback}_callbacks
          )
        else
          @#{callback}_callbacks
        end
      end
    end_eval
  end
end