Class: Backenup::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/backenup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path) ⇒ Storage

Returns a new instance of Storage.



11
12
13
14
15
# File 'lib/backenup.rb', line 11

def initialize(base_path)
  @base_path = File.absolute_path(base_path)
  
  @repo = File.exists?(base_path) ? Grit::Repo.new(base_path) : Grit::Repo.init(base_path)
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



9
10
11
# File 'lib/backenup.rb', line 9

def base_path
  @base_path
end

Instance Method Details

#clearObject

Clears the current contents of the storage.



46
47
48
49
50
# File 'lib/backenup.rb', line 46

def clear
  self.ls.each do |f|
    FileUtils.rm_rf File.join(self.storage_path, f)
  end
end

#commit(message = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/backenup.rb', line 53

def commit(message = nil)
  message = default_message if message.nil?
  
  Dir.chdir self.base_path do
    
    with_timeout 0 do
      with_max_size 0 do
        @repo.add "."             # Add all new / modified files
        @repo.commit_all message  # This handles any file that was deleted
      end
    end
  end

end

#cp(src, dest = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/backenup.rb', line 28

def cp(src, dest = nil)
  if dest.nil?
    dest = File.basename(src)
  end
  
  dest = File.join(self.storage_path, dest)
  
  make_storage_path_exist
  FileUtils.cp_r src, dest
end

#default_messageObject



69
70
71
72
73
74
75
76
# File 'lib/backenup.rb', line 69

def default_message
  files = self.ls
  if files.any?
    "Storage:\n#{files.map{|f| '  ' + f}.join("\n") }"
  else
    "[No files in storage]"
  end
end

#ls(path = ".") ⇒ Object

Convenient way to see the contents of the storage



22
23
24
25
# File 'lib/backenup.rb', line 22

def ls(path = ".")
  make_storage_path_exist
  Dir.entries(File.join(self.storage_path, path)).reject{|f| [".", ".."].include? f}
end

#rm(dest) ⇒ Object



40
41
42
# File 'lib/backenup.rb', line 40

def rm(dest)
  FileUtils.rm_rf File.join(self.storage_path, dest)
end

#storage_pathObject



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

def storage_path
  File.join(self.base_path, "storage")
end