Class: BakShell::Indexer
- Inherits:
-
Object
- Object
- BakShell::Indexer
- Includes:
- Singleton
- Defined in:
- lib/bak-shell/indexer.rb
Instance Method Summary collapse
- #add(target) ⇒ Object
- #backup_with_id(id) ⇒ Object
- #backup_with_target(target) ⇒ Object
- #backups ⇒ Object
- #find_by_id(id) ⇒ Object
- #find_by_target(target) ⇒ Object
- #ids ⇒ Object
- #index_file ⇒ Object
- #index_file_exists? ⇒ Boolean
- #load! ⇒ Object (also: #reload!)
- #remove(ids) ⇒ Object
- #targets ⇒ Object
Instance Method Details
#add(target) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/bak-shell/indexer.rb', line 38 def add(target) if self.targets.include?(target) raise "Attempt to add an existing backup to the index" end begin id = SecureRandom.hex end while self.ids.include?(id) CSV.open(self.index_file, "ab") { |f| f << [id, target] } self.ids << id self.targets << target self.backups << { id: id, target: target, persistent: false } @backup = OpenStruct.new(self.backups.last) end |
#backup_with_id(id) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/bak-shell/indexer.rb', line 10 def backup_with_id(id) return @backup if !@backup.nil? && id == @backup.id backup = self.find_by_id(id) if backup.nil? @backup = nil else @backup = OpenStruct.new(backup) end @backup end |
#backup_with_target(target) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/bak-shell/indexer.rb', line 24 def backup_with_target(target) return @backup if !@backup.nil? && target == @backup[:target] backup = self.find_by_target(target) if backup.nil? @backup = nil else @backup = OpenStruct.new(backup) end @backup end |
#backups ⇒ Object
89 90 91 92 93 |
# File 'lib/bak-shell/indexer.rb', line 89 def backups self.load! if @backups.nil? @backups end |
#find_by_id(id) ⇒ Object
107 108 109 |
# File 'lib/bak-shell/indexer.rb', line 107 def find_by_id(id) self.backups.find { |b| id == b[:id] } end |
#find_by_target(target) ⇒ Object
111 112 113 |
# File 'lib/bak-shell/indexer.rb', line 111 def find_by_target(target) self.backups.find { |b| target == b[:target] } end |
#ids ⇒ Object
77 78 79 80 81 |
# File 'lib/bak-shell/indexer.rb', line 77 def ids self.load! if @ids.nil? @ids end |
#index_file ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/bak-shell/indexer.rb', line 64 def index_file if @index_file.nil? @index_file = File.join(BakShell::BACKUP_DIR, "baklist.index") FileUtils.touch(@index_file) end @index_file end |
#index_file_exists? ⇒ Boolean
73 74 75 |
# File 'lib/bak-shell/indexer.rb', line 73 def index_file_exists? File.file?(self.index_file) end |
#load! ⇒ Object Also known as: reload!
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/bak-shell/indexer.rb', line 95 def load! @ids = Array.new @targets = Array.new @backups = Array.new CSV.foreach(self.index_file) do |row| @ids << row.first @targets << row.last @backups << { id: row.first, target: row.last, persistent: true } end end |
#remove(ids) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/bak-shell/indexer.rb', line 56 def remove(ids) new_index = self.backups.select { |b| !ids.include?(b[:id]) } new_index.map { |b| b.delete(:persistent) } CSV.open(self.index_file, "wb") do |f| new_index.each { |b| f << b.values } end end |
#targets ⇒ Object
83 84 85 86 87 |
# File 'lib/bak-shell/indexer.rb', line 83 def targets self.load! if @targets.nil? @targets end |