File: file_path_mode_perms.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 (68 lines) | stat: -rw-r--r-- 2,010 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
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
# frozen_string_literal: true

require_relative 'destination_base'

module ProcessExecuter
  module Destinations
    # Handles file paths with specific open modes and permissions
    #
    # @api private
    class FilePathModePerms < DestinationBase
      # Initializes a new file path with mode and permissions destination handler
      #
      # Opens the file at the given path with the specified mode and permissions.
      #
      # @param destination [Array<String, String, Integer>] array with file path, mode, and permissions
      #
      # @raise [Errno::ENOENT] if the file path is invalid
      #
      # @raise [ArgumentError] if the mode is invalid
      #
      def initialize(destination)
        super
        @file = File.open(destination[0], destination[1], destination[2])
      end

      # The opened file object
      #
      # @return [File] the opened file
      attr_reader :file

      # Writes data to the file
      #
      # @example
      #   perms_handler = ProcessExecuter::Destinations::FilePathModePerms.new(["output.log", "w", 0644])
      #   perms_handler.write("Log entry with specific permissions")
      #
      # @param data [String] the data to write
      #
      # @return [Integer] the number of bytes written
      #
      # @raise [IOError] if the file is closed
      #
      def write(data)
        super
        file.write data
      end

      # Closes the file if it's open
      #
      # @return [void]
      def close
        file.close unless file.closed?
      end

      # Determines if this class can handle the given destination
      #
      # @param destination [Object] the destination to check
      # @return [Boolean] true if destination is an Array with path, mode, and permissions
      def self.handles?(destination)
        destination.is_a?(Array) &&
          destination.size == 3 &&
          destination[0].is_a?(String) &&
          destination[1].is_a?(String) &&
          destination[2].is_a?(Integer)
      end
    end
  end
end