Module: FileBase

Includes:
FileUtils
Included in:
Kickstart::Repository, Model, Resource
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



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

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

#exists?(type, item) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/filebase.rb', line 29

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

#file_pathObject



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

def file_path; home+location+filename end

#has_location?Boolean

Returns:

  • (Boolean)


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

def has_location?; location!='' end

#homeObject



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

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

#locationObject Also known as: loc



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

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

#make_dir(path, force = false) ⇒ Object



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

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
# File 'lib/filebase.rb', line 53

def mkdir where, force=false
  begin 
    Dir.mkdir where
  rescue
    raise $! unless force
    destroy where
    mkdir where
  end
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



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

def path; home+location end

#reset_locationObject



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

def reset_location; @location='' end

#set_home(here) ⇒ Object



16
17
18
19
# File 'lib/filebase.rb', line 16

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

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



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

def set_location where; @location=where end

#write(contents, where) ⇒ Object



41
42
43
# File 'lib/filebase.rb', line 41

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