Sha256: 2b94c7562a3068e362a2e1d04aa5e7f10fef01f311d1b6a86fa174cb39bc0a1d

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

require 'state_inspector'
require 'state_inspector/observers'
module StateInspector
  module Helper
    def self.included(base)
      base.include Observers
    end

    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[obj]
    ensure
      obj.toggle_informant
      (old_observer.nil? ? Reporter.drop(obj) : Reporter[obj] = old_observer) if observer
      value
    end

    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[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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
state_inspector-1.0.4 lib/state_inspector/helper.rb