Sha256: cdb24d608e9fdd29e92a13e278e75e77919b3b088405aadc3f1a463abee8b85c

Contents?: true

Size: 824 Bytes

Versions: 1

Compression:

Stored size: 824 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[:events] ||= []
        self.current[:events] << event.data_hash
      end
      alias_method :<<, :add_event

      def events
        self.current[:events]
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tracebin-0.0.7 lib/vizsla/recorder.rb