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.
-
#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.
188 189 190 191 192 |
# File 'lib/dry/files/file_system.rb', line 188 def chdir(path, &blk) with_error_handling do file_utils.chdir(path, &blk) 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.
262 263 264 265 266 267 268 |
# File 'lib/dry/files/file_system.rb', line 262 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.
328 329 330 |
# File 'lib/dry/files/file_system.rb', line 328 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.
342 343 344 |
# File 'lib/dry/files/file_system.rb', line 342 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.
314 315 316 |
# File 'lib/dry/files/file_system.rb', line 314 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.
158 159 160 |
# File 'lib/dry/files/file_system.rb', line 158 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
145 146 147 |
# File 'lib/dry/files/file_system.rb', line 145 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.
215 216 217 218 219 |
# File 'lib/dry/files/file_system.rb', line 215 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.
244 245 246 247 248 |
# File 'lib/dry/files/file_system.rb', line 244 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.
170 171 172 |
# File 'lib/dry/files/file_system.rb', line 170 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
282 283 284 285 286 |
# File 'lib/dry/files/file_system.rb', line 282 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
298 299 300 301 302 |
# File 'lib/dry/files/file_system.rb', line 298 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 |