/
backends.rb
94 lines (78 loc) · 2.22 KB
/
backends.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
class FakeBackend
def read
# load file
return {"dev" => "/home/alexis/dev/"}
end
def write(shortcuts)
puts "writing files"
end
end
class SqliteBackend
def initialize(file=nil)
require 'sqlite3'
file ||= "shortcuts.db"
@file = file
if not File.exists?(@file)
@db = SQLite3::Database.new(@file)
self.create_tables
else
@db = SQLite3::Database.new(@file)
end
@hash = {}
end
def read
@db.execute("SELECT name, path FROM shortcuts;").each do |name, path|
@hash[name] = path
end
return @hash.clone
end
def write(shortcuts)
# check the differences between the old and the new hash and
# insert/delete when needed.
shortcuts.each do |key, value|
if @hash.has_key? key and @hash[key] != value
self.update(key, value)
elsif not @hash.has_key? key
self.create(key, value)
end
end
@hash.each do |key, value|
if not shortcuts.has_key? key
self.delete(key)
end
end
end
protected # all methods folowing that will be protected
def create_tables()
@db.execute("DROP TABLE IF EXISTS shortcuts;")
@db.execute("CREATE TABLE shortcuts (name varchar(100), path text);")
end
def create(name, path)
@db.execute("INSERT INTO shortcuts VALUES (\"#{name}\", \"#{path}\");")
end
def delete(name)
@db.execute("DELETE FROM shortcuts WHERE name = \"#{name}\";")
end
def update(name, path)
@db.execute("UPDATE shortcuts SET path=\"#{path}\" where name=\"#{name}\";")
end
end
class RedisBackend
def initialize(host=nil, port=nil)
require 'rubygems'
require 'redis'
require 'json'
@server = Redis.new(:host => host, :port => port)
end
def read
shortcuts = @server.get "shortcuts"
if shortcuts != nil
shortcuts = JSON.load(shortcuts)
end
shortcuts ||= {}
return shortcuts
end
def write(shortcuts)
@server.set "shortcuts", shortcuts.to_json
end
end