Class: ProcessExecuter::ResultWithCapture

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/process_executer/result_with_capture.rb

Overview

A decorator for ProcessExecuter::Result that adds the following attributes:

  • stdout: the captured stdout of the command
  • stderr: the captured stderr of the command

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result, stdout_buffer:, stderr_buffer:) ⇒ ResultWithCapture

Create a new ResultWithCapture object

Examples:

manually create a ResultWithCapture instance

stdout_buffer = StringIO.new
stderr_buffer = StringIO.new
command = ['echo HELLO; echo ERROR >&2']
result = ProcessExecuter.run(*command, out: stdout_buffer, err: stderr_buffer)
result_with_capture = ProcessExecuter::ResultWithCapture.new(result, stdout_buffer:, stderr_buffer:)

# Normally, you would use the `run_with_capture` method to create a
# ResultWithCapture instance. The above code is equivalent to:

result_with_capture = ProcessExecuter.run_with_capture('echo HELLO; echo ERROR >&2')

Parameters:

  • result (ProcessExecuter::Result)

    the result to delegate to

  • stdout_buffer (StringIO)

    the captured stdout

  • stderr_buffer (StringIO)

    the captured stderr



32
33
34
35
36
# File 'lib/process_executer/result_with_capture.rb', line 32

def initialize(result, stdout_buffer:, stderr_buffer:)
  super(result)
  @stdout_buffer = stdout_buffer
  @stderr_buffer = stderr_buffer
end

Instance Attribute Details

#stderr_bufferStringIO (readonly)

The buffer used to capture stderr

Examples:

result.stderr_buffer #=> #<StringIO:0x00007f8c1b0a2d80>

Returns:

  • (StringIO)


54
55
56
# File 'lib/process_executer/result_with_capture.rb', line 54

def stderr_buffer
  @stderr_buffer
end

#stdout_bufferStringIO (readonly)

The buffer used to capture stdout

Examples:

result.stdout_buffer #=> #<StringIO:0x00007f8c1b0a2d80>

Returns:

  • (StringIO)


42
43
44
# File 'lib/process_executer/result_with_capture.rb', line 42

def stdout_buffer
  @stdout_buffer
end

Instance Method Details

#stderrString

The captured stderr of the command

Examples:

result.stderr #=> "ERROR\n"

Returns:

  • (String)


60
# File 'lib/process_executer/result_with_capture.rb', line 60

def stderr = @stderr_buffer.string

#stdoutString

The captured stdout of the command

Examples:

result.stdout #=> "HELLO\n"

Returns:

  • (String)


48
# File 'lib/process_executer/result_with_capture.rb', line 48

def stdout = @stdout_buffer.string