Module: FileBase

Includes:
FileUtils
Included in:
Collection, Static
Defined in:
lib/filebase.rb

Constant Summary collapse

Home =
''

Instance Method Summary collapse

Instance Method Details

#copy(from, to = home) ⇒ Object



49
50
51
# File 'lib/filebase.rb', line 49

def copy(from, to=home)
  FileUtils.cp_r from, to
end

#destroy(what) ⇒ Object



69
70
71
72
# File 'lib/filebase.rb', line 69

def destroy what
  FileUtils.rm_rf what
  puts 'destroying '+what
end

#exists?(type, item) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/filebase.rb', line 24

def exists? type, item
  return false unless File.exist? item
  truth = case type
  when :folder
     return(File.directory?(item))
  when :file
     return(!File.directory?(item))
  else
     return(true)
  end
end

#file_pathObject



22
# File 'lib/filebase.rb', line 22

def file_path; home+location+filename end

#has_location?Boolean

Returns:

  • (Boolean)


19
# File 'lib/filebase.rb', line 19

def has_location?; location!='' end

#homeObject



9
# File 'lib/filebase.rb', line 9

def home; (@home||Home).as_folder end

#items_under(pth, match = nil) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/filebase.rb', line 36

def items_under(pth, match=nil)
  match||=''
  folders = []
  files = []
  Dir.entries(pth).each { |item| (exists?(:folder, pth.as_folder+item) ? folders << item : files << item) unless item[0,1]=='.' or not item=~/#{match}/}
  {:folders => folders, :files => files}
end

#locationObject Also known as: loc



15
# File 'lib/filebase.rb', line 15

def location; (@location||'').as_folder end

#make_dir(path, force = false) ⇒ Object



64
65
66
67
# File 'lib/filebase.rb', line 64

def make_dir path, force=false
  mkdir path, force
  puts 'creating '+path
end

#mkdir(where, force = false) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/filebase.rb', line 53

def mkdir where, force=false
  begin 
    Dir.mkdir where
  rescue
    raise $! unless force
    destroy where
    mkdir where
  end
  where
end

#move(from, to = home) ⇒ Object



45
46
47
# File 'lib/filebase.rb', line 45

def move(from, to=home)
  FileUtils.mv from, to
end

#pathObject



21
# File 'lib/filebase.rb', line 21

def path; home+location end

#reset_locationObject



18
# File 'lib/filebase.rb', line 18

def reset_location; @location='' end

#set_home(here) ⇒ Object



10
11
12
13
# File 'lib/filebase.rb', line 10

def set_home(here)
  mkdir here unless exists? :folder, here
  @home=here
end

#set_location(where) ⇒ Object Also known as: loc=



16
# File 'lib/filebase.rb', line 16

def set_location where; @location=where end

#setup_location(where) ⇒ Object



17
# File 'lib/filebase.rb', line 17

def setup_location where; @location=where; mkdir path unless exists? :folder, path end

#write(contents, where) ⇒ Object



74
75
76
# File 'lib/filebase.rb', line 74

def write(contents, where)
  File.open(where, 'w'){ |f| f << contents }
end

#write_template(what, where) ⇒ Object



77
78
79
# File 'lib/filebase.rb', line 77

def write_template what, where
  write ERB.new(File.read(what)).result(binding), where
end