Class: Utils::XDG::XDGPathname

Inherits:
Pathname
  • Object
show all
Defined in:
lib/utils/xdg.rb

Overview

A Pathname subclass that provides additional XDG directory functionality

This class extends the standard Pathname class to include methods for working with XDG (Cross-Desktop Group) base directory specifications. It adds capabilities for creating subdirectories, reading files, and handling path operations within the context of XDG-compliant directory structures.

Examples:

pathname = XDGPathname.new('/home/user')
sub_dir = pathname.sub_dir_path('documents')
content = pathname.read('config.txt')

Instance Method Summary collapse

Instance Method Details

#read(path, default: nil) {|file| ... } ⇒ String?

The read method reads file contents or yields to a block for processing.

This method attempts to read a file at the specified path, returning the file’s contents as a string. If a block is provided, it opens the file and yields to the block with a File object. If the file does not exist and a default value is provided, it returns the default value. When a default value is provided along with a block, the block is invoked with a StringIO object containing the default value.

Parameters:

  • path (String)

    the path to the file to read

  • default (String, nil) (defaults to: nil)

    the default value to return if the file does not exist

Yields:

  • (file)

    optional block to process the file

Yield Parameters:

  • file (File)

    the file object for processing

Returns:

  • (String, nil)

    the file contents if no block is given, the result of the block if given, or the default value if the file does not exist



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/utils/xdg.rb', line 78

def read(path, default: nil, &block)
  full_path = join(path)
  if File.exist?(full_path)
    if block
      File.new(full_path, &block)
    else
      File.read(full_path, encoding: 'UTF-8')
    end
  else
    if default && block
      block.(StringIO.new(default))
    else
      default
    end
  end
end

#sub_dir_path(dirname) ⇒ Pathname

The sub_dir_path method creates a subdirectory path and ensures it exists

This method takes a directory name, combines it with the current path to form a subdirectory path, and then checks if the path already exists. If the path exists but is not a directory, it raises an ArgumentError. If the path does not exist, it creates the directory structure using FileUtils.

Parameters:

  • dirname (String)

    the name of the subdirectory to create or access

Returns:

  • (Pathname)

    the Pathname object representing the subdirectory path

Raises:

  • (ArgumentError)

    if the path exists but is not a directory



46
47
48
49
50
51
52
53
54
55
# File 'lib/utils/xdg.rb', line 46

def sub_dir_path(dirname)
  path = self + dirname
  if path.exist?
    path.directory? or raise ArgumentError,
      "path #{path.to_s.inspect} exists and is not a directory"
  else
    FileUtils.mkdir_p path
  end
  path
end