Module: Omnizip::Temp

Defined in:
lib/omnizip/temp.rb,
lib/omnizip/temp/temp_file.rb,
lib/omnizip/temp/safe_extract.rb,
lib/omnizip/temp/temp_file_pool.rb

Overview

Temporary file management with automatic cleanup Provides safe, atomic operations with RAII pattern

Defined Under Namespace

Classes: ArchiveHelper, Configuration, SafeExtract, TempFile, TempFilePool, TempFileRegistry

Class Method Summary collapse

Class Method Details

.cleanup_allObject

Cleanup all tracked temp files



91
92
93
# File 'lib/omnizip/temp.rb', line 91

def cleanup_all
  registry.cleanup_all
end

.configurationConfiguration

Global configuration

Returns:



26
27
28
# File 'lib/omnizip/temp.rb', line 26

def configuration
  @configuration ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Configure temp file operations

Yields:

  • (config)

    Configuration block



32
33
34
35
# File 'lib/omnizip/temp.rb', line 32

def configure
  yield configuration
  setup_cleanup_hooks if configuration.cleanup_on_exit
end

.directory(prefix: nil) {|path| ... } ⇒ Object

Create temporary directory with automatic cleanup

Parameters:

  • prefix (String) (defaults to: nil)

    Directory prefix

Yields:

  • (path)

    Block called with temp directory path

Returns:

  • (Object)

    Block return value



64
65
66
67
68
69
# File 'lib/omnizip/temp.rb', line 64

def directory(prefix: nil, &block)
  require "tmpdir"
  prefix ||= configuration.prefix

  Dir.mktmpdir(prefix, configuration.directory, &block)
end

.file(prefix: nil, suffix: "") {|path| ... } ⇒ Object

Create temporary file with automatic cleanup

Parameters:

  • prefix (String) (defaults to: nil)

    Filename prefix

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

    Filename suffix

Yields:

  • (path)

    Block called with temp file path

Returns:

  • (Object)

    Block return value



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/omnizip/temp.rb', line 42

def file(prefix: nil, suffix: "")
  prefix ||= configuration.prefix
  temp_file = TempFile.new(
    prefix: prefix,
    suffix: suffix,
    directory: configuration.directory,
  )

  registry.track(temp_file)

  begin
    yield(temp_file.path)
  ensure
    registry.untrack(temp_file)
    temp_file.unlink unless temp_file.kept?
  end
end

.registryTempFileRegistry

Get temp file registry

Returns:



86
87
88
# File 'lib/omnizip/temp.rb', line 86

def registry
  @registry ||= TempFileRegistry.new
end

.with_archive(format: :zip) {|archive| ... } ⇒ Object

Create temporary archive with automatic cleanup

Parameters:

  • format (Symbol) (defaults to: :zip)

    Archive format (:zip, :seven_zip)

Yields:

  • (archive)

    Block called with archive helper

Returns:

  • (Object)

    Block return value



75
76
77
78
79
80
81
82
# File 'lib/omnizip/temp.rb', line 75

def with_archive(format: :zip)
  suffix = format == :zip ? ".zip" : ".7z"

  file(suffix: suffix) do |path|
    archive = ArchiveHelper.new(path, format)
    yield(archive)
  end
end