Module: Shining::FileMethods
- Defined in:
- lib/ext/filemethods.rb
Instance Method Summary collapse
- #basename(file, take = '') ⇒ Object
- #copy(from, to) ⇒ 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
- #move(from, to) ⇒ Object
- #new_dir(dir, careful = true) ⇒ Object
- #new_file(path) ⇒ Object
- #read_file(file) ⇒ Object
Instance Method Details
#basename(file, take = '') ⇒ Object
52 53 54 |
# File 'lib/ext/filemethods.rb', line 52 def basename file, take = '' File.basename file, take end |
#copy(from, to) ⇒ Object
19 20 21 |
# File 'lib/ext/filemethods.rb', line 19 def copy from, to Shining.say("Copying #{from} to #{to}") { File.directory?(from) ? FileUtils.cp_r(from, to) : FileUtils.cp(from, to) } end |
#delete!(file) ⇒ Object
56 57 58 |
# File 'lib/ext/filemethods.rb', line 56 def delete! file dir?(file) ? FileUtils.rm_rf(file) : FileUtils.rm(file) end |
#dir?(dir) ⇒ Boolean
11 12 13 |
# File 'lib/ext/filemethods.rb', line 11 def dir? dir File.directory? dir end |
#dirname(file) ⇒ Object
44 45 46 |
# File 'lib/ext/filemethods.rb', line 44 def dirname file File.dirname file end |
#erb(file) ⇒ Object
32 33 34 |
# File 'lib/ext/filemethods.rb', line 32 def erb file ERB.new(read_file(file)).result(binding) end |
#expand(path) ⇒ Object
40 41 42 |
# File 'lib/ext/filemethods.rb', line 40 def path File. path end |
#extname(file) ⇒ Object
48 49 50 |
# File 'lib/ext/filemethods.rb', line 48 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
36 37 38 |
# File 'lib/ext/filemethods.rb', line 36 def json file JSON.parse read_file(file) end |
#move(from, to) ⇒ Object
15 16 17 |
# File 'lib/ext/filemethods.rb', line 15 def move from, to Shining.say("Moving #{from} to #{to}") { FileUtils.mv(from, to) } end |
#new_dir(dir, careful = true) ⇒ Object
23 24 25 26 |
# File 'lib/ext/filemethods.rb', line 23 def new_dir dir, careful = true confirm "#{dir} already exists. Proceed?" if careful and dir?(dir) Shining.say("Creating directory #{dir}") { FileUtils.mkdir_p dir } end |
#new_file(path) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/ext/filemethods.rb', line 60 def new_file path Shining.say "Creating file #{path}" do 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 end |
#read_file(file) ⇒ Object
28 29 30 |
# File 'lib/ext/filemethods.rb', line 28 def read_file file File.read file end |