Module: CassandraMapper::Support::Observing::ClassMethods

Defined in:
lib/cassandra_mapper/support/observing.rb

Instance Method Summary collapse

Instance Method Details

#add_observer(observer) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/cassandra_mapper/support/observing.rb', line 43

def add_observer(observer)
  unless observer.respond_to? :update
    raise ArgumentError, "observer needs to respond to `update'"
  end
  @observer_instances ||= []
  @observer_instances << observer
end

#count_observersObject



59
60
61
# File 'lib/cassandra_mapper/support/observing.rb', line 59

def count_observers
  @observer_instances.size
end

#instantiate_observersObject

Instantiate the global Active Record observers.



39
40
41
# File 'lib/cassandra_mapper/support/observing.rb', line 39

def instantiate_observers
  observers.each { |o| instantiate_observer(o) }
end

#notify_observers(*arg) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/cassandra_mapper/support/observing.rb', line 51

def notify_observers(*arg)
  if defined? @observer_instances
    for observer in @observer_instances
      observer.update(*arg)
    end
  end
end

#observersObject

Gets the current observers.



34
35
36
# File 'lib/cassandra_mapper/support/observing.rb', line 34

def observers
  @observers ||= []
end

#observers=(*values) ⇒ Object

Active Model Observers Activation

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.



29
30
31
# File 'lib/cassandra_mapper/support/observing.rb', line 29

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