Class: RubyGit::CommandLine::Result

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/ruby_git/command_line/result.rb

Overview

The result of running a git command

Adds stdout and stderr processing to the ProcessExecuter::ResultWithCapture class.

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ RubyGit::CommandLine::Result

Initialize a new result object

Examples:

result = Git::CommandLine.run_with_capture('git', 'status')
RubyGit::CommandLine::Result.new(result)

Parameters:

  • result (ProcessExecuter::ResultWithCapture)

    The result of running the command



# File 'lib/ruby_git/command_line/result.rb', line 15

Instance Method Details

#process_stderr {|stderr, result| ... } ⇒ self

Process the captured stderr output

Examples:

result = RubyGit::CommandLine::Result.new(
  ProcessExecuter.run_with_capture('echo hello >&2')
)
result.stderr #=> "hello\n"
result.process_stderr { |stderr, _result| stderr.upcase }
result.stderr #=> "HELLO\n"
result.unprocessed_stderr #=> "hello\n"

Chain processing

result = RubyGit::CommandLine::Result.new(
  ProcessExecuter.run_with_capture('echo hello >&2')
)
result.stderr #=> "hello\n"
# Here is the chain processing:
result.process_stderr { |s| s.upcase }.process_stderr { |s| s.reverse }
result.stderr #=> "OLLEH\n"
result.unprocessed_stderr #=> "hello\n"

Yields:

  • (stderr, result)

    Yields the stderr output and the result object

Yield Parameters:

Yield Returns:

  • (String)

    The processed stderr output

Returns:

  • (self)


147
148
149
150
151
152
153
# File 'lib/ruby_git/command_line/result.rb', line 147

def process_stderr(&block)
  return self if block.nil?

  @processed_stderr = block.call(stderr, self)

  self
end

#process_stdout {|stdout, result| ... } ⇒ self

Process the captured stdout output

Examples:

result = RubyGit::CommandLine::Result.new(
  ProcessExecuter.run_with_capture('echo hello')
)
result.stdout #=> "hello\n"
result.process_stdout { |stdout, _result| stdout.upcase }
result.stdout #=> "HELLO\n"
result.unprocessed_stdout #=> "hello\n"

Chain processing

result = RubyGit::CommandLine::Result.new(
  ProcessExecuter.run_with_capture('echo hello')
)
result.stdout #=> "hello\n"
# Here is the chain processing:
result.process_stdout { |s| s.upcase }.process_stdout { |s| s.reverse }
result.stdout #=> "OLLEH\n"
result.unprocessed_stdout #=> "hello\n"

Yields:

  • (stdout, result)

    Yields the stdout output and the result object

Yield Parameters:

Yield Returns:

  • (String)

    The processed stdout output

Returns:

  • (self)


72
73
74
75
76
77
78
# File 'lib/ruby_git/command_line/result.rb', line 72

def process_stdout(&block)
  return self if block.nil?

  @processed_stdout = block.call(stdout, self)

  self
end

#stderrString?

Return the processed stderr output (or original if it was not processed)

This output is only returned if a stderr redirection is a ProcessExecuter::MonitoredPipe.

Examples:

result = RubyGit::CommandLine::Result.new(
  ProcessExecuter.run_with_capture('echo hello >&2')
)
result.stderr #=> "hello\n"

Returns:

  • (String, nil)


113
114
115
# File 'lib/ruby_git/command_line/result.rb', line 113

def stderr
  defined?(@processed_stderr) ? @processed_stderr : unprocessed_stderr
end

#stdoutString?

Return the processed stdout output (or original if it was not processed)

Examples:

result = RubyGit::CommandLine::Result.new(
  ProcessExecuter.run_with_capture('echo hello')
)
result.stdout #=> "hello\n"

Returns:

  • (String, nil)


38
39
40
# File 'lib/ruby_git/command_line/result.rb', line 38

def stdout
  defined?(@processed_stdout) ? @processed_stdout : unprocessed_stdout
end

#unprocessed_stderrString?

Returns the original stderr output before it was processed

Examples:

result = RubyGit::CommandLine::Result.new(
  ProcessExecuter.run_with_capture('echo hello >&2')
)
result.stderr #=> "hello\n"
result.unprocessed_stderr #=> "hello\n"
result.process_stderr { |stderr| stderr.upcase }
result.stderr #=> "HELLO\n"
result.unprocessed_stderr #=> "hello\n"

Returns:

  • (String, nil)


171
172
173
# File 'lib/ruby_git/command_line/result.rb', line 171

def unprocessed_stderr
  __getobj__.stderr
end

#unprocessed_stdoutString?

Returns the original stdout output before it was processed

Examples:

result = RubyGit::CommandLine::Result.new(
  ProcessExecuter.run_with_capture('echo hello')
)
result.stdout #=> "hello\n"
result.unprocessed_stdout #=> "hello\n"
result.process_stdout { |s| s.upcase }
result.stdout #=> "HELLO\n"
result.unprocessed_stdout #=> "hello\n"

Returns:

  • (String, nil)


96
97
98
# File 'lib/ruby_git/command_line/result.rb', line 96

def unprocessed_stdout
  __getobj__.stdout
end