Module: Cure::Helpers::FileHelpers

Included in:
Export::Exporter, Extract::Extractor, Main, Transformation::Transform, Transformation::TransformResult
Defined in:
lib/cure/helpers/file_helpers.rb

Instance Method Summary collapse

Instance Method Details

#clean_dir(path) ⇒ Object



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

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

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

#read_file(file_location) ⇒ Object



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

def read_file(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

  File.read(result)
end

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



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

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



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

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

  clean_dir(temp_dir)
  yield
  clean_dir(temp_dir)
end