class Dbox::DB

Constants

DB_FILE
DB_TMPFILE

Attributes

local_path[RW]
remote_path[RW]
root[RW]

Public Class Methods

api() click to toggle source
# File lib/dbox/db.rb, line 136
def self.api
  @api ||= API.connect
end
clone(remote_path, local_path) click to toggle source
# File lib/dbox/db.rb, line 19
def self.clone(remote_path, local_path)
  log.info "Cloning #{remote_path} into #{local_path}"
  res = api.metadata(remote_path)
  raise(BadPath, "Remote path error") unless remote_path == res["path"]
  db = new(local_path, res)
  db.pull
end
corrupt?(local_path) click to toggle source
# File lib/dbox/db.rb, line 43
def self.corrupt?(local_path)
  begin
    load(local_path)
    false
  rescue CorruptDatabase
    true
  end
end
create(remote_path, local_path) click to toggle source
# File lib/dbox/db.rb, line 14
def self.create(remote_path, local_path)
  api.create_dir(remote_path)
  clone(remote_path, local_path)
end
db_file(local_path) click to toggle source
# File lib/dbox/db.rb, line 144
def self.db_file(local_path)
  File.join(local_path, DB_FILE)
end
db_tmpfile(local_path) click to toggle source
# File lib/dbox/db.rb, line 148
def self.db_tmpfile(local_path)
  File.join(local_path, DB_TMPFILE)
end
destroy!(local_path) click to toggle source
# File lib/dbox/db.rb, line 63
def self.destroy!(local_path)
  FileUtils.rm(db_file(local_path)) if exists?(local_path)
end
exists?(local_path) click to toggle source
# File lib/dbox/db.rb, line 39
def self.exists?(local_path)
  File.exists?(db_file(local_path))
end
load(local_path) click to toggle source
# File lib/dbox/db.rb, line 52
def self.load(local_path)
  if exists?(local_path)
    db = File.open(db_file(local_path), "r") {|f| YAML::load(f.read) }
    raise CorruptDatabase unless db && db.kind_of?(DB)
    db.local_path = local_path
    db
  else
    raise MissingDatabase, "No DB file found in #{local_path}"
  end
end
move(new_remote_path, local_path) click to toggle source
# File lib/dbox/db.rb, line 35
def self.move(new_remote_path, local_path)
  load(local_path).move(new_remote_path)
end
new(local_path, res) click to toggle source

IMPORTANT: DropboxDb.new is private. Please use DropboxDb.create, DropboxDb.clone, or DropboxDb.load as the entry point.

# File lib/dbox/db.rb, line 69
def initialize(local_path, res)
  @local_path = local_path
  @remote_path = res["path"]
  FileUtils.mkdir_p(@local_path)
  @root = DropboxDir.new(self, res)
  save
end
pull(local_path) click to toggle source
# File lib/dbox/db.rb, line 27
def self.pull(local_path)
  load(local_path).pull
end
push(local_path) click to toggle source
# File lib/dbox/db.rb, line 31
def self.push(local_path)
  load(local_path).push
end
saving_timestamp(path) { || ... } click to toggle source
# File lib/dbox/db.rb, line 130
def self.saving_timestamp(path)
  mtime = File.mtime(path)
  yield
  File.utime(Time.now, mtime, path)
end

Public Instance Methods

api() click to toggle source
# File lib/dbox/db.rb, line 140
def api
  self.class.api
end
db_file() click to toggle source
# File lib/dbox/db.rb, line 152
def db_file
  self.class.db_file(@local_path)
end
db_tmpfile() click to toggle source
# File lib/dbox/db.rb, line 156
def db_tmpfile
  self.class.db_tmpfile(@local_path)
end
local_to_relative_path(path) click to toggle source
# File lib/dbox/db.rb, line 98
def local_to_relative_path(path)
  if path.include?(@local_path)
    path.sub(@local_path, "").sub(/^\//, "")
  else
    raise BadPath, "Not a local path: #{path}"
  end
end
move(new_remote_path) click to toggle source
# File lib/dbox/db.rb, line 92
def move(new_remote_path)
  api.move(@remote_path, new_remote_path)
  @remote_path = new_remote_path
  save
end
pull() click to toggle source
# File lib/dbox/db.rb, line 84
def pull
  @root.pull
end
push() click to toggle source
# File lib/dbox/db.rb, line 88
def push
  @root.push
end
relative_to_local_path(path) click to toggle source
# File lib/dbox/db.rb, line 114
def relative_to_local_path(path)
  if path && path.length > 0
    File.join(@local_path, path)
  else
    @local_path
  end
end
relative_to_remote_path(path) click to toggle source
# File lib/dbox/db.rb, line 122
def relative_to_remote_path(path)
  if path && path.length > 0
    File.join(@remote_path, path)
  else
    @remote_path
  end
end
remote_to_relative_path(path) click to toggle source
# File lib/dbox/db.rb, line 106
def remote_to_relative_path(path)
  if path.include?(@remote_path)
    path.sub(@remote_path, "").sub(/^\//, "")
  else
    raise BadPath, "Not a remote path: #{path}"
  end
end
save() click to toggle source
# File lib/dbox/db.rb, line 77
def save
  self.class.saving_timestamp(@local_path) do
    File.open(db_tmpfile, "w") {|f| f << YAML::dump(self) }
    FileUtils.mv(db_tmpfile, db_file)
  end
end