Skip to content
Browse files

Only use one sdbm database for all models

  • Loading branch information...
1 parent 8b0753c commit 60a9738a22205e1d23cb92414a548f81dfb427c8 Andy Delcambre committed Dec 1, 2011
Showing with 19 additions and 5 deletions.
  1. +0 −1 Gemfile
  2. +19 −4 lib/cubbyhole/sdbm.rb
View
1 Gemfile
@@ -6,5 +6,4 @@ gemspec
group :test do
gem 'rspec'
gem 'rake'
- gem 'ruby-debug'
end
View
23 lib/cubbyhole/sdbm.rb
@@ -3,20 +3,35 @@
module Cubbyhole
class SDBM
extend Forwardable
- def_delegators :@sdbm, :keys, :delete, :clear
+ def_delegators :@sdbm, :clear
def initialize(name)
- @sdbm = ::SDBM.new("cubbyhole.#{name}.sdbm")
+ @name = name
+ @sdbm = ::SDBM.new("cubbyhole.sdbm")
end
def [](key)
- if str = @sdbm[key]
+ if str = @sdbm[munge_key(key)]
Marshal.load(str)
end
end
def []=(key, val)
- @sdbm[key] = Marshal.dump(val)
+ @sdbm[munge_key(key)] = Marshal.dump(val)
+ end
+
+ def keys
+ @sdbm.keys.grep(/^#{@name}:/).map do |key|
+ key.sub(/^#{@name}:/, "")
+ end
+ end
+
+ def delete(key)
+ @sdbm.delete(munge_key(key))
+ end
+
+ def munge_key(key)
+ "#{@name}:#{key}"
end
end

0 comments on commit 60a9738

Please sign in to comment.
Something went wrong with that request. Please try again.