Module: MongoMapper::Plugins::Callbacks::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#define_callbacks(*callbacks) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 24

def define_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