Module: Observatory::Observer::ClassMethods

Defined in:
lib/observatory/observer.rb

Instance Method Summary collapse

Instance Method Details

#observe(event_name) ⇒ Object



86
87
88
# File 'lib/observatory/observer.rb', line 86

def observe(event_name)
  @observer_next_event_name_to_observe = event_name
end

#observers_to_set_upObject



82
83
84
# File 'lib/observatory/observer.rb', line 82

def observers_to_set_up
  @observers_to_set_up ||= {}
end

#overwrite_initializeObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/observatory/observer.rb', line 63

def overwrite_initialize
  class_eval do
    unless method_defined?(:initialize_and_setup_observers)
      define_method(:initialize_and_setup_observers) do |*args|
        initialize_without_observer *args
        self.class.observers_to_set_up.each_pair do |name, methods|
          methods.each do |m|
            @dispatcher.connect(name, method(m))
          end
        end
      end
    end
    if instance_method(:initialize) != instance_method(:initialize_and_setup_observers)
      alias_method :initialize_without_observer, :initialize
      alias_method :initialize, :initialize_and_setup_observers
    end
  end
end