Module: MemFs
- Defined in:
- lib/memfs.rb,
lib/memfs/io.rb,
lib/memfs/dir.rb,
lib/memfs/file.rb,
lib/memfs/version.rb,
lib/memfs/fake/file.rb,
lib/memfs/file/stat.rb,
lib/memfs/fake/entry.rb,
lib/memfs/file_system.rb,
lib/memfs/fake/symlink.rb,
lib/memfs/fake/directory.rb,
lib/memfs/fake/file/content.rb,
lib/memfs/filesystem_access.rb
Overview
Provides a clean way to interact with a fake file system.
Defined Under Namespace
Modules: Fake, FilesystemAccess Classes: Dir, File, FileSystem, IO
Constant Summary collapse
- OriginalDir =
Keeps track of the original Ruby Dir class.
::Dir
- OriginalFile =
Keeps track of the original Ruby File class.
::File
- OriginalIO =
Keeps track of the original Ruby IO class.
::IO
- VERSION =
'1.0.0'.freeze
Class Method Summary collapse
-
.activate { ... } ⇒ Object
Calls the given block with MemFs activated.
-
.activate!(clear: true) ⇒ Object
Activates the fake file system.
-
.deactivate! ⇒ Object
Deactivates the fake file system.
-
.halt ⇒ Object
Switches back to the original file system, calls the given block (if any), and switches back afterwards.
-
.touch(*paths) ⇒ Object
Creates a file and all its parent directories.
Class Method Details
.activate { ... } ⇒ Object
Calls the given block with MemFs activated.
The advantage of using #activate against #activate! is that, in case an exception occurs, MemFs is deactivated.
54 55 56 57 58 59 |
# File 'lib/memfs.rb', line 54 def activate activate! yield ensure deactivate! end |
.activate!(clear: true) ⇒ Object
Don’t forget to call #deactivate! to disable the fake file system, you may have some issues in your scripts or tests otherwise.
Activates the fake file system.
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/memfs.rb', line 76 def activate!(clear: true) Object.class_eval do remove_const :Dir remove_const :File remove_const :IO const_set :Dir, MemFs::Dir const_set :IO, MemFs::IO const_set :File, MemFs::File end MemFs::FileSystem.instance.clear! if clear end |
.deactivate! ⇒ Object
This method should always be called when using activate!
Deactivates the fake file system.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/memfs.rb', line 97 def deactivate! Object.class_eval do remove_const :Dir remove_const :File remove_const :IO const_set :Dir, MemFs::OriginalDir const_set :IO, MemFs::OriginalIO const_set :File, MemFs::OriginalFile end end |
.halt ⇒ Object
Switches back to the original file system, calls the given block (if any), and switches back afterwards.
If a block is given, all file & dir operations (like reading dir contents or requiring files) will operate on the original fs.
121 122 123 124 125 126 127 |
# File 'lib/memfs.rb', line 121 def halt deactivate! yield if block_given? ensure activate!(clear: false) end |
.touch(*paths) ⇒ Object
Creates a file and all its parent directories.
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/memfs.rb', line 135 def touch(*paths) if ::File != MemFs::File fail 'Always call MemFs.touch inside a MemFs active context.' end paths.each do |path| FileUtils.mkdir_p File.dirname(path) FileUtils.touch path end end |