class Fixture

Fixture class for Tempfiles.

@example Load a fixture file as a tempfile

Tempfile.Fixture('/path/to/data.txt').copy do |tempfile|
  # do something with the tempfile
end

# the tempfile should be gone

@example Don't unlink the tempfile

tempfile = Tempfile::Fixture.new('/path/to/data.txt').copy!
tempfile # => the tempfile

Constants

VERSION

Attributes

fixture[RW]
tempfile_options[RW]

Public Class Methods

new(path, tempfile_options: {}) click to toggle source
# File lib/tempfile/fixture.rb, line 26
def initialize(path, tempfile_options: {})
  self.fixture = path
  self.tempfile_options = tempfile_options
end

Public Instance Methods

copy(&block) click to toggle source
# File lib/tempfile/fixture.rb, line 38
def copy(&block)
  file = copy!(&block)
  self
ensure
  if file
    file.close unless file.closed?

    # Silently fail (hello windows)
    file.unlink rescue nil # rubocop:disable Style/RescueModifier
  end
end
copy!() { |file| ... } click to toggle source
# File lib/tempfile/fixture.rb, line 31
def copy!
  file = Tempfile.new(tempfile_args, tempfile_options)
  FileUtils.copy_file(fixture, file.path, true)
  yield file if block_given?
  file
end

Private Instance Methods

tempfile_args() click to toggle source
# File lib/tempfile/fixture.rb, line 52
def tempfile_args
  name = File.basename(fixture)
  ext = File.extname(fixture)

  # Some files don't have an extension!
  return name unless ext && ext.length.positive?

  [name, ext.start_with?('.') ? ext : ".#{ext}"]
end