summaryrefslogtreecommitdiff
path: root/lib/rbot/registry
diff options
context:
space:
mode:
authorMatthias H <apoc@sixserv.org>2014-03-06 16:07:13 +0100
committerMatthias H <apoc@sixserv.org>2014-03-06 16:07:13 +0100
commitbaff61847ea90195e4ef1b063e4da521e02ab9d9 (patch)
tree72e89e8c43fdeafd4bbbcc86592a706c5148d7f5 /lib/rbot/registry
parent15be46dbc99df42db41c247967c9c504008de5cf (diff)
[registry] improved tests, sqlite/tc bugfixes
Diffstat (limited to 'lib/rbot/registry')
-rw-r--r--lib/rbot/registry/sqlite.rb19
-rw-r--r--lib/rbot/registry/tc.rb7
2 files changed, 22 insertions, 4 deletions
diff --git a/lib/rbot/registry/sqlite.rb b/lib/rbot/registry/sqlite.rb
index 1af5d242..6a4a2312 100644
--- a/lib/rbot/registry/sqlite.rb
+++ b/lib/rbot/registry/sqlite.rb
@@ -23,7 +23,7 @@ class Registry
begin
@registry.execute('SELECT COUNT(*) FROM data')
rescue
- @registry.execute('CREATE TABLE data (key string, value blob)')
+ @registry.execute('CREATE TABLE data (key PRIMARY KEY, value)')
end
end
@registry
@@ -37,7 +37,7 @@ class Registry
def [](key)
if dbexists?
begin
- value = @registry.get_first_row('SELECT value FROM data WHERE key = ?', key.to_s)
+ value = registry.get_first_row('SELECT value FROM data WHERE key = ?', key.to_s)
return restore(value.first)
rescue
return default
@@ -65,12 +65,18 @@ class Registry
end
end
+ alias each_pair each
+
def has_key?(key)
return nil unless dbexists?
res = registry.get_first_row('SELECT COUNT(*) FROM data WHERE key = ?', key.to_s)
return res.first > 0
end
+ alias include? has_key?
+ alias member? has_key?
+ alias key? has_key?
+
def has_value?(value)
return nil unless dbexists?
value = SQLite3::Blob.new(store(value))
@@ -81,10 +87,11 @@ class Registry
def delete(key)
return default unless dbexists?
begin
+ value = self[key]
registry.execute('DELETE FROM data WHERE key = ?', key.to_s)
- registry.changes > 0
+ value if registry.changes > 0
rescue
- false
+ nil
end
end
@@ -100,6 +107,8 @@ class Registry
registry.execute('DELETE FROM data')
end
+ alias truncate clear
+
# returns the number of keys in your registry namespace
def length
return 0 unless dbexists?
@@ -107,6 +116,8 @@ class Registry
res.first
end
+ alias size length
+
end
end # Registry
diff --git a/lib/rbot/registry/tc.rb b/lib/rbot/registry/tc.rb
index 4e248f42..f1835c8a 100644
--- a/lib/rbot/registry/tc.rb
+++ b/lib/rbot/registry/tc.rb
@@ -41,6 +41,13 @@ class Registry
@registry.optimize
end
+ def delete(key)
+ return default unless dbexists?
+ value = self[key]
+ registry.delete(key.to_s)
+ value # return deleted value if success
+ end
+
end
end # Registry