Module: StateInspector::Helper

Defined in:
lib/state_inspector/helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
# File 'lib/state_inspector/helper.rb', line 5

def self.included(base)
  base.include Observers
end

Instance Method Details

#toggle_snoop(obj, observer = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/state_inspector/helper.rb', line 9

def toggle_snoop(obj, observer=nil)
  if observer
    old_observer = Reporter.has_observer?(obj) ? Reporter[obj] : nil
    Reporter[obj] = observer
  end
  obj.toggle_informant
  value = yield Reporter.get(obj)
ensure
  obj.toggle_informant
  (old_observer.nil? ? Reporter.drop(obj) : Reporter[obj] = old_observer) if observer
  value
end

#toggle_snoop_clean(obj, observer = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/state_inspector/helper.rb', line 22

def toggle_snoop_clean(obj, observer=nil)
  obj.state_inspector.skip_setter_snoops
  if observer
    old_observer = Reporter.has_observer?(obj) ? Reporter[obj] : nil
    Reporter[obj] = observer
  end
  obj.toggle_informant
  value = yield Reporter.get(obj)
ensure
  obj.toggle_informant
  si = obj.respond_to?(:class_exec) ? obj : obj.class
  obj.state_inspector.restore_methods(*si.instance_variable_get(:@state_inspector).keys)
  si.remove_instance_variable(:@state_inspector) 
  (old_observer.nil? ? Reporter.drop(obj) : Reporter[obj] = old_observer) if observer
  value
end