Module: CouchPotato::Persistence::Callbacks
- Defined in:
- lib/couch_potato/persistence/callbacks.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#run_callbacks(name) ⇒ Object
Runs all callbacks on a model with the given name, e.g.
Class Method Details
.included(base) ⇒ Object
:nodoc:
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/couch_potato/persistence/callbacks.rb', line 4 def self.included(base) #:nodoc: base.extend ClassMethods base.class_eval do attr_accessor :skip_callbacks def self.callbacks #:nodoc: @callbacks ||= {: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 |
Instance Method Details
#run_callbacks(name) ⇒ Object
Runs all callbacks on a model with the given name, e.g. :after_create.
This method is called by the CouchPotato::Database object when saving/destroying an object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/couch_potato/persistence/callbacks.rb', line 22 def run_callbacks(name) return if skip_callbacks callbacks = self.class.ancestors.map do |clazz| clazz.callbacks[name] if clazz.respond_to?(:callbacks) end.flatten.compact.uniq callbacks.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 |