Sha256: 2769a8f867c5629e3b6a617343bed1bf04ede499fa151c9bf2e4870b28250252

Contents?: true

Size: 509 Bytes

Versions: 6

Compression:

Stored size: 509 Bytes

Contents

require 'stringio'

class RackConsole
  class OutputCapture
    def initialize
      @old = $stdout
      @io = ::StringIO.new
      @main_thread = ::Thread.current
    end

    def write(value)
      io.write(value)
    end

    def capture
      $stdout = self
      yield
    ensure
      $stdout = @old
    end

    def output
      @io.rewind
      @io.read
    end

    private

    def io
      ::Thread.current == @main_thread || @main_thread[:rack_console_capture_all] ? @io : @old
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rack_web_console-0.2.0 lib/rack_console/output_capture.rb
rack_web_console-0.1.4 lib/rack_console/output_capture.rb
rack_web_console-0.1.3 lib/rack_console/output_capture.rb
rack_web_console-0.1.2 lib/rack_console/output_capture.rb
rack_web_console-0.1.1 lib/rack_console/output_capture.rb
rack_web_console-0.1.0 lib/rack_console/output_capture.rb