Module: Tres::FileMethods
- Extended by:
- FileMethods
- Included in:
- AssetManager, FileMethods, Packager, TemplateManager
- Defined in:
- lib/ext/filemethods.rb
Instance Method Summary collapse
- #append_to_file(path, contents) ⇒ Object
- #basename(file, take = '') ⇒ Object
- #change_dir(to) ⇒ Object
- #copy(from, to) ⇒ Object
- #create_file(path, contents) ⇒ Object
- #delete!(file) ⇒ Object
- #dir?(dir) ⇒ Boolean
- #dirname(file) ⇒ Object
- #erb(file) ⇒ Object
- #expand(path) ⇒ Object
- #extname(file) ⇒ Object
- #file?(file) ⇒ Boolean
- #json(file) ⇒ Object
- #mkdir_p(dir) ⇒ Object
- #move(from, to) ⇒ Object
- #name_and_extension(file) ⇒ Object
- #new_dir(dir, careful = true) ⇒ Object
- #new_file(path) ⇒ Object
- #read_file(file) ⇒ Object
- #readlines(file) ⇒ Object
- #relativize(path, root) ⇒ Object
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
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 path File. 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
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 |