Class: Cmds::Result
- Inherits:
-
Object
- Object
- Cmds::Result
- 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
-
#cmd ⇒ String
readonly
The command string that was executed.
-
#err ⇒ String
readonly
The command process' standard error.
-
#out ⇒ String
readonly
The command process' standard output.
-
#status ⇒ Fixnum
readonly
The command process' exit status code.
Instance Method Summary collapse
-
#assert ⇒ Result
raises an error if the command failed (exited with a #status other than
0). -
#error? ⇒ Boolean
True if #status is not
0. -
#initialize(cmd, status, out, err) ⇒ Result
constructor
A new instance of Result.
-
#ok? ⇒ Boolean
True if #status is
0.
Constructor Details
#initialize(cmd, status, out, err) ⇒ Result
Returns a new instance of Result.
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
#cmd ⇒ String (readonly)
Returns 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 |
#err ⇒ String (readonly)
Returns 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 |
#out ⇒ String (readonly)
Returns 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 |
#status ⇒ Fixnum (readonly)
Returns 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
#assert ⇒ Result
raises an error if the command failed (exited with a #status other
than 0).
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.
46 47 48 |
# File 'lib/cmds/result.rb', line 46 def error? ! ok? end |
#ok? ⇒ Boolean
Returns true if #status is 0.
41 42 43 |
# File 'lib/cmds/result.rb', line 41 def ok? @status == 0 end |