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 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.
26 27 28 29 30 31 32 |
# File 'lib/moneta/adapters/datamapper.rb', line 26 def initialize( = {}) raise ArgumentError, 'Option :setup is required' unless [:setup] @repository = (.delete(:repository) || :moneta).to_sym Store.storage_names[@repository] = (.delete(:table) || :moneta).to_s ::DataMapper.setup(@repository, [:setup]) context { Store.auto_upgrade! } end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
86 87 88 89 |
# File 'lib/moneta/adapters/datamapper.rb', line 86 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.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/moneta/adapters/datamapper.rb', line 63 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
75 76 77 78 79 80 81 82 83 |
# File 'lib/moneta/adapters/datamapper.rb', line 75 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
35 36 37 |
# File 'lib/moneta/adapters/datamapper.rb', line 35 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
40 41 42 43 44 45 |
# File 'lib/moneta/adapters/datamapper.rb', line 40 def load(key, = {}) context do record = Store.get(key) record && record.v end end |
#store(key, value, options = {}) ⇒ Object
Store value with key
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/moneta/adapters/datamapper.rb', line 48 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 |