Class: Elf::Utilities::FilePool

Inherits:
Object
  • Object
show all
Defined in:
lib/elf/utils/pool.rb

Overview

Pool for ELF files.

This pool is useful for tools that recurse over a tree of dependencies to avoid creating multiple instances of Elf::File being open and accessing the same file.

Notes:

- Instances might be re-created if no strong references left to specific
  Elf::File
- New instance will be created on access if previous one were closed

Class Method Summary collapse

Class Method Details

.[](file) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/elf/utils/pool.rb', line 37

def self.[](file)
  realfile = Pathname.new(file).realpath

  cached = @pool[realfile].__getobj__ rescue nil
  if cached.nil? || cached.closed?
    cached = Elf::File.new(realfile)
    @pool[realfile] = WeakRef.new(cached)
  end

  cached
end