Module: ActiveModel::Observing::ClassMethods

### Home Classes Methods

### In Files

- lib/active_model/observing.rb

### Methods

- #add_observer
- #count_observers
- #inherited
- #instantiate_observers
- #notify_observers
- #observer_instances
- #observers
- #observers=

### Class/Module Index

Quicksearch

- ActiveModel
- ActiveModel::AttributeMethods
- ActiveModel::AttributeMethods::ClassMethods
- ActiveModel::AttributeMethods::ClassMethods::AttributeMethodMatcher
- ActiveModel::BlockValidator
- ActiveModel::Callbacks
- ActiveModel::Conversion
- ActiveModel::Dirty
- ActiveModel::EachValidator
- ActiveModel::Errors
- ActiveModel::Lint
- ActiveModel::Lint::Tests
- ActiveModel::MassAssignmentSecurity
- ActiveModel::MassAssignmentSecurity::BlackList
- ActiveModel::MassAssignmentSecurity::ClassMethods
- ActiveModel::MassAssignmentSecurity::Error
- ActiveModel::MassAssignmentSecurity::LoggerSanitizer
- ActiveModel::MassAssignmentSecurity::PermissionSet
- ActiveModel::MassAssignmentSecurity::Sanitizer
- ActiveModel::MassAssignmentSecurity::StrictSanitizer
- ActiveModel::MassAssignmentSecurity::WhiteList
- ActiveModel::MissingAttributeError
- ActiveModel::Name
- ActiveModel::Naming
- ActiveModel::Observer
- ActiveModel::ObserverArray
- ActiveModel::Observing
- ActiveModel::Observing::ClassMethods
- ActiveModel::SecurePassword
- ActiveModel::SecurePassword::ClassMethods
- ActiveModel::SecurePassword::InstanceMethodsOnActivation
- ActiveModel::Serialization
- ActiveModel::Serializers
- ActiveModel::Serializers::JSON
- ActiveModel::Serializers::Xml
- ActiveModel::StrictValidationFailed
- ActiveModel::Translation
- ActiveModel::Validations
- ActiveModel::Validations::AcceptanceValidator
- ActiveModel::Validations::Callbacks
- ActiveModel::Validations::Callbacks::ClassMethods
- ActiveModel::Validations::ClassMethods
- ActiveModel::Validations::ConfirmationValidator
- ActiveModel::Validations::ExclusionValidator
- ActiveModel::Validations::FormatValidator
- ActiveModel::Validations::HelperMethods
- ActiveModel::Validations::InclusionValidator
- ActiveModel::Validations::LengthValidator
- ActiveModel::Validations::NumericalityValidator
- ActiveModel::Validations::PresenceValidator
- ActiveModel::Validations::WithValidator
- ActiveModel::Validator

No matching classes.

# ActiveModel::Observing::ClassMethods

### Public Instance Methods

add_observer(observer) click to toggle source

Add a new observer to the pool. The new observer needs to respond to 'update', otherwise it raises an `ArgumentError` exception.

    # File lib/active_model/observing.rb, line 65
    def add_observer(observer)
      unless observer.respond_to? :update
        raise ArgumentError, "observer needs to respond to `update'"
      end
      observer_instances << observer
    end

count_observers() click to toggle source

Total number of observers.

    # File lib/active_model/observing.rb, line 78
    def count_observers
      observer_instances.size
    end

instantiate_observers() click to toggle source

Instantiate the global observers.

    # File lib/active_model/observing.rb, line 58
    def instantiate_observers
      observers.each { |o| instantiate_observer(o) }
    end

notify_observers(*arg) click to toggle source

Notify list of observers of a change.

    # File lib/active_model/observing.rb, line 73
    def notify_observers(*arg)
      observer_instances.each { |observer| observer.update(*arg) }
    end

observer_instances() click to toggle source

Gets the current observer instances.

    # File lib/active_model/observing.rb, line 53
    def observer_instances
      @observer_instances ||= []
    end

observers() click to toggle source

Gets an array of observers observing this model. The array also provides `enable` and `disable` methods that allow you to selectively enable and disable observers. (see `ActiveModel::ObserverArray.enable` and `ActiveModel::ObserverArray.disable` for more on this)

    # File lib/active_model/observing.rb, line 48
    def observers
      @observers ||= ObserverArray.new(self)
    end

observers=(*values) click to toggle source

## Active Model Observers Activation

Activates the observers assigned. Examples:

    class ORM
      include ActiveModel::Observing
    end

    # Calls PersonObserver.instance
    ORM.observers = :person_observer

    # Calls Cacher.instance and GarbageCollector.instance
    ORM.observers = :cacher, :garbage_collector

    # Same as above, just using explicit class references
    ORM.observers = Cacher, GarbageCollector

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

    # File lib/active_model/observing.rb, line 39
    def observers=(*values)
      observers.replace(values.flatten)
    end

### Protected Instance Methods

inherited(subclass) click to toggle source

Notify observers when the observed class is subclassed.

    # File lib/active_model/observing.rb, line 99
    def inherited(subclass)
      super
      notify_observers :observed_class_inherited, subclass
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.