Module: AntMapper::Observing::ClassMethods

Defined in:
lib/ant_mapper/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/ant_mapper/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/ant_mapper/observer.rb', line 29

def observers
  @observers ||= []
end

#observers=(*observers) ⇒ Object

激活观察器. 示例:

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

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

# 通上
AntMapper::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/ant_mapper/observer.rb', line 24

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