Class: Moneta::Adapters::DataMapper
- Inherits:
-
Object
- Object
- Moneta::Adapters::DataMapper
- Defined in:
- lib/moneta/adapters/datamapper.rb
Overview
Datamapper backend
Defined Under Namespace
Classes: Store
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#create(key, value, options = {}) ⇒ Boolean
Atomically sets a key to value if it’s not set.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#initialize(options = {}) ⇒ DataMapper
constructor
A new instance of DataMapper.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods included from NilValues
#fetch_values, #merge!, #slice
Methods included from Config
Methods included from Defaults
#[], #[]=, #close, #decrement, #each_key, #features, #fetch, #fetch_values, included, #increment, #merge!, #slice, #supports?, #update, #values_at
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ DataMapper
Returns a new instance of DataMapper.
31 32 33 34 35 36 |
# File 'lib/moneta/adapters/datamapper.rb', line 31 def initialize( = {}) configure() Store.storage_names[config.repository] = config.table.to_s ::DataMapper.setup(config.repository, config.setup) context { Store.auto_upgrade! } end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
90 91 92 93 |
# File 'lib/moneta/adapters/datamapper.rb', line 90 def clear( = {}) context { Store.all.destroy! } self end |
#create(key, value, options = {}) ⇒ Boolean
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Atomically sets a key to value if it’s not set.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/moneta/adapters/datamapper.rb', line 67 def create(key, value, = {}) context do Store.create(k: key, v: value) true end rescue # FIXME: This catches too many errors # it should only catch a not-unique-exception false end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
79 80 81 82 83 84 85 86 87 |
# File 'lib/moneta/adapters/datamapper.rb', line 79 def delete(key, = {}) context do if record = Store.get(key) value = record.v record.destroy! value end end end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
39 40 41 |
# File 'lib/moneta/adapters/datamapper.rb', line 39 def key?(key, = {}) context { Store.get(key) != nil } end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
44 45 46 47 48 49 |
# File 'lib/moneta/adapters/datamapper.rb', line 44 def load(key, = {}) context do record = Store.get(key) record && record.v end end |
#store(key, value, options = {}) ⇒ Object
Store value with key
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/moneta/adapters/datamapper.rb', line 52 def store(key, value, = {}) context do if record = Store.get(key) record.update(k: key, v: value) else Store.create(k: key, v: value) end value end rescue tries ||= 0 (tries += 1) < 10 ? retry : raise end |