Class: Dbox::DB

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/dbox/db.rb

Defined Under Namespace

Classes: DropboxBlob, DropboxDir, DropboxFile

Constant Summary collapse

DB_FILE =
".dropbox.db"
DB_TMPFILE =
".dropbox.db.tmp"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Loggable

included, #log

Constructor Details

#initialize(local_path, res) ⇒ DB

Returns a new instance of DB.



69
70
71
72
73
74
75
# 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

Instance Attribute Details

#local_pathObject

Returns the value of attribute local_path.



12
13
14
# File 'lib/dbox/db.rb', line 12

def local_path
  @local_path
end

#remote_pathObject

Returns the value of attribute remote_path.



12
13
14
# File 'lib/dbox/db.rb', line 12

def remote_path
  @remote_path
end

#rootObject

Returns the value of attribute root.



12
13
14
# File 'lib/dbox/db.rb', line 12

def root
  @root
end

Class Method Details

.apiObject



136
137
138
# File 'lib/dbox/db.rb', line 136

def self.api
  @api ||= API.connect
end

.clone(remote_path, local_path) ⇒ Object

Raises:



19
20
21
22
23
24
25
# File 'lib/dbox/db.rb', line 19

def self.clone(remote_path, local_path)
  log.info "Cloning #{remote_path} into #{local_path}"
  res = api.(remote_path)
  raise(BadPath, "Remote path error") unless remote_path == res["path"]
  db = new(local_path, res)
  db.pull
end

.corrupt?(local_path) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
# 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) ⇒ Object



14
15
16
17
# 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) ⇒ Object



144
145
146
# File 'lib/dbox/db.rb', line 144

def self.db_file(local_path)
  File.join(local_path, DB_FILE)
end

.db_tmpfile(local_path) ⇒ Object



148
149
150
# File 'lib/dbox/db.rb', line 148

def self.db_tmpfile(local_path)
  File.join(local_path, DB_TMPFILE)
end

.destroy!(local_path) ⇒ Object



63
64
65
# 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) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/dbox/db.rb', line 39

def self.exists?(local_path)
  File.exists?(db_file(local_path))
end

.load(local_path) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



35
36
37
# File 'lib/dbox/db.rb', line 35

def self.move(new_remote_path, local_path)
  load(local_path).move(new_remote_path)
end

.pull(local_path) ⇒ Object



27
28
29
# File 'lib/dbox/db.rb', line 27

def self.pull(local_path)
  load(local_path).pull
end

.push(local_path) ⇒ Object



31
32
33
# File 'lib/dbox/db.rb', line 31

def self.push(local_path)
  load(local_path).push
end

.saving_timestamp(path) ⇒ Object



130
131
132
133
134
# File 'lib/dbox/db.rb', line 130

def self.saving_timestamp(path)
  mtime = File.mtime(path)
  yield
  File.utime(Time.now, mtime, path)
end

Instance Method Details

#apiObject



140
141
142
# File 'lib/dbox/db.rb', line 140

def api
  self.class.api
end

#db_fileObject



152
153
154
# File 'lib/dbox/db.rb', line 152

def db_file
  self.class.db_file(@local_path)
end

#db_tmpfileObject



156
157
158
# File 'lib/dbox/db.rb', line 156

def db_tmpfile
  self.class.db_tmpfile(@local_path)
end

#local_to_relative_path(path) ⇒ Object



98
99
100
101
102
103
104
# 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) ⇒ Object



92
93
94
95
96
# 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

#pullObject



84
85
86
# File 'lib/dbox/db.rb', line 84

def pull
  @root.pull
end

#pushObject



88
89
90
# File 'lib/dbox/db.rb', line 88

def push
  @root.push
end

#relative_to_local_path(path) ⇒ Object



114
115
116
117
118
119
120
# 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) ⇒ Object



122
123
124
125
126
127
128
# 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) ⇒ Object



106
107
108
109
110
111
112
# 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

#saveObject



77
78
79
80
81
82
# 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