Class: Omnizip::Temp::TempFile
- Inherits:
-
Object
- Object
- Omnizip::Temp::TempFile
- Defined in:
- lib/omnizip/temp/temp_file.rb
Overview
RAII-pattern temporary file with automatic cleanup
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
-
.finalizer(tempfile) ⇒ Object
rubocop:disable Lint/IneffectiveAccessModifier.
Instance Method Summary collapse
-
#close ⇒ Object
Close the temp file.
-
#file ⇒ Tempfile
Get the underlying Tempfile object.
-
#finalized? ⇒ Boolean
Check if file has been finalized.
-
#initialize(prefix: "omniz_", suffix: "", directory: nil) ⇒ TempFile
constructor
Create new temporary file.
-
#keep! ⇒ Object
Prevent automatic deletion.
-
#kept? ⇒ Boolean
Check if file will be kept.
-
#read(length = nil) ⇒ String?
Read data from temp file.
-
#rewind ⇒ Object
Rewind to beginning of file.
-
#unlink ⇒ Object
Delete the temp file.
-
#write(data) ⇒ Integer
Write data to temp file.
Constructor Details
#initialize(prefix: "omniz_", suffix: "", directory: nil) ⇒ TempFile
Create new temporary file
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/omnizip/temp/temp_file.rb', line 15 def initialize(prefix: "omniz_", suffix: "", directory: nil) @prefix = prefix @suffix = suffix @directory = directory @tempfile = nil @path = nil @kept = false @finalized = false create setup_finalizer end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/omnizip/temp/temp_file.rb', line 9 def path @path end |
Class Method Details
.finalizer(tempfile) ⇒ Object
rubocop:disable Lint/IneffectiveAccessModifier
112 113 114 115 116 117 118 119 120 |
# File 'lib/omnizip/temp/temp_file.rb', line 112 def self.finalizer(tempfile) proc do tempfile.close unless tempfile.closed? tempfile.unlink rescue StandardError # Ignore errors in finalizer nil end end |
Instance Method Details
#close ⇒ Object
Close the temp file
54 55 56 |
# File 'lib/omnizip/temp/temp_file.rb', line 54 def close @tempfile.close if @tempfile && !@tempfile.closed? end |
#file ⇒ Tempfile
Get the underlying Tempfile object
30 31 32 |
# File 'lib/omnizip/temp/temp_file.rb', line 30 def file @tempfile end |
#finalized? ⇒ Boolean
Check if file has been finalized
85 86 87 |
# File 'lib/omnizip/temp/temp_file.rb', line 85 def finalized? @finalized end |
#keep! ⇒ Object
Prevent automatic deletion
73 74 75 |
# File 'lib/omnizip/temp/temp_file.rb', line 73 def keep! @kept = true end |
#kept? ⇒ Boolean
Check if file will be kept
79 80 81 |
# File 'lib/omnizip/temp/temp_file.rb', line 79 def kept? @kept end |
#read(length = nil) ⇒ String?
Read data from temp file
44 45 46 |
# File 'lib/omnizip/temp/temp_file.rb', line 44 def read(length = nil) @tempfile.read(length) end |
#rewind ⇒ Object
Rewind to beginning of file
49 50 51 |
# File 'lib/omnizip/temp/temp_file.rb', line 49 def rewind @tempfile.rewind end |
#unlink ⇒ Object
Delete the temp file
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/omnizip/temp/temp_file.rb', line 59 def unlink return if @finalized || @kept if @tempfile @tempfile.close unless @tempfile.closed? @tempfile.unlink @finalized = true end rescue StandardError # Ignore errors during cleanup nil end |
#write(data) ⇒ Integer
Write data to temp file
37 38 39 |
# File 'lib/omnizip/temp/temp_file.rb', line 37 def write(data) @tempfile.write(data) end |