class TMP::Instance
Attributes
tmpdir[R]
Public Class Methods
new(tmp_folder=nil)
click to toggle source
# File lib/tmp/instance.rb, line 5 def initialize(tmp_folder=nil) @tmpdir = tmp_folder || get_system_tmpdir end
Public Instance Methods
[](file_name)
click to toggle source
# File lib/tmp/instance.rb, line 15 def [](file_name) open(file_name, 'r') do |f| begin Marshal.load(f.read) rescue ArgumentError => ex ex.message.to_s.include?('marshal') ? nil : raise(ex) end end end
[]=(file_name, dump_object)
click to toggle source
# File lib/tmp/instance.rb, line 9 def []=(file_name, dump_object) open(file_name, 'w+') do |file| file.write(Marshal.dump(dump_object)) end end
open(file_name, *args) { |f| ... }
click to toggle source
# File lib/tmp/instance.rb, line 25 def open(file_name, *args) file_path = path_for(file_name) File.open(file_path, *args) do |f| begin f.flock(File::LOCK_EX) yield(f) ensure f.flock(File::LOCK_UN) end end rescue Errno::ENOENT File.open(file_path, 'a') {} retry end
path_for(file_name)
click to toggle source
# File lib/tmp/instance.rb, line 43 def path_for(file_name) File.join(tmpdir, file_name) end
Protected Instance Methods
get_system_tmpdir()
click to toggle source
# File lib/tmp/instance.rb, line 49 def get_system_tmpdir require 'tmpdir' require 'securerandom' tmp_dir_to_use = File.join(Dir.tmpdir,SecureRandom.uuid.to_s) Dir.mkdir(tmp_dir_to_use) unless File.exist?(tmp_dir_to_use) return tmp_dir_to_use end