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.



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

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.



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
56
57
58
59
# File 'lib/cmds/result.rb', line 24

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
    Cmds.check_status @cmd, @status, @err
    self
  end # raise_error
end

#errString (readonly)

Returns the command process' standard error.

Returns:

  • (String)

    the command process' standard error.



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
56
57
58
59
# File 'lib/cmds/result.rb', line 24

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
    Cmds.check_status @cmd, @status, @err
    self
  end # raise_error
end

#outString (readonly)

Returns the command process' standard output.

Returns:

  • (String)

    the command process' standard output.



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
56
57
58
59
# File 'lib/cmds/result.rb', line 24

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
    Cmds.check_status @cmd, @status, @err
    self
  end # raise_error
end

#statusFixnum (readonly)

Returns the command process' exit status code.

Returns:

  • (Fixnum)

    the command process' exit status code.



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
56
57
58
59
# File 'lib/cmds/result.rb', line 24

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
    Cmds.check_status @cmd, @status, @err
    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.



55
56
57
58
# File 'lib/cmds/result.rb', line 55

def assert
  Cmds.check_status @cmd, @status, @err
  self
end

#error?Boolean

Returns true if #status is not 0.

Returns:

  • (Boolean)

    true if #status is not 0.



44
45
46
# File 'lib/cmds/result.rb', line 44

def error?
  ! ok?
end

#ok?Boolean

Returns true if #status is 0.

Returns:

  • (Boolean)

    true if #status is 0.



39
40
41
# File 'lib/cmds/result.rb', line 39

def ok?
  @status == 0
end