Module: ActiveModel::Observing::ClassMethods
- Defined in:
- lib/active_model/observing.rb
Instance Method Summary (collapse)
-
- (Object) add_observer(observer)
Add a new observer to the pool.
-
- (Object) count_observers
Total number of observers.
-
- (Object) instantiate_observers
Instantiate the global observers.
-
- (Object) notify_observers(*arg)
Notify list of observers of a change.
-
- (Object) observer_instances
Gets the current observer instances.
-
- (Object) observers
Gets an array of observers observing this model.
-
- (Object) observers=(*values)
Active Model Observers Activation.
Instance Method Details
- (Object) add_observer(observer)
Add a new observer to the pool. The new observer needs to respond to 'update', otherwise it raises an ArgumentError exception.
65 66 67 68 69 70 |
# File 'lib/active_model/observing.rb', line 65 def add_observer(observer) unless observer.respond_to? :update raise ArgumentError, "observer needs to respond to `update'" end observer_instances << observer end |
- (Object) count_observers
Total number of observers.
78 79 80 |
# File 'lib/active_model/observing.rb', line 78 def count_observers observer_instances.size end |
- (Object) instantiate_observers
Instantiate the global observers.
58 59 60 |
# File 'lib/active_model/observing.rb', line 58 def instantiate_observers observers.each { |o| instantiate_observer(o) } end |
- (Object) notify_observers(*arg)
Notify list of observers of a change.
73 74 75 |
# File 'lib/active_model/observing.rb', line 73 def notify_observers(*arg) observer_instances.each { |observer| observer.update(*arg) } end |
- (Object) observer_instances
Gets the current observer instances.
53 54 55 |
# File 'lib/active_model/observing.rb', line 53 def observer_instances @observer_instances ||= [] end |
- (Object) observers
Gets an array of observers observing this model. The array also provides enable and disable methods that allow you to selectively enable and disable observers. (see ActiveModel::ObserverArray.enable and ActiveModel::ObserverArray.disable for more on this)
48 49 50 |
# File 'lib/active_model/observing.rb', line 48 def observers @observers ||= ObserverArray.new(self) end |
- (Object) observers=(*values)
Active Model Observers Activation
Activates the observers assigned. Examples:
class ORM
include ActiveModel::Observing
end
# Calls PersonObserver.instance
ORM.observers = :person_observer
# Calls Cacher.instance and GarbageCollector.instance
ORM.observers = :cacher, :garbage_collector
# Same as above, just using explicit class references
ORM.observers = Cacher, GarbageCollector
Note: Setting this does not instantiate the observers yet. instantiate_observers is called during startup, and before each development request.
39 40 41 |
# File 'lib/active_model/observing.rb', line 39 def observers=(*values) observers.replace(values.flatten) end |