Class: Backenup::Storage
- Inherits:
-
Object
- Object
- Backenup::Storage
- Defined in:
- lib/backenup.rb
Instance Attribute Summary collapse
-
#base_path ⇒ Object
readonly
Returns the value of attribute base_path.
Instance Method Summary collapse
-
#clear ⇒ Object
Clears the current contents of the storage.
- #commit(message = nil) ⇒ Object
- #cp(src, dest = nil) ⇒ Object
- #default_message ⇒ Object
-
#initialize(base_path) ⇒ Storage
constructor
A new instance of Storage.
-
#ls(path = ".") ⇒ Object
Convenient way to see the contents of the storage.
- #rm(dest) ⇒ Object
- #storage_path ⇒ Object
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_path ⇒ Object (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
#clear ⇒ Object
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( = nil) = if .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 # 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_message ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/backenup.rb', line 69 def 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_path ⇒ Object
17 18 19 |
# File 'lib/backenup.rb', line 17 def storage_path File.join(self.base_path, "storage") end |