lib/active_model/observing.rb in activemodel-3.0.0.beta3 vs lib/active_model/observing.rb in activemodel-3.0.0.beta4

- old
+ new

@@ -1,19 +1,15 @@ -require 'observer' require 'singleton' require 'active_support/core_ext/array/wrap' require 'active_support/core_ext/module/aliasing' require 'active_support/core_ext/string/inflections' +require 'active_support/core_ext/string/conversions' module ActiveModel module Observing extend ActiveSupport::Concern - included do - extend Observable - end - module ClassMethods # Activates the observers assigned. Examples: # # # Calls PersonObserver.instance # ActiveRecord::Base.observers = :person_observer @@ -39,10 +35,30 @@ # Instantiate the global Active Record observers. def instantiate_observers observers.each { |o| instantiate_observer(o) } end + def add_observer(observer) + unless observer.respond_to? :update + raise ArgumentError, "observer needs to respond to `update'" + end + @observer_instances ||= [] + @observer_instances << observer + end + + def notify_observers(*arg) + if defined? @observer_instances + for observer in @observer_instances + observer.update(*arg) + end + end + end + + def count_observers + @observer_instances.size + end + protected def instantiate_observer(observer) #:nodoc: # string/symbol if observer.respond_to?(:to_sym) observer = observer.to_s.camelize.constantize.instance @@ -54,11 +70,10 @@ end # Notify observers when the observed class is subclassed. def inherited(subclass) super - changed notify_observers :observed_class_inherited, subclass end end private @@ -68,10 +83,9 @@ # notify_observers(:before_save) # ... # notify_observers(:after_save) # end def notify_observers(method) - self.class.changed self.class.notify_observers(method, self) end end # Observer classes respond to lifecycle callbacks to implement trigger-like