Class: Cmds::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/cmds/result.rb

Overview

a simple data structure returned from calling #capture on a Cmd instance.

it contains the exit status code, standard output and standard error, as well as the actual string command issued (after all substitutions).

instances also have a few convenience methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd, status, out, err) ⇒ Result

Returns a new instance of Result.

Parameters:

  • cmd (String)

    #cmd attribute.

  • status (Fixnum)

    #status attribute.

  • out (String)

    #out attribute.

  • err (String)

    #err attribute.



33
34
35
36
37
38
# File 'lib/cmds/result.rb', line 33

def initialize cmd, status, out, err
  @cmd = cmd
  @status = status
  @out = out
  @err = err
end

Instance Attribute Details

#cmdString (readonly)

Returns the command string that was executed.

Returns:

  • (String)

    the command string that was executed.



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
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cmds/result.rb', line 26

class Result
  attr_reader :cmd, :status, :out, :err
  
  # @param cmd [String] {#cmd} attribute.
  # @param status [Fixnum] {#status} attribute.
  # @param out [String] {#out} attribute.
  # @param err [String] {#err} attribute.
  def initialize cmd, status, out, err
    @cmd = cmd
    @status = status
    @out = out
    @err = err
  end
  
  # @return [Boolean] true if {#status} is `0`.
  def ok?
    @status == 0
  end
  
  # @return [Boolean] true if {#status} is not `0`.
  def error?
    ! ok?
  end
  
  # raises an error if the command failed (exited with a {#status} other 
  # than `0`).
  #
  # @return [Result] it's self (so that it can be chained).
  #
  # @raise [SystemCallError] if the command failed.
  #
  def assert
    if error?
      msg = <<-BLOCK.squish
        command `#{ @cmd }` exited with status #{ @status }
        and stderr #{ err.inspect }
      BLOCK

      raise SystemCallError.new msg, @status
    end
    self
  end # raise_error
end

#errString (readonly)

Returns the command process' standard error.

Returns:

  • (String)

    the command process' standard error.



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
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cmds/result.rb', line 26

class Result
  attr_reader :cmd, :status, :out, :err
  
  # @param cmd [String] {#cmd} attribute.
  # @param status [Fixnum] {#status} attribute.
  # @param out [String] {#out} attribute.
  # @param err [String] {#err} attribute.
  def initialize cmd, status, out, err
    @cmd = cmd
    @status = status
    @out = out
    @err = err
  end
  
  # @return [Boolean] true if {#status} is `0`.
  def ok?
    @status == 0
  end
  
  # @return [Boolean] true if {#status} is not `0`.
  def error?
    ! ok?
  end
  
  # raises an error if the command failed (exited with a {#status} other 
  # than `0`).
  #
  # @return [Result] it's self (so that it can be chained).
  #
  # @raise [SystemCallError] if the command failed.
  #
  def assert
    if error?
      msg = <<-BLOCK.squish
        command `#{ @cmd }` exited with status #{ @status }
        and stderr #{ err.inspect }
      BLOCK

      raise SystemCallError.new msg, @status
    end
    self
  end # raise_error
end

#outString (readonly)

Returns the command process' standard output.

Returns:

  • (String)

    the command process' standard output.



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
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cmds/result.rb', line 26

class Result
  attr_reader :cmd, :status, :out, :err
  
  # @param cmd [String] {#cmd} attribute.
  # @param status [Fixnum] {#status} attribute.
  # @param out [String] {#out} attribute.
  # @param err [String] {#err} attribute.
  def initialize cmd, status, out, err
    @cmd = cmd
    @status = status
    @out = out
    @err = err
  end
  
  # @return [Boolean] true if {#status} is `0`.
  def ok?
    @status == 0
  end
  
  # @return [Boolean] true if {#status} is not `0`.
  def error?
    ! ok?
  end
  
  # raises an error if the command failed (exited with a {#status} other 
  # than `0`).
  #
  # @return [Result] it's self (so that it can be chained).
  #
  # @raise [SystemCallError] if the command failed.
  #
  def assert
    if error?
      msg = <<-BLOCK.squish
        command `#{ @cmd }` exited with status #{ @status }
        and stderr #{ err.inspect }
      BLOCK

      raise SystemCallError.new msg, @status
    end
    self
  end # raise_error
end

#statusFixnum (readonly)

Returns the command process' exit status code.

Returns:

  • (Fixnum)

    the command process' exit status code.



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
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cmds/result.rb', line 26

class Result
  attr_reader :cmd, :status, :out, :err
  
  # @param cmd [String] {#cmd} attribute.
  # @param status [Fixnum] {#status} attribute.
  # @param out [String] {#out} attribute.
  # @param err [String] {#err} attribute.
  def initialize cmd, status, out, err
    @cmd = cmd
    @status = status
    @out = out
    @err = err
  end
  
  # @return [Boolean] true if {#status} is `0`.
  def ok?
    @status == 0
  end
  
  # @return [Boolean] true if {#status} is not `0`.
  def error?
    ! ok?
  end
  
  # raises an error if the command failed (exited with a {#status} other 
  # than `0`).
  #
  # @return [Result] it's self (so that it can be chained).
  #
  # @raise [SystemCallError] if the command failed.
  #
  def assert
    if error?
      msg = <<-BLOCK.squish
        command `#{ @cmd }` exited with status #{ @status }
        and stderr #{ err.inspect }
      BLOCK

      raise SystemCallError.new msg, @status
    end
    self
  end # raise_error
end

Instance Method Details

#assertResult

raises an error if the command failed (exited with a #status other than 0).

Returns:

  • (Result)

    it's self (so that it can be chained).

Raises:

  • (SystemCallError)

    if the command failed.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cmds/result.rb', line 57

def assert
  if error?
    msg = <<-BLOCK.squish
      command `#{ @cmd }` exited with status #{ @status }
      and stderr #{ err.inspect }
    BLOCK

    raise SystemCallError.new msg, @status
  end
  self
end

#error?Boolean

Returns true if #status is not 0.

Returns:

  • (Boolean)

    true if #status is not 0.



46
47
48
# File 'lib/cmds/result.rb', line 46

def error?
  ! ok?
end

#ok?Boolean

Returns true if #status is 0.

Returns:

  • (Boolean)

    true if #status is 0.



41
42
43
# File 'lib/cmds/result.rb', line 41

def ok?
  @status == 0
end