Class: Utils::XDG::XDGPathname
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.
Instance Method Summary collapse
-
#read(path, default: nil) {|file| ... } ⇒ String?
The read method reads file contents or yields to a block for processing.
-
#sub_dir_path(dirname) ⇒ Pathname
The sub_dir_path method creates a subdirectory path and ensures it exists.
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.
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.
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 |