Module: Shining::FileMethods

Included in:
Heroku
Defined in:
lib/ext/filemethods.rb

Instance Method Summary collapse

Instance Method Details

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



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

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

#change_dir(to) ⇒ Object



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

def change_dir to
  Dir.chdir to
end

#copy(from, to) ⇒ Object



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

def copy from, to
  Shining.say("  #{"Copying".color(:blue).bright}\t#{from} to #{to}") {
    Dir[from].each do |something|
      File.directory?(something) ? FileUtils.cp_r(something, to) : FileUtils.cp(something, to)
    end
  }
end

#delete!(file) ⇒ Object



77
78
79
80
81
# File 'lib/ext/filemethods.rb', line 77

def delete! file
  Shining.say " #{"Deleting".color(:red).bright}\t#{file}" do
    dir?(file) ? FileUtils.rm_rf(file) : FileUtils.rm(file) rescue nil
  end
end

#dir?(dir) ⇒ Boolean

Returns:

  • (Boolean)


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

def dir? dir
  File.directory? dir
end

#dirname(file) ⇒ Object



65
66
67
# File 'lib/ext/filemethods.rb', line 65

def dirname file
  File.dirname file
end

#erb(file) ⇒ Object



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

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

#expand(path) ⇒ Object



61
62
63
# File 'lib/ext/filemethods.rb', line 61

def expand path
  File.expand_path path
end

#extname(file) ⇒ Object



69
70
71
# File 'lib/ext/filemethods.rb', line 69

def extname file
  File.extname file
end

#file?(file) ⇒ Boolean

Returns:

  • (Boolean)


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

def file? file
  File.exists? file
end

#json(file) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/ext/filemethods.rb', line 51

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

#move(from, to) ⇒ Object



20
21
22
23
24
# File 'lib/ext/filemethods.rb', line 20

def move from, to
  Shining.say("  Moving\t#{from} to #{to}") {
    Dir[from].each do |something| FileUtils.mv something, to end
  }
end

#name_and_extension(file) ⇒ Object



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

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

#new_dir(dir, careful = true) ⇒ Object



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

def new_dir dir, careful = true
  confirm "#{dir} already exists. Proceed?" if careful and dir?(dir)
  Shining.say("  #{"Creating".color(:green).bright}\t#{dir}") { FileUtils.mkdir_p dir }
end

#new_file(path) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/ext/filemethods.rb', line 83

def new_file path
  Shining.say "  #{file?(path) ? "Editing".color(:yellow).bright : "Creating".color(:green).bright}\t#{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



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

def read_file file
  File.read file
end