Module: Tres::FileMethods

Extended by:
FileMethods
Included in:
AssetManager, FileMethods, Packager, TemplateManager
Defined in:
lib/ext/filemethods.rb

Instance Method Summary collapse

Instance Method Details

#append_to_file(path, contents) ⇒ Object



50
51
52
# File 'lib/ext/filemethods.rb', line 50

def append_to_file path, contents
  File.open path, 'a' do |file| file << contents end
end

#basename(file, take = '') ⇒ Object



88
89
90
# File 'lib/ext/filemethods.rb', line 88

def basename file, take = ''
  File.basename file, take
end

#change_dir(to) ⇒ Object



15
16
17
# File 'lib/ext/filemethods.rb', line 15

def change_dir to
  Dir.chdir to
end

#copy(from, to) ⇒ Object



27
28
29
30
31
# File 'lib/ext/filemethods.rb', line 27

def copy from, to
  Dir[from].each do |something|
    File.directory?(something) ? FileUtils.cp_r(something, to) : FileUtils.cp(something, to)
  end
end

#create_file(path, contents) ⇒ Object



46
47
48
# File 'lib/ext/filemethods.rb', line 46

def create_file path, contents
  File.open path, 'w' do |file| file << contents end
end

#delete!(file) ⇒ Object



92
93
94
# File 'lib/ext/filemethods.rb', line 92

def delete! file
  dir?(file) ? FileUtils.rm_rf(file) : FileUtils.rm(file) rescue nil
end

#dir?(dir) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/ext/filemethods.rb', line 11

def dir? dir
  File.directory? dir
end

#dirname(file) ⇒ Object



76
77
78
# File 'lib/ext/filemethods.rb', line 76

def dirname file
  File.dirname file
end

#erb(file) ⇒ Object



58
59
60
# File 'lib/ext/filemethods.rb', line 58

def erb file
  ERB.new(read_file(file)).result(binding)
end

#expand(path) ⇒ Object



72
73
74
# File 'lib/ext/filemethods.rb', line 72

def expand path
  File.expand_path path
end

#extname(file) ⇒ Object



84
85
86
# File 'lib/ext/filemethods.rb', line 84

def extname file
  File.extname file
end

#file?(file) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/ext/filemethods.rb', line 7

def file? file
  File.exists? file
end

#json(file) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/ext/filemethods.rb', line 62

def json file
  begin
    JSON.parse read_file(file)
  rescue Errno::ENOENT
    raise Tres::NoSuchFile, "File #{file} doesn't exist"
  rescue 
    raise Tres::CantParseJSONFile, "File #{file} doesn't appear to be valid JSON"
  end
end

#mkdir_p(dir) ⇒ Object



80
81
82
# File 'lib/ext/filemethods.rb', line 80

def mkdir_p dir
  FileUtils.mkdir_p dir
end

#move(from, to) ⇒ Object



19
20
21
# File 'lib/ext/filemethods.rb', line 19

def move from, to
  Dir[from].each do |something| FileUtils.mv something, to end
end

#name_and_extension(file) ⇒ Object



54
55
56
# File 'lib/ext/filemethods.rb', line 54

def name_and_extension file
  [basename(file, extname(file)), extname(file).sub(/^./, '')]
end

#new_dir(dir, careful = true) ⇒ Object



33
34
35
36
# File 'lib/ext/filemethods.rb', line 33

def new_dir dir, careful = true
  confirm "#{dir} already exists. Proceed?" if careful and dir?(dir)
  FileUtils.mkdir_p dir
end

#new_file(path) ⇒ Object



96
97
98
99
100
101
# File 'lib/ext/filemethods.rb', line 96

def new_file path
  File.open path, 'w' do |file|
    yieldage = yield if block_given?
    file.write yieldage unless yieldage.empty? or not yieldage.is_a?(String)
  end
end

#read_file(file) ⇒ Object



38
39
40
# File 'lib/ext/filemethods.rb', line 38

def read_file file
  File.read file
end

#readlines(file) ⇒ Object



42
43
44
# File 'lib/ext/filemethods.rb', line 42

def readlines file
  File.readlines file
end

#relativize(path, root) ⇒ Object



23
24
25
# File 'lib/ext/filemethods.rb', line 23

def relativize path, root
  Pathname(path).relative_path_from(Pathname(root)).to_s
end