Module: ActiveObject::Observing::ClassMethods

Defined in:
lib/active_object/observer.rb

Instance Method Summary collapse

Instance Method Details

#instantiate_observersObject

实例化全部Ant Mapper观察器.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_object/observer.rb', line 34

def instantiate_observers
  return if @observers.blank?
  @observers.each do |observer|
    if observer.respond_to?(:to_sym) # Symbol or String
      observer.to_s.camelize.constantize.instance
    elsif observer.respond_to?(:instance)
      observer.instance
    else
      raise ArgumentError, "#{observer} must be a lowercase, underscored class name (or an instance of the class itself) responding to the instance method. Example: Person.observers = :big_brother # calls BigBrother.instance"
    end
  end
end

#observersObject

获得当前observers.



29
30
31
# File 'lib/active_object/observer.rb', line 29

def observers
  @observers ||= []
end

#observers=(*observers) ⇒ Object

激活观察器. 示例:

# 调用PersonObserver.instance
ActiveObject::Base.observers = :person_observer

# 调用Cacher.instance 和 GarbageCollector.instance
ActiveObject::Base.observers = :cacher, :garbage_collector

# 同上
ActiveObject::Base.observers = Cacher, GarbageCollector

Note: Setting this does not instantiate the observers yet. instantiate_observers is called during startup, and before each development request.



24
25
26
# File 'lib/active_object/observer.rb', line 24

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