Class: MongoMapper::Observer

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mongo_mapper/observing.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObserver

Returns a new instance of Observer.



33
34
35
# File 'lib/mongo_mapper/observing.rb', line 33

def initialize
  Set.new(observed_classes).each { |klass| add_observer! klass }
end

Class Method Details

.observe(*models) ⇒ Object



18
19
20
21
22
# File 'lib/mongo_mapper/observing.rb', line 18

def observe(*models)
  models.flatten!
  models.collect! { |model| model.is_a?(Symbol) ? model.to_s.camelize.constantize : model }
  define_method(:observed_classes) { Set.new(models) }
end

.observed_classObject



24
25
26
27
28
29
30
# File 'lib/mongo_mapper/observing.rb', line 24

def observed_class
  if observed_class_name = name[/(.*)Observer/, 1]
    observed_class_name.constantize
  else
    nil
  end
end

Instance Method Details

#update(observed_method, object) ⇒ Object

:nodoc:



37
38
39
# File 'lib/mongo_mapper/observing.rb', line 37

def update(observed_method, object) #:nodoc:
  send(observed_method, object) if respond_to?(observed_method)
end