Sha256: 2a9a4f8a8655a73e1f8687faf454c4952c539634656a04f9a4af6e9c272dcc66

Contents?: true

Size: 1.6 KB

Versions: 6

Compression:

Stored size: 1.6 KB

Contents

# encoding: utf-8

module TTY
  class Command
    # Encapsulates the information on the command executed
    #
    # @api public
    class Result
      include Enumerable

      # All data written out to process's stdout stream
      attr_reader :out
      alias stdout out

      # All data written out to process's stdin stream
      attr_reader :err
      alias stderr err

      def initialize(status, out, err)
        @status = status
        @out    = out
        @err    = err
      end

      # Enumerate over output lines
      #
      # @param [String] separator
      #
      # @api public
      def each(separator = nil)
        sep = separator || TTY::Command.record_separator
        return unless @out
        elements = @out.split(sep)
        if block_given?
          elements.each { |line| yield(line) }
        else
          elements.to_enum
        end
      end

      # Information on how the process exited
      #
      # @api public
      def exit_status
        @status
      end
      alias exitstatus exit_status
      alias status exit_status

      def to_i
        @status
      end

      def to_s
        @status.to_s
      end

      def to_ary
        [@out, @err]
      end

      def exited?
        @status != nil
      end
      alias complete? exited?

      def success?
        exited? ?  @status.zero? : false
      end

      def failure?
        !success?
      end
      alias failed? failure?

      def ==(other)
        return false unless other.is_a?(TTY::Command::Result)
        @status == other.to_i && to_ary == other.to_ary
      end
    end # Result
  end # Command
end # TTY

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
tty-command-0.5.0 lib/tty/command/result.rb
tty-command-0.4.0 lib/tty/command/result.rb
tty-command-0.3.3 lib/tty/command/result.rb
tty-command-0.3.2 lib/tty/command/result.rb
tty-command-0.3.1 lib/tty/command/result.rb
tty-command-0.3.0 lib/tty/command/result.rb