Module: Moneta::HashAdapter Private
- Included in:
- Adapters::KyotoCabinet, Adapters::LevelDB, Adapters::LocalMemCache, Adapters::Memory, Adapters::TDB, Adapters::TokyoCabinet, Adapters::TokyoTyrant, DBMAdapter
- Defined in:
- lib/moneta/hash_adapter.rb
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Instance Attribute Summary collapse
- #backend ⇒ Object readonly private
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#fetch_values(*keys, **options, &defaults) ⇒ Object
Behaves identically to #values_at except that it accepts an optional block.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#merge!(pairs, options = {}, &block) ⇒ 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.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
-
#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.
Instance Attribute Details
#backend ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
4 5 6 |
# File 'lib/moneta/hash_adapter.rb', line 4 def backend @backend end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
27 28 29 30 |
# File 'lib/moneta/hash_adapter.rb', line 27 def clear( = {}) @backend.clear self end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
22 23 24 |
# File 'lib/moneta/hash_adapter.rb', line 22 def delete(key, = {}) @backend.delete(key) 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.
39 40 41 42 43 |
# File 'lib/moneta/hash_adapter.rb', line 39 def fetch_values(*keys, **, &defaults) return super unless @backend.respond_to? :fetch_values defaults ||= {} # prevents KeyError @backend.fetch_values(*keys, &defaults) end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
7 8 9 |
# File 'lib/moneta/hash_adapter.rb', line 7 def key?(key, = {}) @backend.has_key?(key) end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
12 13 14 |
# File 'lib/moneta/hash_adapter.rb', line 12 def load(key, = {}) @backend[key] end |
#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self
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.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/moneta/hash_adapter.rb', line 52 def merge!(pairs, = {}, &block) return super unless method = [:merge!, :update].find do |method| @backend.respond_to? method end hash = Hash === pairs ? pairs : Hash[pairs.to_a] case method when :merge! @backend.merge!(hash, &block) when :update @backend.update(hash, &block) 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.
46 47 48 49 |
# File 'lib/moneta/hash_adapter.rb', line 46 def slice(*keys, **) return super unless @backend.respond_to? :slice @backend.slice(*keys) end |
#store(key, value, options = {}) ⇒ Object
Store value with key
17 18 19 |
# File 'lib/moneta/hash_adapter.rb', line 17 def store(key, value, = {}) @backend[key] = value 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.
33 34 35 36 |
# File 'lib/moneta/hash_adapter.rb', line 33 def values_at(*keys, **) return super unless @backend.respond_to? :values_at @backend.values_at(*keys) end |