Class: TMP::Instance
- Inherits:
-
Object
- Object
- TMP::Instance
- Defined in:
- lib/tmp/instance.rb
Instance Attribute Summary collapse
-
#tmpdir ⇒ Object
readonly
Returns the value of attribute tmpdir.
Instance Method Summary collapse
- #[](file_name) ⇒ Object
- #[]=(file_name, dump_object) ⇒ Object
-
#initialize(tmp_folder = nil) ⇒ Instance
constructor
A new instance of Instance.
- #open(file_name, *args) ⇒ Object
- #path_for(file_name) ⇒ Object
Constructor Details
#initialize(tmp_folder = nil) ⇒ Instance
Returns a new instance of Instance.
5 6 7 |
# File 'lib/tmp/instance.rb', line 5 def initialize(tmp_folder=nil) @tmpdir = tmp_folder || get_system_tmpdir end |
Instance Attribute Details
#tmpdir ⇒ Object (readonly)
Returns the value of attribute tmpdir.
3 4 5 |
# File 'lib/tmp/instance.rb', line 3 def tmpdir @tmpdir end |
Instance Method Details
#[](file_name) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# 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..to_s.include?('marshal') ? nil : raise(ex) end end end |
#[]=(file_name, dump_object) ⇒ Object
9 10 11 12 13 |
# 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) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# 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) ⇒ Object
43 44 45 |
# File 'lib/tmp/instance.rb', line 43 def path_for(file_name) File.join(tmpdir, file_name) end |