Module: Cjoiner::Helpers::Files

Included in:
Engines::Engine, Joiner
Defined in:
lib/cjoiner/helpers.rb

Instance Method Summary collapse

Instance Method Details

#delete_file(file) ⇒ Object

delete a file, can be File or String



56
57
58
59
# File 'lib/cjoiner/helpers.rb', line 56

def delete_file(file)
  item = file.kind_of?(File) ? file.path : file
  File.delete item
end

#expand_path(file) ⇒ Object

return Pathname.new.expand_path



67
68
69
# File 'lib/cjoiner/helpers.rb', line 67

def expand_path(file)
  Pathname.new(file).expand_path
end

#file(file) ⇒ Object

use Pathname



72
73
74
# File 'lib/cjoiner/helpers.rb', line 72

def file(file)
  Pathname.new file
end

#file_exists(file, halt = true) ⇒ Object

check if a file exists



11
12
13
14
15
# File 'lib/cjoiner/helpers.rb', line 11

def file_exists(file, halt = true)
  check = ::File.exists? file
  raise(Cjoiner::Errors::FileNotFound, file) if !check and halt
  check
end

#load_yaml(file) ⇒ Object

load a yaml file



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

def load_yaml(file)
  if file_exists(file)
    ::YAML::load_file(file)
  end
end

#move_file(from, to) ⇒ Object

move a file



25
26
27
28
# File 'lib/cjoiner/helpers.rb', line 25

def move_file(from, to)
  return false if from == to
  FileUtils.mv from, to
end

#on_windowsObject

RUBY_PLATFORM tells cygwin or mswin



62
63
64
# File 'lib/cjoiner/helpers.rb', line 62

def on_windows
  (RUBY_PLATFORM =~ /cygwin|mswin/) != nil
end

#read_file(file) ⇒ Object

read file



31
32
33
# File 'lib/cjoiner/helpers.rb', line 31

def read_file(file)
  File.read(file) if file_exists file
end

#temp_file(file, data) ⇒ Object

create a temporal file



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cjoiner/helpers.rb', line 41

def temp_file(file, data)
  # due to some java+windows+cygwin limitation
  # we need to create our custom tempfile
  if on_windows
    name = "_cjoiner_tmp_#{file}"
    temp = File.new(name, "w")
    temp.write(data)
  else
    temp = Tempfile.new(file) << data
  end
  temp.close
  temp
end

#write_file(file, data) ⇒ Object

write data to file



36
37
38
# File 'lib/cjoiner/helpers.rb', line 36

def write_file(file, data)
  file.open("w") { |io| io.puts data }
end