1# frozen_string_literal: true
2
3require_relative 'destination_base'
4
5module ProcessExecuter
6 module Destinations
7 # Handles numeric file descriptors
8 #
9 # @api private
10 class FileDescriptor < DestinationBase
11 # Writes data to the file descriptor
12 #
13 # @param data [String] the data to write
14 #
15 # @return [Integer] the number of bytes written
16 #
17 # @raise [SystemCallError] if the file descriptor is invalid
18 #
19 # @example
20 # fd_handler = ProcessExecuter::Destinations::FileDescriptor.new(3)
21 # fd_handler.write("Hello world")
22 #
23 def write(data)
24 super
25 io = ::IO.open(destination, mode: 'a', autoclose: false)
26 io.write(data).tap { io.close }
27 end
28
29 # Determines if this class can handle the given destination
30 #
31 # @param destination [Object] the destination to check
32 #
33 # @return [Boolean] true if destination is a file descriptor that's not stdout or stderr
34 #
35 def self.handles?(destination)
36 destination.is_a?(Integer) && ![1, 2].include?(destination)
37 end
38 end
39 end
40end
41