Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch between fake & real fs. #24

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/memfs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,37 @@ def deactivate!
end
module_function :deactivate!

# Switches back to the original file system, calls the given block (if any),
# and switches back afterwards.
#
# Allows for file & dir operations that need to take place on the real fs.
#
# @example
# MemFs.halt do
# puts Dir.getwd
# end
#
# @return nothing
def halt

switch_to = -> dir_class, file_class {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer do...end for multi-line blocks without chaining.
Use the lambda method for multi-line lambdas.
Wrap stabby lambda arguments with parentheses.
Trailing whitespace detected.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ups, sorry for the many violations - feel free to rewrite the code if you like the feature. I'm still a sort of apprentice when it comes to Ruby...


Object.class_eval do
remove_const :Dir
remove_const :File

const_set :Dir, dir_class
const_set :File, file_class
end
}

switch_to.call MemFs::OriginalDir, MemFs::OriginalFile
yield if block_given?
ensure
switch_to.call MemFs::Dir, MemFs::File
end
module_function :halt

# Creates a file and all its parent directories.
#
# @param path: The path of the file to create.
Expand Down