2007-11-09 09:59:15 -05:00
require 'observer'
2008-06-28 02:29:47 -04:00
require 'singleton'
2009-06-11 14:21:56 -04:00
require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/array/wrap'
2007-11-09 09:59:15 -05:00
module ActiveModel
module Observing
2009-06-11 00:35:34 -04:00
extend ActiveSupport :: Concern
included do
extend Observable
end
2007-11-09 09:59:15 -05:00
module ClassMethods
2009-06-11 00:35:34 -04:00
# 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.
2007-11-09 09:59:15 -05:00
def observers = ( * values )
@observers = values . flatten
end
2009-06-11 00:35:34 -04:00
# Gets the current observers.
def observers
@observers || = [ ]
end
# Instantiate the global Active Record observers.
2007-11-09 09:59:15 -05:00
def instantiate_observers
observers . each { | o | instantiate_observer ( o ) }
end
2009-06-11 00:35:34 -04:00
protected
def instantiate_observer ( observer )
# string/symbol
if observer . respond_to? ( :to_sym )
observer = 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
# Notify observers when the observed class is subclassed.
def inherited ( subclass )
super
changed
notify_observers :observed_class_inherited , subclass
2007-11-09 09:59:15 -05:00
end
end
end
class Observer
include Singleton
class << self
# Attaches the observer to the supplied model classes.
def observe ( * models )
2009-06-11 00:35:34 -04:00
models . flatten!
models . collect! { | model | model . respond_to? ( :to_sym ) ? model . to_s . camelize . constantize : model }
define_method ( :observed_classes ) { models }
2007-11-09 09:59:15 -05:00
end
2009-06-11 00:35:34 -04:00
def observed_classes
Array . wrap ( observed_class )
2007-11-09 09:59:15 -05:00
end
# The class observed by default is inferred from the observer's class name:
2009-06-11 00:35:34 -04:00
# assert_equal Person, PersonObserver.observed_class
2007-11-09 09:59:15 -05:00
def observed_class
2009-06-11 00:35:34 -04:00
if observed_class_name = name [ / (.*)Observer / , 1 ]
2007-11-09 09:59:15 -05:00
observed_class_name . constantize
else
nil
end
end
end
# Start observing the declared classes and their subclasses.
def initialize
2009-06-11 00:35:34 -04:00
observed_classes . each { | klass | add_observer! ( klass ) }
end
def observed_classes
self . class . observed_classes
2007-11-09 09:59:15 -05:00
end
# Send observed_method(object) if the method exists.
def update ( observed_method , object ) #:nodoc:
send ( observed_method , object ) if respond_to? ( observed_method )
end
# Special method sent by the observed class when it is inherited.
# Passes the new subclass.
def observed_class_inherited ( subclass ) #:nodoc:
self . class . observe ( observed_classes + [ subclass ] )
2009-06-11 00:35:34 -04:00
add_observer! ( subclass )
2007-11-09 09:59:15 -05:00
end
2009-06-11 00:35:34 -04:00
protected
def add_observer! ( klass )
klass . add_observer ( self )
end
2007-11-09 09:59:15 -05:00
end
2009-06-11 00:35:34 -04:00
end