Module: TaliaCore::DataTypes::TempFileHandling
- Included in:
- FileRecord
- Defined in:
- lib/talia_core/data_types/temp_file_handling.rb
Overview
Module for the handling of temporary files in data storage objects
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#copy_to_temp_file(file) ⇒ Object
Copies the given file to a randomly named Tempfile.
-
#random_tempfile_filename ⇒ Object
Generates a unique filename for a Tempfile.
-
#temp_data ⇒ Object
Gets the data from the latest temp file.
-
#temp_data=(data) ⇒ Object
Writes the given data to a Tempfile and adds it to the collection of temp files.
-
#temp_path ⇒ Object
Gets the latest temp path from the collection of temp paths.
-
#temp_path=(value) ⇒ Object
Adds a new temp_path to the array.
-
#temp_paths ⇒ Object
Gets an array of the currently used temp paths.
-
#write_to_temp_file(data) ⇒ Object
Writes the given file to a randomly named Tempfile.
Instance Method Details
#copy_to_temp_file(file) ⇒ Object
Copies the given file to a randomly named Tempfile.
69 70 71 |
# File 'lib/talia_core/data_types/temp_file_handling.rb', line 69 def copy_to_temp_file(file) self.class.copy_to_temp_file file, random_tempfile_filename end |
#random_tempfile_filename ⇒ Object
Generates a unique filename for a Tempfile.
79 80 81 |
# File 'lib/talia_core/data_types/temp_file_handling.rb', line 79 def random_tempfile_filename "#{rand Time.now.to_i}#{location || 'attachment'}" end |
#temp_data ⇒ Object
Gets the data from the latest temp file. This will read the file into memory.
59 60 61 |
# File 'lib/talia_core/data_types/temp_file_handling.rb', line 59 def temp_data ? File.read(temp_path) : nil end |
#temp_data=(data) ⇒ Object
Writes the given data to a Tempfile and adds it to the collection of temp files.
64 65 66 |
# File 'lib/talia_core/data_types/temp_file_handling.rb', line 64 def temp_data=(data) self.temp_path = write_to_temp_file data unless data.nil? end |
#temp_path ⇒ Object
Gets the latest temp path from the collection of temp paths. While working with an attachment, multiple Tempfile objects may be created for various processing purposes (resizing, for example). An array of all the tempfile objects is stored so that the Tempfile instance is held on to until it’s not needed anymore. The collection is cleared after saving the attachment.
40 41 42 43 |
# File 'lib/talia_core/data_types/temp_file_handling.rb', line 40 def temp_path p = temp_paths.first p.respond_to?(:path) ? p.path : p.to_s end |
#temp_path=(value) ⇒ Object
Adds a new temp_path to the array. This should take a string or a Tempfile. This class makes no attempt to remove the files, so Tempfiles should be used. Tempfiles remove themselves when they go out of scope. You can also use string paths for temporary files, such as those used for uploaded files in a web server.
53 54 55 56 |
# File 'lib/talia_core/data_types/temp_file_handling.rb', line 53 def temp_path=(value) temp_paths.unshift value temp_path end |
#temp_paths ⇒ Object
Gets an array of the currently used temp paths. Defaults to a copy of #full_filename.
46 47 48 |
# File 'lib/talia_core/data_types/temp_file_handling.rb', line 46 def temp_paths @temp_paths ||= (new_record? || !File.exist?(full_filename)) ? [] : [copy_to_temp_file(full_filename)] end |
#write_to_temp_file(data) ⇒ Object
Writes the given file to a randomly named Tempfile.
74 75 76 |
# File 'lib/talia_core/data_types/temp_file_handling.rb', line 74 def write_to_temp_file(data) self.class.write_to_temp_file data, self.location end |