class BakShell::Indexer

Public Instance Methods

add(target) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/bak-shell/indexer.rb, line 89
def backups
  self.load! if @backups.nil?

  @backups
end
find_by_id(id) click to toggle source
# 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) click to toggle source
# File lib/bak-shell/indexer.rb, line 111
def find_by_target(target)
  self.backups.find { |b| target == b[:target] }
end
ids() click to toggle source
# File lib/bak-shell/indexer.rb, line 77
def ids
  self.load! if @ids.nil?

  @ids
end
index_file() click to toggle source
# 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?() click to toggle source
# File lib/bak-shell/indexer.rb, line 73
def index_file_exists?
  File.file?(self.index_file)
end
load!() click to toggle source
# 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
Also aliased as: reload!
reload!()
Alias for: load!
remove(ids) click to toggle source
# 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() click to toggle source
# File lib/bak-shell/indexer.rb, line 83
def targets
  self.load! if @targets.nil?

  @targets
end