6 class ObserverCannotObserveTwiceError < StandardError
7 def initialize(message = 'observer cannot observe twice')
13 raise ObserverCannotObserveTwiceError if @target
15 make_observable(target)
24 instances[o.object_id] = o
29 def make_observable(target)
30 methods_to_observe.each do |method|
31 target.instance_eval %{
32 def #{method}(*args, &block)
33 observer = #{self.class}.instances[#{self.object_id}]
34 observer.send(:before_#{method}, *args, &block) if observer.respond_to? :before_#{method}
35 observer.result = super
36 observer.send(:after_#{method}, *args, &block) if observer.respond_to? :after_#{method}
43 def methods_to_observe
44 (methods_for(:before) + methods_for(:after)).uniq
48 methods.grep(/^#{name}_/).map { |n| n.to_s.gsub(/^#{name}_/, '').intern }