Module: Moneta::Defaults
- Includes:
- OptionSupport
- Included in:
- Adapter, Adapters::DataMapper, Adapters::File, Adapters::Null, Cache, Proxy, Stack
- Defined in:
- lib/moneta/defaults.rb
Overview
Simple interface to key/value stores with Hash-like interface.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
Fetch value with key.
-
#[]=(key, value) ⇒ Object
Store value with key.
-
#close ⇒ Object
Explicitly close the store.
-
#create(key, value, options = {}) ⇒ Boolean
Atomically sets a key to value if it’s not set.
-
#decrement(key, amount = 1, options = {}) ⇒ Object
Atomically decrement integer value with key.
-
#each_key ⇒ Object
Calls block once for each key in store, passing the key as a parameter.
-
#features ⇒ Array<Symbol>
Returns features list.
-
#fetch(key, default = nil, options = nil) ⇒ Object
Fetch a value with a key.
-
#fetch_values(*keys, **options) ⇒ Object
Behaves identically to #values_at except that it accepts an optional block.
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#merge!(pairs, options = {}) ⇒ Object
Stores the pairs in the key-value store, and returns itself.
-
#slice(*keys, **options) ⇒ <(Object, Object)>
Returns a collection of key-value pairs corresponding to those supplied keys which are present in the key-value store, and their associated values.
-
#supports?(feature) ⇒ Boolean
Return true if adapter supports the given feature.
-
#update(pairs, options = {}, &block) ⇒ Object
Stores the pairs in the key-value store, and returns itself.
-
#values_at(*keys, **options) ⇒ Array<Object, nil>
Returns an array containing the values associated with the given keys, in the same order as the supplied keys.
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Class Method Details
.included(base) ⇒ Object
50 51 52 |
# File 'lib/moneta/defaults.rb', line 50 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#[](key) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
145 146 147 |
# File 'lib/moneta/defaults.rb', line 145 def [](key) load(key) end |
#[]=(key, value) ⇒ Object
Store value with key
155 156 157 |
# File 'lib/moneta/defaults.rb', line 155 def []=(key, value) store(key, value) end |
#close ⇒ Object
Explicitly close the store
104 |
# File 'lib/moneta/defaults.rb', line 104 def close; 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.
189 190 191 |
# File 'lib/moneta/defaults.rb', line 189 def create(key, value, = {}) raise NotImplementedError, 'create is not supported' end |
#decrement(key, amount = 1, options = {}) ⇒ Object
Atomically decrement integer value with key
This is just syntactic sugar for calling #increment with a negative value.
This method also accepts negative amounts.
97 98 99 |
# File 'lib/moneta/defaults.rb', line 97 def decrement(key, amount = 1, = {}) increment(key, -amount, ) end |
#each_key ⇒ Enumerator #each_key {|key| ... } ⇒ self
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Calls block once for each key in store, passing the key as a parameter. If no block is given, an enumerator is returned instead.
173 174 175 |
# File 'lib/moneta/defaults.rb', line 173 def each_key raise NotImplementedError, 'each_key is not supported' end |
#features ⇒ Array<Symbol>
Returns features list
309 310 311 |
# File 'lib/moneta/defaults.rb', line 309 def features self.class.features end |
#fetch(key, options = {}, &block) ⇒ Object #fetch(key, default, options = {}) ⇒ Object
Fetch a value with a key
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/moneta/defaults.rb', line 129 def fetch(key, default = nil, = nil) if block_given? raise ArgumentError, 'Only one argument accepted if block is given' if result = load(key, default || {}) result == nil ? yield(key) : result else result = load(key, || {}) result == nil ? default : result end end |
#fetch_values(*keys, **options) ⇒ Object #fetch_values(*keys, **options) {|key| ... } ⇒ Array<Object, nil>
Some adapters may implement this method atomically. The default implmentation uses #values_at.
Behaves identically to #values_at except that it accepts an optional block. When supplied, the block will be called successively with each supplied key that is not present in the store. The return value of the block call will be used in place of nil in returned the array of values.
231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/moneta/defaults.rb', line 231 def fetch_values(*keys, **) values = values_at(*keys, **) return values unless block_given? keys.zip(values).map do |key, value| if value == nil yield key else value end end end |
#increment(key, amount = 1, options = {}) ⇒ Object
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Atomically increment integer value with key
This method also accepts negative amounts.
80 81 82 |
# File 'lib/moneta/defaults.rb', line 80 def increment(key, amount = 1, = {}) raise NotImplementedError, 'increment is not supported' end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
63 64 65 |
# File 'lib/moneta/defaults.rb', line 63 def key?(key, = {}) load(key, ) != nil end |
#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self
Some adapters may implement this method atomically, or in two passes when a block is provided. The default implmentation uses #key?, #load and #store.
Stores the pairs in the key-value store, and returns itself. When a block is provided, it will be called before overwriting any existing values with the key, old value and supplied value, and the return value of the block will be used in place of the supplied value.
290 291 292 293 294 295 296 297 298 299 |
# File 'lib/moneta/defaults.rb', line 290 def merge!(pairs, = {}) pairs.each do |key, value| if block_given? existing = load(key, ) value = yield(key, existing, value) unless existing == nil end store(key, value, ) end self end |
#slice(*keys, **options) ⇒ <(Object, Object)>
The keys in the return value may be the same objects that were supplied (i.e. Object#equal?), or may simply be equal (i.e. Object#==).
Some adapters may implement this method atomically. The default implmentation uses #values_at.
Returns a collection of key-value pairs corresponding to those supplied keys which are present in the key-value store, and their associated values. Only those keys present in the store will have pairs in the return value. The return value can be any enumerable object that yields pairs, so it could be a hash, but needn’t be.
260 261 262 263 264 |
# File 'lib/moneta/defaults.rb', line 260 def slice(*keys, **) keys.zip(values_at(*keys, **)).reject do |_, value| value == nil end end |
#supports?(feature) ⇒ Boolean
Return true if adapter supports the given feature.
316 317 318 |
# File 'lib/moneta/defaults.rb', line 316 def supports?(feature) features.include?(feature) end |
#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self
Some adapters may implement this method atomically, or in two passes when a block is provided. The default implmentation uses #key?, #load and #store.
Stores the pairs in the key-value store, and returns itself. When a block is provided, it will be called before overwriting any existing values with the key, old value and supplied value, and the return value of the block will be used in place of the supplied value.
302 303 304 |
# File 'lib/moneta/defaults.rb', line 302 def update(pairs, = {}, &block) merge!(pairs, , &block) end |
#values_at(*keys, **options) ⇒ Array<Object, nil>
Some adapters may implement this method atomically, but the default implementation simply makes repeated calls to #load.
Returns an array containing the values associated with the given keys, in the same order as the supplied keys. If a key is not present in the key-value-store, nil is returned in its place.
206 207 208 |
# File 'lib/moneta/defaults.rb', line 206 def values_at(*keys, **) keys.map { |key| load(key, ) } end |