Created
July 20, 2020 12:09
-
-
Save isidzukuri/6aa917662183967dccf427d2524de8bb to your computer and use it in GitHub Desktop.
creates temporary file with name without randomizing
This file contains hidden or 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 WriteTemporaryFile | |
| class Remover | |
| def initialize(tmpfile) | |
| @pid = Process.pid | |
| @tmpfile = tmpfile | |
| end | |
| def call(*args) | |
| return if @pid != Process.pid | |
| @tmpfile.close | |
| begin | |
| File.unlink(@tmpfile.path) | |
| rescue Errno::ENOENT | |
| end | |
| end | |
| end | |
| def self.call(filename, content) | |
| file = File.new("tmp/#{filename}", 0|File::RDWR|File::CREAT|File::EXCL, {perm: 0600}) | |
| file.binmode | |
| file.write(content) | |
| ObjectSpace.define_finalizer(self, Remover.new(file)) | |
| file.read | |
| file | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment