Module: Cure::FileHelpers

Included in:
Export::Exporter, Main, Transformation::Transform, Transformation::TransformContext
Defined in:
lib/cure/file_helpers.rb

Instance Method Summary collapse

Instance Method Details

#clean_dir(path) ⇒ Object



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

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



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

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



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

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



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

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

  clean_dir(temp_dir)
  yield
  clean_dir(temp_dir)
end