Module: Cure::Helpers::FileHelpers

Included in:
Export::ChunkCsvProcessor, Export::CsvProcessor, Extract::Extractor, Launcher, Transformation::Transform
Defined in:
lib/cure/helpers/file_helpers.rb

Instance Method Summary collapse

Instance Method Details

#clean_dir(path) ⇒ Object



18
19
20
21
22
# File 'lib/cure/helpers/file_helpers.rb', line 18

def clean_dir(path)
  dir = File.file?(path) ? File.dirname(path) : path

  FileUtils.remove_dir(dir) if File.directory?(dir)
end

#open_file(file_location) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/cure/helpers/file_helpers.rb', line 33

def open_file(file_location)
  result = file_location.start_with?("/") ? file_location : Pathname.new(file_location)

  raise "No file found at [#{file_location}]" unless File.exist? result.to_s

  File.open(result)
end

#read_file(file_location) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/cure/helpers/file_helpers.rb', line 24

def read_file(file_location)
  result = file_location.start_with?("/") ? file_location : Pathname.new(file_location)
  # result = file_location.start_with?("/") ? file_location : File.join(File.dirname(__FILE__), file_location)

  raise "No file found at [#{file_location}]" unless File.exist? result.to_s

  File.read(result)
end

#with_file(path, extension, &block) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/cure/helpers/file_helpers.rb', line 9

def with_file(path, extension, &block)
  dir = File.dirname(path)

  FileUtils.mkdir_p(dir) unless File.directory?(dir)

  path = "#{path}.#{extension}"
  File.open(path, "w", &block)
end

#with_temp_dir(temp_dir, &_block) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/cure/helpers/file_helpers.rb', line 41

def with_temp_dir(temp_dir, &_block)
  return unless block_given?

  clean_dir(temp_dir)
  yield
  clean_dir(temp_dir)
end