Class: Sq::Dbsync::TempfileFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/sq/dbsync/tempfile_factory.rb

Overview

Provide extra functionality on top of the standard tempfile API.

Class Method Summary collapse

Class Method Details

.make(name) ⇒ Object

ENV is explicitly referenced here, since a change to JRuby in 1.7.0 makes ‘Dir.tmpdir` preference non-world writable directories first, of which `.` is a member. This makes it impossible to configure a world writable directory solely via the environment.



11
12
13
# File 'lib/sq/dbsync/tempfile_factory.rb', line 11

def self.make(name)
  Tempfile.new(name, ENV['TMPDIR'] || Dir.tmpdir)
end

.make_with_content(name, content) ⇒ Object



15
16
17
18
19
20
# File 'lib/sq/dbsync/tempfile_factory.rb', line 15

def self.make_with_content(name, content)
  file = make(name)
  file.write(content)
  file.flush
  file
end

.make_world_writable(name) ⇒ Object

A world writable file is necessary if it is being used as a communication mechanism with other processes (such as MySQL ‘LOAD DATA INFILE`).



24
25
26
27
28
# File 'lib/sq/dbsync/tempfile_factory.rb', line 24

def self.make_world_writable(name)
  file = make(name)
  file.chmod(0666)
  file
end

.split(file, n, logger, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/sq/dbsync/tempfile_factory.rb', line 30

def self.split(file, n, logger, &block)
  `split -l #{n} #{file.path} #{file.path}.`
  files = Dir[file.path + '.*']
  files.each_with_index do |tempfile, i|
    logger.log("Loading chunk #{i+1}/#{files.length}")
    block.call(tempfile)
    FileUtils.rm(tempfile)
  end
end