Class: Omnizip::Temp::TempFile

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/temp/temp_file.rb

Overview

RAII-pattern temporary file with automatic cleanup

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix: "omniz_", suffix: "", directory: nil) ⇒ TempFile

Create new temporary file

Parameters:

  • prefix (String) (defaults to: "omniz_")

    Filename prefix

  • suffix (String) (defaults to: "")

    Filename suffix

  • directory (String, nil) (defaults to: nil)

    Directory (nil = system default)



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

#pathObject (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

#closeObject

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

#fileTempfile

Get the underlying Tempfile object

Returns:

  • (Tempfile)

    The tempfile



30
31
32
# File 'lib/omnizip/temp/temp_file.rb', line 30

def file
  @tempfile
end

#finalized?Boolean

Check if file has been finalized

Returns:

  • (Boolean)

    True if 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

Returns:

  • (Boolean)

    True if file won't be auto-deleted



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

Parameters:

  • length (Integer, nil) (defaults to: nil)

    Number of bytes to read

Returns:

  • (String, nil)

    Data read



44
45
46
# File 'lib/omnizip/temp/temp_file.rb', line 44

def read(length = nil)
  @tempfile.read(length)
end

#rewindObject

Rewind to beginning of file



49
50
51
# File 'lib/omnizip/temp/temp_file.rb', line 49

def rewind
  @tempfile.rewind
end

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

Parameters:

  • data (String)

    Data to write

Returns:

  • (Integer)

    Bytes written



37
38
39
# File 'lib/omnizip/temp/temp_file.rb', line 37

def write(data)
  @tempfile.write(data)
end