Class: Media::TempFile
- Inherits:
-
Object
- Object
- Media::TempFile
- Defined in:
- lib/media/temp_file.rb
Class Method Summary collapse
-
.open(data, content_type = nil) ⇒ Object
like initialize, but if given a block, then it yields the TempFile and also unlinks the file at the end of the block.
- .tempfile_path ⇒ Object
Instance Method Summary collapse
- #any? ⇒ Boolean
- #clear ⇒ Object
-
#initialize(data, content_type = nil) ⇒ TempFile
constructor
data may be one of:.
- #path ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(data, content_type = nil) ⇒ TempFile
data may be one of:
- FileUpload object: like the kind returned in multibyte encoded file upload forms.
- Pathname object: then load data from the file pointed to by the pathname.
- IO object: read the contents of the io object, copy to tmp file.
- otherwise, dump the contents of the data to the tmp file.
if data is empty, we generate an empty one.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/media/temp_file.rb', line 41 def initialize(data, content_type=nil) if data.nil? @tmpfile = TempFile.create_from_content_type(content_type) elsif data.respond_to?(:path) # we are dealing with an uploaded file object @tmpfile = TempFile.create_from_file(data.path, content_type, {mode: :move}) elsif data.is_a?(StringIO) data.rewind @tmpfile = TempFile.create_from_data(data.read, content_type) elsif data.instance_of?(Pathname) @tmpfile = TempFile.create_from_file(data.to_s, content_type) else @tmpfile = TempFile.create_from_data(data, content_type) end end |
Class Method Details
.open(data, content_type = nil) ⇒ Object
like initialize, but if given a block, then it yields the TempFile and also unlinks the file at the end of the block.
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/media/temp_file.rb', line 61 def self.open(data, content_type=nil) tmp = TempFile.new(data, content_type) if block_given? begin yield tmp ensure tmp.clear end nil else tmp end end |
.tempfile_path ⇒ Object
21 22 23 |
# File 'lib/media/temp_file.rb', line 21 def self.tempfile_path ::Media::TMP_PATH end |
Instance Method Details
#any? ⇒ Boolean
81 82 83 |
# File 'lib/media/temp_file.rb', line 81 def any? @tmpfile.any? end |
#clear ⇒ Object
75 76 77 78 79 |
# File 'lib/media/temp_file.rb', line 75 def clear # this is not really needed, because the tmp files are deleted as soon as # @tmpfile gets garbage collected. # @tmpfile.unlink end |
#path ⇒ Object
85 86 87 |
# File 'lib/media/temp_file.rb', line 85 def path @tmpfile.path end |
#to_s ⇒ Object
89 90 91 |
# File 'lib/media/temp_file.rb', line 89 def to_s @tmpfile.path end |