Sha256: dea2c1b8351744ffaf00bd38c894c2c9413199f5dbdd9f566eff037d25c557a0

Contents?: true

Size: 899 Bytes

Versions: 1

Compression:

Stored size: 899 Bytes

Contents

module Vizsla
  class Recorder
    THREAD_LOCAL_KEY = :_vizsla_current
    LOCK = Mutex.new

    class << self
      def current
        LOCK.synchronize do
          Thread.current[THREAD_LOCAL_KEY]
        end
      end

      def current=(val)
        Thread.current[THREAD_LOCAL_KEY] = val
      end

      def start_recording
        self.current = {}
      end

      def recording?
        !self.current.nil?
      end

      def add_event(event)
        return unless self.recording?
        self.current[event.recorder_type] ||= []
        self.current[event.recorder_type] << event.prettify_data
      end
      alias_method :<<, :add_event

      def events
        self.current
      end

      def stop_recording
        LOCK.synchronize do
          Thread.current[THREAD_LOCAL_KEY] = nil
        end
      end

      at_exit do
        self.stop_recording
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vizsla-0.0.2 lib/vizsla/recorder.rb