Guest User

How Crème Fraiche creates temporary directories

a guest
Aug 22nd, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.91 KB | None | 0 0
  1.     # Creates a temporary directory upon the first call but always returns its path
  2.     # Stolen from Alex Chaffee's files-gem and
  3.     # http://stackoverflow.com/questions/1139265/what-is-the-best-way-to-get-a-temporary-directory-in-ruby-on-rails
  4.     def temp_dir options = {:remove => true}
  5.         @temp_dir ||= begin
  6.                       @log.debug('creating temp_dir')
  7.                       require 'tmpdir'
  8.                       require 'fileutils'
  9.                       #
  10.                       # called_from = File.basename caller.first.split(':').first, ".rb"
  11.                       called_from = CremeFraiche::prog_info[:app_name].gsub(' ', '_')
  12.                       @log.debug('called_from is ' << called_from)
  13.                       path = File.join(Dir::tmpdir, "#{called_from}_#{Time.now.to_i}_#{rand(1000)}")
  14.                       @log.debug('temp-dir path is ' << path)
  15.                       Dir.mkdir(path)
  16.                       at_exit {FileUtils.rm_rf(path) if File.exists?(path)} if options[:remove]
  17.                       File.new path
  18.                   end
  19.     end
Advertisement
Add Comment
Please, Sign In to add comment