Module: TaskJuggler::StdIoWrapper

Included in:
SheetReceiver, SheetSender
Defined in:
lib/taskjuggler/StdIoWrapper.rb

Overview

This module provides just one method to run the passed block. It will capture all content that will be send to $stdout and $stderr by the block. I can also feed the String provided by stdIn to $stdin of the block.

Defined Under Namespace

Classes: Results

Instance Method Summary collapse

Instance Method Details

#stdIoWrapper(stdIn = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/taskjuggler/StdIoWrapper.rb', line 23

def stdIoWrapper(stdIn = nil)
  # Save the old $stdout and $stderr and replace them with StringIO
  # objects to capture the output.
  oldStdOut = $stdout
  oldStdErr = $stderr
  $stdout = (out = StringIO.new)
  $stderr = (err = StringIO.new)

  # If the caller provided a String to feed into $stdin, we replace that
  # as well.
  if stdIn
    oldStdIn = $stdin
    $stdin = StringIO.new(stdIn)
  end

  begin
    # Call the block with the hooked up IOs.
    res = yield
  rescue RuntimeError
    # Blocks that are called this way usually return 0 on success and 1 on
    # errors.
    res = 1
  ensure
    # Restore the stdio channels no matter what.
    $stdout = oldStdOut
    $stderr = oldStdErr
    $stdin = oldStdIn if stdIn
  end

  # Return the return value of the block and the $stdout and $stderr
  # captures.
  Results.new(res, out.string, err.string)
end