Class: Dry::Files::FileSystem Private
- Inherits:
-
Object
- Object
- Dry::Files::FileSystem
- Defined in:
- lib/dry/files/file_system.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
File System abstraction to support ‘Dry::Files`
Instance Attribute Summary collapse
- #file ⇒ Object readonly private
- #file_utils ⇒ Object readonly private
Instance Method Summary collapse
-
#chdir(path, &blk) ⇒ Object
private
Temporary changes the current working directory of the process to the given path and yield the given block.
-
#chmod(path, mode) ⇒ Object
private
Sets UNIX permissions of the file at the given path.
-
#cp(source, destination, **kwargs) ⇒ Object
private
Copies file content from ‘source` to `destination` All the intermediate `destination` directories are created.
-
#directory?(path) ⇒ TrueClass, FalseClass
private
Check if the given path is a directory.
-
#executable?(path) ⇒ TrueClass, FalseClass
private
Check if the given path is an executable.
-
#exist?(path) ⇒ TrueClass, FalseClass
private
Check if the given path exist.
-
#expand_path(path, dir) ⇒ Object
private
Converts a path to an absolute path.
-
#initialize(file: File, file_utils: FileUtils) ⇒ Dry::Files::FileSystem
constructor
private
Creates a new instance.
-
#join(*path) ⇒ String
private
Returns a new string formed by joining the strings using Operating System path separator.
-
#mkdir(path, **kwargs) ⇒ Object
private
Creates a directory and all its parent directories.
-
#mkdir_p(path, **kwargs) ⇒ Object
private
Creates a directory and all its parent directories.
-
#open(path, mode, *args, &blk) {|the| ... } ⇒ Object
private
Opens (or creates) a new file for both read/write operations.
-
#pwd ⇒ String
private
Returns the name of the current working directory.
-
#read(path, *args, **kwargs) ⇒ Object
private
Opens the file, optionally seeks to the given offset, then returns length bytes (defaulting to the rest of the file).
-
#readlines(path, *args) ⇒ Object
private
Reads the entire file specified by name as individual lines, and returns those lines in an array.
-
#rm(path, **kwargs) ⇒ Object
private
Removes (deletes) a file.
-
#rm_rf(path, *args) ⇒ Object
private
Removes (deletes) a directory.
-
#touch(path, **kwargs) ⇒ Object
private
Updates modification time (mtime) and access time (atime) of file(s) in list.
-
#write(path, *content) ⇒ Object
private
Creates a new file or rewrites the contents of an existing file for the given path and content All the intermediate directories are created.
Constructor Details
#initialize(file: File, file_utils: FileUtils) ⇒ Dry::Files::FileSystem
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a new instance
28 29 30 31 |
# File 'lib/dry/files/file_system.rb', line 28 def initialize(file: File, file_utils: FileUtils) @file = file @file_utils = file_utils end |
Instance Attribute Details
#file ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
14 15 16 |
# File 'lib/dry/files/file_system.rb', line 14 def file @file end |
#file_utils ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
18 19 20 |
# File 'lib/dry/files/file_system.rb', line 18 def file_utils @file_utils end |
Instance Method Details
#chdir(path, &blk) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Temporary changes the current working directory of the process to the given path and yield the given block.
The argument ‘path` is intended to be a directory.
207 208 209 210 211 |
# File 'lib/dry/files/file_system.rb', line 207 def chdir(path, &blk) with_error_handling do file_utils.chdir(path, &blk) end end |
#chmod(path, mode) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets UNIX permissions of the file at the given path.
Accepts permissions in numeric mode only, best provided as octal numbers matching the standard UNIX octal permission modes, such as ‘0o544` for a file writeable by its owner and readable by others, or `0o755` for a file writeable by its owner and executable by everyone.
147 148 149 150 151 |
# File 'lib/dry/files/file_system.rb', line 147 def chmod(path, mode) with_error_handling do file_utils.chmod(mode, path) end end |
#cp(source, destination, **kwargs) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Copies file content from ‘source` to `destination` All the intermediate `destination` directories are created.
281 282 283 284 285 286 287 |
# File 'lib/dry/files/file_system.rb', line 281 def cp(source, destination, **kwargs) mkdir_p(destination) with_error_handling do file_utils.cp(source, destination, **kwargs) end end |
#directory?(path) ⇒ TrueClass, FalseClass
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if the given path is a directory.
347 348 349 |
# File 'lib/dry/files/file_system.rb', line 347 def directory?(path) file.directory?(path) end |
#executable?(path) ⇒ TrueClass, FalseClass
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if the given path is an executable.
361 362 363 |
# File 'lib/dry/files/file_system.rb', line 361 def executable?(path) file.executable?(path) end |
#exist?(path) ⇒ TrueClass, FalseClass
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if the given path exist.
333 334 335 |
# File 'lib/dry/files/file_system.rb', line 333 def exist?(path) file.exist?(path) end |
#expand_path(path, dir) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Converts a path to an absolute path.
177 178 179 |
# File 'lib/dry/files/file_system.rb', line 177 def (path, dir) file.(path, dir) end |
#join(*path) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new string formed by joining the strings using Operating System path separator
164 165 166 |
# File 'lib/dry/files/file_system.rb', line 164 def join(*path) file.join(*path) end |
#mkdir(path, **kwargs) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a directory and all its parent directories.
The argument ‘path` is intended to be a directory that you want to explicitly create.
234 235 236 237 238 |
# File 'lib/dry/files/file_system.rb', line 234 def mkdir(path, **kwargs) with_error_handling do file_utils.mkdir_p(path, **kwargs) end end |
#mkdir_p(path, **kwargs) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a directory and all its parent directories.
The argument ‘path` is intended to be a file, where its directory ancestors will be implicitly created.
263 264 265 266 267 |
# File 'lib/dry/files/file_system.rb', line 263 def mkdir_p(path, **kwargs) mkdir( file.dirname(path), **kwargs ) end |
#open(path, mode, *args, &blk) {|the| ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Opens (or creates) a new file for both read/write operations.
If the file doesn’t exist, it creates a new one.
49 50 51 52 53 54 55 |
# File 'lib/dry/files/file_system.rb', line 49 def open(path, mode, *args, &blk) touch(path) with_error_handling do file.open(path, mode, *args, &blk) end end |
#pwd ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the name of the current working directory.
189 190 191 |
# File 'lib/dry/files/file_system.rb', line 189 def pwd file_utils.pwd end |
#read(path, *args, **kwargs) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Opens the file, optionally seeks to the given offset, then returns length bytes (defaulting to the rest of the file).
Read ensures the file is closed before returning.
70 71 72 73 74 |
# File 'lib/dry/files/file_system.rb', line 70 def read(path, *args, **kwargs) with_error_handling do file.read(path, *args, **kwargs) end end |
#readlines(path, *args) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Reads the entire file specified by name as individual lines, and returns those lines in an array
87 88 89 90 91 |
# File 'lib/dry/files/file_system.rb', line 87 def readlines(path, *args) with_error_handling do file.readlines(path, *args) end end |
#rm(path, **kwargs) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Removes (deletes) a file
301 302 303 304 305 |
# File 'lib/dry/files/file_system.rb', line 301 def rm(path, **kwargs) with_error_handling do file_utils.rm(path, **kwargs) end end |
#rm_rf(path, *args) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Removes (deletes) a directory
317 318 319 320 321 |
# File 'lib/dry/files/file_system.rb', line 317 def rm_rf(path, *args) with_error_handling do file_utils.remove_entry_secure(path, *args) end end |
#touch(path, **kwargs) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Updates modification time (mtime) and access time (atime) of file(s) in list.
Files are created if they don’t exist.
106 107 108 109 110 111 112 113 |
# File 'lib/dry/files/file_system.rb', line 106 def touch(path, **kwargs) raise IOError, Errno::EISDIR.new(path.to_s) if directory?(path) with_error_handling do mkdir_p(path) file_utils.touch(path, **kwargs) end end |
#write(path, *content) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a new file or rewrites the contents of an existing file for the given path and content All the intermediate directories are created.
126 127 128 129 130 131 132 |
# File 'lib/dry/files/file_system.rb', line 126 def write(path, *content) mkdir_p(path) self.open(path, WRITE_MODE) do |f| f.write(Array(content).flatten.join) end end |