Permalink
Browse files

Make #fetch work like Ruby fetch (thx voidlock)

  • Loading branch information...
1 parent a305fc2 commit f65e168fa38ee1aaf78d60802df3232f66fb49ca @wycats committed May 9, 2009
View
@@ -17,7 +17,7 @@ def [](key)
super
end
- def fetch(key, default)
+ def fetch(key, default = nil, &blk)
check_expired(key)
super
end
@@ -62,4 +62,11 @@ def update_options(key, options)
end
end
end
+
+ module Defaults
+ def fetch(key, value = nil)
+ value ||= block_given? ? yield(key) : default
+ self[key] || value
+ end
+ end
end
View
@@ -8,17 +8,18 @@
module Moneta
class Berkeley
+ include Defaults
def initialize(options={})
- file = options[:file]
+ file = @file = options[:file]
@db = Bdb::Db.new()
@db.open(nil, file, nil, Bdb::Db::BTREE, Bdb::DB_CREATE, 0)
unless options[:skip_expires]
@expiration = Moneta::Berkeley.new(:file => "#{file}_expiration", :skip_expires => true )
self.extend(StringExpires)
end
end
-
+
module Implementation
def key?(key)
nil | self[key]
@@ -38,10 +39,6 @@ def [](key)
@db[key]
end
- def fetch(key, default)
- self[key] || default
- end
-
def delete(key)
value = self[key]
@db.del(nil,key,0) if value
View
@@ -7,6 +7,8 @@
module Moneta
class Couch
+ include Defaults
+
def initialize(options = {})
@db = ::CouchRest.database!(options[:db])
unless options[:skip_expires]
@@ -35,10 +37,6 @@ def []=(key, value)
self[key]
end
- def fetch(key, default)
- self[key] || default
- end
-
def delete(key)
value = @db.get(key)
@db.delete_doc({"_id" => value["_id"], "_rev" => value["_rev"]}) if value
View
@@ -8,15 +8,11 @@
class MonetaHash
include DataMapper::Resource
-
- def self.default_repository_name
- :moneta
- end
-
+
property :the_key, String, :key => true
property :value, Object, :lazy => false
property :expires, Time
-
+
def self.value(key)
obj = self.get(key)
obj && obj.value
@@ -26,79 +22,93 @@ def self.value(key)
module Moneta
class DataMapper
class Expiration
- def initialize(klass)
+ def initialize(klass, repository)
@klass = klass
+ @repository = repository
end
-
+
def [](key)
if obj = get(key)
obj.expires
end
end
-
+
def []=(key, value)
obj = get(key)
obj.expires = value
- obj.save
+ obj.save(@repository)
end
-
+
def delete(key)
obj = get(key)
obj.expires = nil
- obj.save
+ obj.save(@repository)
end
-
+
private
def get(key)
- @klass.get(key)
+ repository(@repository) { @klass.get(key) }
end
end
-
+
def initialize(options = {})
- ::DataMapper.setup(:moneta, options[:setup])
- MonetaHash.auto_upgrade!
+ @repository = options.delete(:repository) || :moneta
+ ::DataMapper.setup(@repository, options[:setup])
+ repository_context { MonetaHash.auto_upgrade! }
@hash = MonetaHash
- @expiration = Expiration.new(MonetaHash)
+ @expiration = Expiration.new(MonetaHash, @repository)
end
-
+
module Implementation
def key?(key)
- !!@hash.get(key)
+ repository_context { !!@hash.get(key) }
end
-
+
def has_key?(key)
- !!@hash.get(key)
+ repository_context { !!@hash.get(key) }
end
-
+
def [](key)
- @hash.value(key)
+ repository_context { @hash.value(key) }
end
def []=(key, value)
- obj = @hash.get(key)
- if obj
- obj.update(key, value)
- else
- @hash.create(:the_key => key, :value => value)
- end
+ repository_context {
+ obj = @hash.get(key)
+ if obj
+ obj.update(key, value)
+ else
+ @hash.create(:the_key => key, :value => value)
+ end
+ }
end
-
- def fetch(key, default)
- self[key] || default
+
+ def fetch(key, value = nil)
+ repository_context {
+ value ||= block_given? ? yield(key) : default
+ self[key] || value
+ }
end
-
+
def delete(key)
- value = self[key]
- @hash.all(:the_key => key).destroy!
- value
+ repository_context {
+ value = self[key]
+ @hash.all(:the_key => key).destroy!
+ value
+ }
end
-
+
def store(key, value, options = {})
- self[key] = value
+ repository_context { self[key] = value }
end
-
+
def clear
- @hash.all.destroy!
+ repository_context { @hash.all.destroy! }
+ end
+
+ private
+ def repository_context
+ repository(@repository) { yield }
end
end
include Implementation
View
@@ -65,11 +65,7 @@ def []=(key, value)
file.puts(contents)
end
end
-
- def fetch(key, default)
- self[key] || default
- end
-
+
def delete(key)
value = self[key]
FileUtils.rm(path(key))
@@ -93,6 +89,7 @@ def path(key)
end
include Implementation
include Expires
+ include Defaults
end
end
View
@@ -25,8 +25,9 @@ def []=(key, value)
store(key, value)
end
- def fetch(key, default)
- self[key] || default
+ def fetch(key, value = nil)
+ value ||= block_given? ? yield(key) : default
+ self[key] || value
end
def delete(key)
View
@@ -7,6 +7,8 @@
module Moneta
class Redis
+ include Defaults
+
def initialize(options = {})
@cache = ::Redis.new(options)
end
@@ -24,11 +26,7 @@ def [](key)
def []=(key, value)
store(key, value)
end
-
- def fetch(key, default)
- @cache[key] || default
- end
-
+
def delete(key)
value = @cache[key]
@cache.delete(key) if value
View
@@ -26,8 +26,9 @@ def []=(key, value)
super(key, Marshal.dump(value))
end
- def fetch(key, default)
- self[key] || default
+ def fetch(key, value = nil)
+ value ||= block_given? ? yield(key) : default
+ self[key] || value
end
def store(key, value, options = {})
View
@@ -56,8 +56,9 @@ def []=(key, value)
store(key, value)
end
- def fetch(key, default)
- self[key] || default
+ def fetch(key, value = nil)
+ value ||= block_given? ? yield(key) : default
+ self[key] || value
end
def delete(key)
View
@@ -3,6 +3,8 @@
module Moneta
class BasicSDBM < ::SDBM
+ include Defaults
+
def [](key)
if val = super
Marshal.load(val)
@@ -13,10 +15,6 @@ def []=(key, value)
super(key, Marshal.dump(value))
end
- def fetch(key, default)
- self[key] || default
- end
-
def store(key, value, options = {})
self[key] = value
end
View
@@ -28,8 +28,9 @@ def []=(key, value)
super(key, Marshal.dump(value))
end
- def fetch(key, default)
- self[key] || default
+ def fetch(key, value = nil)
+ value ||= block_given? ? yield(key) : default
+ self[key] || value
end
def store(key, value, options = {})
View
@@ -37,7 +37,8 @@ def []=(key, value)
@hash.set(key, Marshal.dump(value))
end
- def fetch(key, value)
+ def fetch(key, value = nil)
+ value ||= block_given? ? yield(key) : default
self[key] || value
end
Oops, something went wrong.

0 comments on commit f65e168

Please sign in to comment.