Class: ProcessExecuter::Command::Result

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

Overview

A wrapper around Status which adds captured command output

This class is used to represent the result of a subprocess execution, combining the process status with the captured output for easier access and manipulation.

Features:

  • Provides access to the process's status, stdout, and stderr.
  • Allows conversion of stdout and stderr buffers to strings.

Examples:

Create a Result object

status = ProcessExecuter.spawn(*command, timeout:, out:, err:)
result = ProcessExecuter::Command::Result.new(command, status, out_buffer.string, err_buffer.string)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, status, stdout, stderr) ⇒ Result

Create a new Result object

Examples:

status = ProcessExecuter.spawn(*command, timeout:, out:, err:)
Result.new(command, status, out_buffer.string, err_buffer.string)

Parameters:

  • command (Array<String>)

    The command that was executed

  • status (ProcessExecuter::Status)

    The status of the process

  • stdout (String)

    The stdout output from the process

  • stderr (String)

    The stderr output from the process



31
32
33
34
35
36
# File 'lib/process_executer/command/result.rb', line 31

def initialize(command, status, stdout, stderr)
  super(status)
  @command = command
  @stdout = stdout
  @stderr = stderr
end

Instance Attribute Details

#commandArray<String> (readonly)

The command that was run

Examples:

result.command #=> %w[git status]

Returns:

  • (Array<String>)


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

def command
  @command
end

#stderrString (readonly)

The captured stderr output from the process

Examples:

result.stderr #=> "ERROR: file not found"

Returns:

  • (String)


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

def stderr
  @stderr
end

#stdoutString (readonly)

The captured stdout output from the process

Examples:

result.stdout #=> "On branch master\nnothing to commit, working tree clean\n"

Returns:

  • (String)


48
49
50
# File 'lib/process_executer/command/result.rb', line 48

def stdout
  @stdout
end

Instance Method Details

#stderr_to_sString, Object

Return the stderr output as a string

Examples:

When stderr is a StringIO containing "Hello World"

result.stderr_to_s #=> "Hello World"

When stderr is a File object

result.stderr_to_s #=> #<File:/tmp/output.txt>

Returns:

  • (String, Object)

    Returns a String if stderr is a StringIO; otherwise, returns the stderr object



72
73
74
# File 'lib/process_executer/command/result.rb', line 72

def stderr_to_s
  stderr.respond_to?(:string) ? stderr.string : stderr
end

#stdout_to_sString, Object

Return the stdout output as a string

Examples:

When stdout is a StringIO containing "Hello World"

result.stdout_to_s #=> "Hello World"

When stdout is a File object

result.stdout_to_s #=> #<File:/tmp/output.txt>

Returns:

  • (String, Object)

    Returns a String if stdout is a StringIO; otherwise, returns the stdout object



62
63
64
# File 'lib/process_executer/command/result.rb', line 62

def stdout_to_s
  stdout.respond_to?(:string) ? stdout.string : stdout
end