Module: ActiveModel::Observing::ClassMethods

Defined in:
lib/active_model/observing.rb

Instance Method Summary collapse

Instance Method Details

#add_observer(observer) ⇒ Object



42
43
44
45
46
47
48
# 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

#count_observersObject



58
59
60
# File 'lib/active_model/observing.rb', line 58

def count_observers
  @observer_instances.size
end

#instantiate_observersObject

Instantiate the global Active Record observers.



38
39
40
# File 'lib/active_model/observing.rb', line 38

def instantiate_observers
  observers.each { |o| instantiate_observer(o) }
end

#notify_observers(*arg) ⇒ Object



50
51
52
53
54
55
56
# 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

#observersObject

Gets the current observers.



33
34
35
# File 'lib/active_model/observing.rb', line 33

def observers
  @observers ||= []
end

#observers=(*values) ⇒ Object

Active Model Observers Activation

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.



28
29
30
# File 'lib/active_model/observing.rb', line 28

def observers=(*values)
  @observers = values.flatten
end