git » nmdb » commit 81834a7

Implement [], []= and include? in the Ruby GenericDB.

author Alberto Bertogli
2007-06-29 13:29:54 UTC
committer Alberto Bertogli
2007-06-29 13:29:54 UTC
parent cdc1ab2d41cf144948725f3a72d7264fee7e0948

Implement [], []= and include? in the Ruby GenericDB.

Instead of aliasing identical things, assume we will have set and get,
and implement [], []= and include? as generic functions.

Signed-off-by: Alberto Bertogli <albertito@gmail.com>

bindings/ruby/nmdb.rb +17 -6

diff --git a/bindings/ruby/nmdb.rb b/bindings/ruby/nmdb.rb
index a47c0a0..a78ab72 100644
--- a/bindings/ruby/nmdb.rb
+++ b/bindings/ruby/nmdb.rb
@@ -132,6 +132,23 @@ class GenericDB
 	def cache_cas(key, old, new)
 		return generic_cas(@db.method(:cache_cas), key, old, new)
 	end
+
+
+	# The following functions asume we have set(), get(), delete() and
+	# cas(), which are supposed to be implemented by our subclasses
+
+	def include?(key)
+		# we assume we have get() implemented
+		return get(key) == @default
+	end
+
+	def []=(key, val)
+		return set(key, val)
+	end
+
+	def [](key)
+		return get(key)
+	end
 end
 
 
@@ -140,8 +157,6 @@ class DB < GenericDB
 	alias get normal_get
 	alias delete normal_delete
 	alias cas normal_cas
-	alias []= set
-	alias [] get
 end
 
 class Cache < GenericDB
@@ -149,8 +164,6 @@ class Cache < GenericDB
 	alias get cache_get
 	alias delete cache_delete
 	alias cas cache_cas
-	alias []= set
-	alias [] get
 end
 
 class Sync < GenericDB
@@ -158,8 +171,6 @@ class Sync < GenericDB
 	alias get normal_get
 	alias delete delete_sync
 	alias cas normal_cas
-	alias []= set
-	alias [] get
 end
 
 end