# File lib/active_model/observing.rb, line 42 def add_observer(observer) unless observer.respond_to? :update raise ArgumentError, "observer needs to respond to `update'" end @observer_instances ||= [] @observer_instances << observer end
# File lib/active_model/observing.rb, line 58 def count_observers @observer_instances.size end
Instantiate the global Active Record observers.
# File lib/active_model/observing.rb, line 38 def instantiate_observers observers.each { |o| instantiate_observer(o) } end
# File lib/active_model/observing.rb, line 50 def notify_observers(*arg) if defined? @observer_instances for observer in @observer_instances observer.update(*arg) end end end
Gets the current observers.
# File lib/active_model/observing.rb, line 33 def observers @observers ||= [] end
Activates the observers assigned. Examples:
# Calls PersonObserver.instance ActiveRecord::Base.observers = :person_observer # Calls Cacher.instance and GarbageCollector.instance ActiveRecord::Base.observers = :cacher, :garbage_collector # Same as above, just using explicit class references ActiveRecord::Base.observers = Cacher, GarbageCollector
Note: Setting this does not instantiate the observers yet. instantiate_observers is called during startup, and before each development request.
# File lib/active_model/observing.rb, line 28 def observers=(*values) @observers = values.flatten end
Generated with the Darkfish Rdoc Generator 2.