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 Cmds 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.



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

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

Instance Attribute Details

#cmdString (readonly)

The command string that was executed.

Returns:

  • (String)


17
18
19
# File 'lib/cmds/result.rb', line 17

def cmd
  @cmd
end

#errString (readonly)

The command process' standard error.

Returns:

  • (String)


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

def err
  @err
end

#outString (readonly)

The command process' standard output.

Returns:

  • (String)


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

def out
  @out
end

#statusFixnum (readonly)

The command process' exit status code.

Returns:

  • (Fixnum)


24
25
26
# File 'lib/cmds/result.rb', line 24

def status
  @status
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.



74
75
76
77
# File 'lib/cmds/result.rb', line 74

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.



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

def error?
  ! ok?
end

#ok?Boolean

Returns true if #status is 0.

Returns:

  • (Boolean)

    true if #status is 0.



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

def ok?
  @status == 0
end

#to_hHash<Symbol, V>

Get a Hash containing the instance variable values for easy logging, JSON dumping, etc.

Examples:

Cmds( "echo %s", "hey" ).to_h
# => {:cmd=>"echo hey", :status=>0, :out=>"hey\n", :err=>""}

Returns:

  • (Hash<Symbol, V>)


89
90
91
92
93
# File 'lib/cmds/result.rb', line 89

def to_h
  instance_variables.map { |name|
    [name.to_s.sub('@', '').to_sym, instance_variable_get( name )]
  }.to_h
end