/usr/lib/ruby/1.8/moneta/datamapper.rb is in libmoneta-ruby1.8 0.6.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | begin
gem "dm-core", "0.9.10"
require "dm-core"
rescue LoadError
puts "You need the dm-core gem in order to use the DataMapper moneta store"
exit
end
class MonetaHash
include DataMapper::Resource
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
end
end
module Moneta
class DataMapper
class Expiration
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(@repository)
end
def delete(key)
obj = get(key)
obj.expires = nil
obj.save(@repository)
end
private
def get(key)
repository(@repository) { @klass.get(key) }
end
end
def initialize(options = {})
@repository = options.delete(:repository) || :moneta
::DataMapper.setup(@repository, options[:setup])
repository_context { MonetaHash.auto_upgrade! }
@hash = MonetaHash
@expiration = Expiration.new(MonetaHash, @repository)
end
module Implementation
def key?(key)
repository_context { !!@hash.get(key) }
end
def has_key?(key)
repository_context { !!@hash.get(key) }
end
def [](key)
repository_context { @hash.value(key) }
end
def []=(key, value)
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, value = nil)
repository_context {
value ||= block_given? ? yield(key) : default
self[key] || value
}
end
def delete(key)
repository_context {
value = self[key]
@hash.all(:the_key => key).destroy!
value
}
end
def store(key, value, options = {})
repository_context { self[key] = value }
end
def clear
repository_context { @hash.all.destroy! }
end
private
def repository_context
repository(@repository) { yield }
end
end
include Implementation
include Expires
end
end
|