Created
February 7, 2016 15:21
-
-
Save amiryal/fb1b2998354797ddead9 to your computer and use it in GitHub Desktop.
Ruby method to keep track of files written to while executing given block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WrittenFilesTracking | |
def self.written_files | |
@@written_files | |
end | |
def self.start_with | |
@@start_with | |
end | |
def self.with(start_with:'./') | |
@@start_with = start_with | |
@@written_files = [] | |
begin | |
class << File | |
alias_method :__written_files_open, :open | |
def __written_files_tracing_open(*args, &block) | |
if args[0].start_with?(WrittenFilesTracking.start_with) && args[1] =~ /w/ | |
WrittenFilesTracking.written_files.push(args[0]) | |
end | |
__written_files_open(*args, &block) | |
end | |
alias_method :open, :__written_files_tracing_open | |
end | |
yield | |
ensure | |
class << File | |
alias_method :open, :__written_files_open | |
remove_method :__written_files_open | |
remove_method :__written_files_tracing_open | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment