File: file_descriptor.rb

package info (click to toggle)
ruby-process-executer 4.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 408 kB
  • sloc: ruby: 873; makefile: 4
file content (40 lines) | stat: -rw-r--r-- 1,137 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

require_relative 'destination_base'

module ProcessExecuter
  module Destinations
    # Handles numeric file descriptors
    #
    # @api private
    class FileDescriptor < DestinationBase
      # Writes data to the file descriptor
      #
      # @param data [String] the data to write
      #
      # @return [Integer] the number of bytes written
      #
      # @raise [SystemCallError] if the file descriptor is invalid
      #
      # @example
      #   fd_handler = ProcessExecuter::Destinations::FileDescriptor.new(3)
      #   fd_handler.write("Hello world")
      #
      def write(data)
        super
        io = ::IO.open(destination, mode: 'a', autoclose: false)
        io.write(data).tap { io.close }
      end

      # Determines if this class can handle the given destination
      #
      # @param destination [Object] the destination to check
      #
      # @return [Boolean] true if destination is a file descriptor that's not stdout or stderr
      #
      def self.handles?(destination)
        destination.is_a?(Integer) && ![1, 2].include?(destination)
      end
    end
  end
end