Sha256: ed5920aaee24f39e0651f655ccf9d7d122959e7137fd398c7e38f8dff9ddbdbd

Contents?: true

Size: 1.82 KB

Versions: 17

Compression:

Stored size: 1.82 KB

Contents

module CouchPotato
  module Persistence
    module Callbacks
      def self.included(base)
        base.extend ClassMethods

        base.class_eval do
          attr_accessor :skip_callbacks
          def self.callbacks
            @callbacks ||= {}
            @callbacks[self.name] ||= {:before_validation => [], :before_validation_on_create => [], 
              :before_validation_on_update => [], :before_validation_on_save => [], :before_create => [], 
              :after_create => [], :before_update => [], :after_update => [],
              :before_save => [], :after_save => [],
              :before_destroy => [], :after_destroy => []}
          end
        end
      end

      # Runs all callbacks on a model with the given name, i.g. :after_create.
      # 
      # This method is called by the CouchPotato::Database object when saving/destroying an object 
      def run_callbacks(name)
        return if skip_callbacks
        self.class.callbacks[name].uniq.each do |callback|
          if callback.is_a?(Symbol)
            send callback
          elsif callback.is_a?(Proc)
            callback.call self
          else
            raise "Don't know how to handle callback of type #{name.class.name}"
          end
        end
      end

      module ClassMethods
        [
          :before_validation,
          :before_validation_on_create,
          :before_validation_on_update,
          :before_validation_on_save,
          :before_create,
          :before_save,
          :before_update,
          :before_destroy,
          :after_update,
          :after_save,
          :after_create,
          :after_destroy
        ].each do |callback|
          define_method callback do |*names|
            names.each do |name|
              callbacks[callback] << name
            end
          end
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 3 rubygems

Version Path
andrewtimberlake-couch_potato-0.2.8.1 lib/couch_potato/persistence/callbacks.rb
andrewtimberlake-couch_potato-0.2.8.2 lib/couch_potato/persistence/callbacks.rb
andrewtimberlake-couch_potato-0.2.8.3 lib/couch_potato/persistence/callbacks.rb
andrewtimberlake-couch_potato-0.2.8.4 lib/couch_potato/persistence/callbacks.rb
langalex-couch_potato-0.2.11 lib/couch_potato/persistence/callbacks.rb
langalex-couch_potato-0.2.12 lib/couch_potato/persistence/callbacks.rb
langalex-couch_potato-0.2.8 lib/couch_potato/persistence/callbacks.rb
langalex-couch_potato-0.2.9 lib/couch_potato/persistence/callbacks.rb
couch_potato-0.2.20 lib/couch_potato/persistence/callbacks.rb
couch_potato-0.2.19 lib/couch_potato/persistence/callbacks.rb
couch_potato-0.2.18 lib/couch_potato/persistence/callbacks.rb
couch_potato-0.2.17 lib/couch_potato/persistence/callbacks.rb
couch_potato-0.2.16 lib/couch_potato/persistence/callbacks.rb
couch_potato-0.2.15 lib/couch_potato/persistence/callbacks.rb
couch_potato-0.2.14 lib/couch_potato/persistence/callbacks.rb
couch_potato-0.2.13 lib/couch_potato/persistence/callbacks.rb
couch_potato-0.2.12 lib/couch_potato/persistence/callbacks.rb