Module: CouchPotato::Persistence::Callbacks

Defined in:
lib/couch_potato/persistence/callbacks.rb

Defined Under Namespace

Modules: ClassMethods Classes: Callback

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/couch_potato/persistence/callbacks.rb', line 47

def self.included(base)
  base.extend ClassMethods

  base.class_eval do
    attr_accessor :skip_callbacks
    def self.callbacks
      @callbacks ||= {}
      @callbacks[self.name] ||= {: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

Instance Method Details

#run_callbacks(name, database) ⇒ Object

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



66
67
68
69
70
71
# File 'lib/couch_potato/persistence/callbacks.rb', line 66

def run_callbacks(name, database)
  return if skip_callbacks
  self.class.callbacks[name].uniq.each do |callback|
    Callback.new(self, callback, database).run
  end
end