]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/registry.rb
added rake and updated Gemfile and Gemfile.lock
[user/henk/code/ruby/rbot.git] / lib / rbot / registry.rb
index 70b450ac2b5885d45ecf4b0c232c3f81ee1c5b91..04892d73d69bd4dd3cfa3aba773df3f7fba55eee 100644 (file)
@@ -57,6 +57,15 @@ class Registry
     @libpath = File.join(File.dirname(__FILE__), 'registry')
     @format = format
     load File.join(@libpath, @format+'.rb') if format
+    # The get_impl method will return all implementations of the
+    # abstract accessor interface, since we only ever load one
+    # (the configured one) accessor implementation, we can just assume
+    # it to be the correct accessor to use.
+    accessors = AbstractAccessor.get_impl
+    if accessors.length > 1
+      warning 'multiple accessor implementations loaded!'
+    end
+    @accessor_class = accessors.first
   end
 
   # Returns a list of supported registry database formats.
@@ -68,12 +77,7 @@ class Registry
 
   # Creates a new Accessor object for the specified database filename.
   def create(path, filename)
-    # The get_impl method will return a list of all the classes that
-    # implement the accessor interface, since we only ever load one
-    # (the configured one) accessor implementation, we can just assume
-    # it to be the correct accessor to use.
-    cls = AbstractAccessor.get_impl.first
-    db = cls.new(File.join(path, 'registry_' + @format, filename.downcase))
+    db = @accessor_class.new(File.join(path, 'registry_' + @format, filename.downcase))
     db.optimize
     db
   end
@@ -112,11 +116,12 @@ class Registry
       @registry = nil
       @default = nil
       @recovery = nil
+      @sub_registries = {}
     end
 
     def sub_registry(prefix)
       path = File.join(@filename.gsub(/\.[^\/\.]+$/,''), prefix.to_s)
-      return self.class.new(path)
+      @sub_registries[path] ||= self.class.new(path)
     end
 
     # creates the registry / subregistry folders
@@ -222,8 +227,8 @@ class Registry
     # like Hash#each
     def each(&block)
       return nil unless dbexists?
-      registry.each_key do |key|
-        block.call(key, self[key])
+      registry.each do |key, value|
+        block.call(key, restore(value))
       end
     end
 
@@ -268,6 +273,7 @@ class Registry
     end
 
     # delete a key from the registry
+    # returns the value in success, nil otherwise
     def delete(key)
       return default unless dbexists?
       value = registry.delete(key.to_s)
@@ -328,7 +334,7 @@ class Registry
 
     # Returns all classes from the namespace that implement this interface
     def self.get_impl
-      ObjectSpace.each_object(Class).select { |klass| klass < self }
+      ObjectSpace.each_object(Class).select { |klass| klass.ancestors[1] == self }
     end
   end