Class: Staticky::Files::MemoryFileSystem
- Inherits:
-
Object
- Object
- Staticky::Files::MemoryFileSystem
- Defined in:
- lib/staticky/files/memory_file_system.rb,
lib/staticky/files/memory_file_system/node.rb
Overview
Memory File System abstraction to support ‘Staticky::Files`
Defined Under Namespace
Classes: Node
Constant Summary collapse
- EMPTY_CONTENT =
rubocop:disable Metrics/ClassLength
""
Instance Method Summary collapse
-
#chdir(path) ⇒ Object
Temporary changes the current working directory of the process to the given path and yield the given block.
-
#chmod(path, mode) ⇒ Object
Sets node UNIX mode.
-
#cp(source, destination) ⇒ Object
Copies file content from ‘source` to `destination` All the intermediate `destination` directories are created.
-
#directory?(path) ⇒ TrueClass, FalseClass
Check if the given path corresponds to a directory.
-
#entries(path) ⇒ Array<String>
Reads entries from a directory.
-
#executable?(path) ⇒ TrueClass, FalseClass
Check if the given path is an executable.
-
#exist?(path) ⇒ TrueClass, FalseClass
Check if the given path exist.
-
#expand_path(path, dir) ⇒ Object
Converts a path to an absolute path.
-
#initialize(root: Node.root) ⇒ Staticky::Files::MemoryFileSystem
constructor
Creates a new instance.
-
#join(*path) ⇒ String
Returns a new string formed by joining the strings using Operating System path separator.
-
#mkdir(path) ⇒ Object
Creates a directory and all its parent directories.
-
#mkdir_p(path) ⇒ Object
Creates a directory and all its parent directories.
-
#mode(path) ⇒ Integer
Gets node UNIX mode.
-
#open(path) {|| ... } ⇒ Staticky::Files::MemoryFileSystem::Node
Opens (or creates) a new file for read/write operations.
-
#pwd ⇒ String
Returns the name of the current working directory.
-
#read(path) ⇒ String
Read file contents.
-
#readlines(path) ⇒ Array<String>
Reads the entire file specified by path as individual lines, and returns those lines in an array.
-
#rm(path) ⇒ Object
Removes (deletes) a file.
-
#rm_rf(path) ⇒ Object
Removes (deletes) a directory.
-
#touch(path) ⇒ Object
Creates a file, if it doesn’t exist, and set empty content.
-
#write(path, *content) ⇒ Object
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) ⇒ Staticky::Files::MemoryFileSystem
Creates a new instance
the in-memory file system
19 20 21 |
# File 'lib/staticky/files/memory_file_system.rb', line 19 def initialize(root: Node.root) @root = root end |
Instance Method Details
#chdir(path) ⇒ Object
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.
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/staticky/files/memory_file_system.rb', line 143 def chdir(path) 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 yield ensure @root = current_root end |
#chmod(path, mode) ⇒ Object
Sets node UNIX mode
269 270 271 272 273 274 275 276 |
# File 'lib/staticky/files/memory_file_system.rb', line 269 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
Copies file content from ‘source` to `destination` All the intermediate `destination` directories are created.
204 205 206 207 |
# File 'lib/staticky/files/memory_file_system.rb', line 204 def cp(source, destination) content = read(source) write(destination, content) end |
#directory?(path) ⇒ TrueClass, FalseClass
Check if the given path corresponds to a directory.
307 308 309 310 |
# File 'lib/staticky/files/memory_file_system.rb', line 307 def directory?(path) path = Path[path] !find_directory(path).nil? end |
#entries(path) ⇒ Array<String>
Reads entries from a directory
331 332 333 334 335 336 337 338 |
# File 'lib/staticky/files/memory_file_system.rb', line 331 def entries(path) path = Path[path] node = find(path) raise IOError, Errno::ENOENT.new(path.to_s) if node.nil? raise IOError, Errno::ENOTDIR.new(path.to_s) unless node.directory? [".", ".."] + Array(node.children&.keys) end |
#executable?(path) ⇒ TrueClass, FalseClass
Check if the given path is an executable.
316 317 318 319 320 321 322 323 |
# File 'lib/staticky/files/memory_file_system.rb', line 316 def executable?(path) path = Path[path] node = find(path) return false if node.nil? node.executable? end |
#exist?(path) ⇒ TrueClass, FalseClass
Check if the given path exist.
297 298 299 300 301 |
# File 'lib/staticky/files/memory_file_system.rb', line 297 def exist?(path) path = Path[path] !find(path).nil? end |
#expand_path(path, dir) ⇒ Object
Converts a path to an absolute path.
120 121 122 123 124 |
# File 'lib/staticky/files/memory_file_system.rb', line 120 def (path, dir) return path if Path.absolute?(path) join(dir, path) end |
#join(*path) ⇒ String
Returns a new string formed by joining the strings using Operating System path separator
112 113 114 |
# File 'lib/staticky/files/memory_file_system.rb', line 112 def join(*path) Path[path] end |
#mkdir(path) ⇒ Object
Creates a directory and all its parent directories.
The argument ‘path` is intended to be a directory that you want to explicitly create.
file
168 169 170 171 172 173 174 175 176 |
# File 'lib/staticky/files/memory_file_system.rb', line 168 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
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.
189 190 191 192 193 194 195 |
# File 'lib/staticky/files/memory_file_system.rb', line 189 def mkdir_p(path) path = Path[path] mkdir( Path.dirname(path) ) end |
#mode(path) ⇒ Integer
Gets node UNIX mode
284 285 286 287 288 289 290 291 |
# File 'lib/staticky/files/memory_file_system.rb', line 284 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) {|| ... } ⇒ Staticky::Files::MemoryFileSystem::Node
Opens (or creates) a new file for read/write operations.
28 29 30 31 32 33 34 35 36 |
# File 'lib/staticky/files/memory_file_system.rb', line 28 def open(path, *) file = touch(path) if block_given? yield file else file end end |
#pwd ⇒ String
Returns the name of the current working directory.
129 130 131 |
# File 'lib/staticky/files/memory_file_system.rb', line 129 def pwd @root.segment end |
#read(path) ⇒ String
Read file contents
or if the file cannot be found
45 46 47 48 49 50 51 52 53 |
# File 'lib/staticky/files/memory_file_system.rb', line 45 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>
Reads the entire file specified by path as individual lines, and returns those lines in an array
or if the file cannot be found
63 64 65 66 67 68 69 70 71 |
# File 'lib/staticky/files/memory_file_system.rb', line 63 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
Removes (deletes) a file
a directory
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/staticky/files/memory_file_system.rb', line 217 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
Removes (deletes) a directory
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/staticky/files/memory_file_system.rb', line 244 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
Creates a file, if it doesn’t exist, and set empty content.
If the file was already existing, it’s a no-op.
80 81 82 83 84 85 86 |
# File 'lib/staticky/files/memory_file_system.rb', line 80 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
Creates a new file or rewrites the contents of an existing file for the given path and content All the intermediate directories are created.
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/staticky/files/memory_file_system.rb', line 94 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 |