Sha256: 38be13d07e9bdfa495985d700c89e4866bbd0fb5c3040200092c078d3d64d536

Contents?: true

Size: 1021 Bytes

Versions: 1

Compression:

Stored size: 1021 Bytes

Contents

require_relative 'state_inspector'
require_relative 'reporter'

module StateInspector
  module Snoop
    def Snoop.extended(base)
      base.include ClassMethods
    end

    def attr_writer attr_name
      define_method("#{attr_name}=") do |value|
        tell_si __method__.to_s.chop,
          instance_variable_get("@#{attr_name.to_s}"),
          value 

        instance_variable_set("@#{attr_name.to_s}", value)
      end
      nil
    end

    def attr_accessor attr_name
      define_method("#{attr_name}") do
        instance_variable_get("@#{attr_name.to_s}")
      end

      self.attr_writer(attr_name)
    end

    module ClassMethods
      def state_inspector
        StateInspector
      end

      def tell_si what, old, new
        state_inspector.report( self, "@#{what.to_s}", old, new ) if informant?
      end

      def toggle_informant
        @informant = !@informant
      end

      def informant?
        @informant || self.class.instance_variable_get(:@informant)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
state_inspector-0.8.0 lib/state_inspector/snoop.rb