Class: Dry::Files::MemoryFileSystem Private
- Inherits:
-
Object
- Object
- Dry::Files::MemoryFileSystem
- Defined in:
- lib/dry/files/memory_file_system.rb,
lib/dry/files/memory_file_system/node.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.
Memory File System abstraction to support ‘Dry::Files`
Defined Under Namespace
Classes: Node
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 node UNIX mode.
-
#cp(source, destination) ⇒ 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 corresponds to 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(root: Node.root) ⇒ Dry::Files::MemoryFileSystem
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) ⇒ Object
private
Creates a directory and all its parent directories.
-
#mkdir_p(path) ⇒ Object
private
Creates a directory and all its parent directories.
-
#mode(path) ⇒ Integer
private
Gets node UNIX mode.
-
#open(path) {|| ... } ⇒ Dry::Files::MemoryFileSystem::Node
private
Opens (or creates) a new file for read/write operations.
-
#pwd ⇒ String
private
Returns the name of the current working directory.
-
#read(path) ⇒ String
private
Read file contents.
-
#readlines(path) ⇒ Array<String>
private
Reads the entire file specified by path as individual lines, and returns those lines in an array.
-
#rm(path) ⇒ Object
private
Removes (deletes) a file.
-
#rm_rf(path) ⇒ Object
private
Removes (deletes) a directory.
-
#touch(path) ⇒ Object
private
Creates a file, if it doesn’t exist, and set empty content.
-
#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(root: Node.root) ⇒ Dry::Files::MemoryFileSystem
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 |
# File 'lib/dry/files/memory_file_system.rb', line 28 def initialize(root: Node.root) @root = root 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.
179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/dry/files/memory_file_system.rb', line 179 def chdir(path, &blk) path = Path[path] directory = find(path) raise IOError, Errno::ENOENT.new(path.to_s) if directory.nil? raise IOError, Errno::ENOTDIR.new(path.to_s) unless directory.directory? current_root = @root @root = directory blk.call ensure @root = current_root 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 node UNIX mode
321 322 323 324 325 326 327 328 |
# File 'lib/dry/files/memory_file_system.rb', line 321 def chmod(path, mode) path = Path[path] node = find(path) raise IOError, Errno::ENOENT.new(path.to_s) if node.nil? node.chmod = mode end |
#cp(source, destination) ⇒ 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.
248 249 250 251 |
# File 'lib/dry/files/memory_file_system.rb', line 248 def cp(source, destination) content = read(source) write(destination, content) 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 corresponds to a directory.
368 369 370 371 |
# File 'lib/dry/files/memory_file_system.rb', line 368 def directory?(path) path = Path[path] !find_directory(path).nil? 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.
380 381 382 383 384 385 386 387 |
# File 'lib/dry/files/memory_file_system.rb', line 380 def executable?(path) path = Path[path] node = find(path) return false if node.nil? node.executable? 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.
355 356 357 358 359 |
# File 'lib/dry/files/memory_file_system.rb', line 355 def exist?(path) path = Path[path] !find(path).nil? 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.
150 151 152 153 154 |
# File 'lib/dry/files/memory_file_system.rb', line 150 def (path, dir) return path if Path.absolute?(path) join(dir, path) 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
139 140 141 |
# File 'lib/dry/files/memory_file_system.rb', line 139 def join(*path) Path[path] end |
#mkdir(path) ⇒ 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.
206 207 208 209 210 211 212 213 214 |
# File 'lib/dry/files/memory_file_system.rb', line 206 def mkdir(path) path = Path[path] node = @root for_each_segment(path) do |segment| node = node.set(segment) raise IOError, Errno::EEXIST.new(path.to_s) if node.file? end end |
#mkdir_p(path) ⇒ 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.
230 231 232 233 234 235 236 |
# File 'lib/dry/files/memory_file_system.rb', line 230 def mkdir_p(path) path = Path[path] mkdir( Path.dirname(path) ) end |
#mode(path) ⇒ Integer
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.
Gets node UNIX mode
339 340 341 342 343 344 345 346 |
# File 'lib/dry/files/memory_file_system.rb', line 339 def mode(path) path = Path[path] node = find(path) raise IOError, Errno::ENOENT.new(path.to_s) if node.nil? node.mode end |
#open(path) {|| ... } ⇒ Dry::Files::MemoryFileSystem::Node
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 read/write operations.
40 41 42 43 44 45 46 47 48 |
# File 'lib/dry/files/memory_file_system.rb', line 40 def open(path, *) file = touch(path) if block_given? yield file else file 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.
162 163 164 |
# File 'lib/dry/files/memory_file_system.rb', line 162 def pwd @root.segment end |
#read(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.
Read file contents
60 61 62 63 64 65 66 67 68 |
# File 'lib/dry/files/memory_file_system.rb', line 60 def read(path) path = Path[path] raise IOError, Errno::EISDIR.new(path.to_s) if directory?(path) file = find_file(path) raise IOError, Errno::ENOENT.new(path.to_s) if file.nil? file.read end |
#readlines(path) ⇒ Array<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.
Reads the entire file specified by path as individual lines, and returns those lines in an array
81 82 83 84 85 86 87 88 89 |
# File 'lib/dry/files/memory_file_system.rb', line 81 def readlines(path) path = Path[path] node = find(path) raise IOError, Errno::ENOENT.new(path.to_s) if node.nil? raise IOError, Errno::EISDIR.new(path.to_s) if node.directory? node.readlines end |
#rm(path) ⇒ 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
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/dry/files/memory_file_system.rb', line 263 def rm(path) path = Path[path] file = nil parent = @root node = @root for_each_segment(path) do |segment| break unless node file = segment parent = node node = node.get(segment) end raise IOError, Errno::ENOENT.new(path.to_s) if node.nil? raise IOError, Errno::EPERM.new(path.to_s) if node.directory? parent.unset(file) end |
#rm_rf(path) ⇒ 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
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/dry/files/memory_file_system.rb', line 293 def rm_rf(path) path = Path[path] file = nil parent = @root node = @root for_each_segment(path) do |segment| break unless node file = segment parent = node node = node.get(segment) end raise IOError, Errno::ENOENT.new(path.to_s) if node.nil? parent.unset(file) end |
#touch(path) ⇒ 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 file, if it doesn’t exist, and set empty content.
If the file was already existing, it’s a no-op.
101 102 103 104 105 106 107 |
# File 'lib/dry/files/memory_file_system.rb', line 101 def touch(path) path = Path[path] raise IOError, Errno::EISDIR.new(path.to_s) if directory?(path) content = read(path) if exist?(path) write(path, content || EMPTY_CONTENT) 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.
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/dry/files/memory_file_system.rb', line 118 def write(path, *content) path = Path[path] node = @root for_each_segment(path) do |segment| node = node.set(segment) end node.write(*content) node end |