Class: Moneta::Proxy
Overview
Proxy base class
Direct Known Subclasses
Enumerable, Expires, Transformer, WeakCreate, WeakIncrement, Wrapper
Instance Attribute Summary collapse
- #adapter ⇒ Object readonly
Class Method Summary collapse
- .features_mask ⇒ Object private
-
.not_supports(*features) ⇒ Object
Declares that this adapter does not support the given feature, and adds a stub method that raises a NotImplementedError.
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#close ⇒ Object
Explicitly close the 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.
-
#each_key(&block) ⇒ Object
Calls block once for each key in store, passing the key as a parameter.
-
#features ⇒ Array<Symbol>
Returns features list.
-
#fetch_values(*keys, **options, &defaults) ⇒ 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.
-
#initialize(adapter, options = {}) ⇒ Proxy
constructor
A new instance of Proxy.
-
#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.
Methods included from Defaults
#[], #[]=, #decrement, #fetch, included, #supports?, #update
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(adapter, options = {}) ⇒ Proxy
Returns a new instance of Proxy.
11 12 13 |
# File 'lib/moneta/proxy.rb', line 11 def initialize(adapter, = {}) @adapter = adapter end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
7 8 9 |
# File 'lib/moneta/proxy.rb', line 7 def adapter @adapter end |
Class Method Details
.features_mask ⇒ Object
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.
126 127 128 |
# File 'lib/moneta/proxy.rb', line 126 def features_mask @features_mask ||= [].freeze end |
.not_supports(*features) ⇒ Object
Declares that this adapter does not support the given feature, and adds a stub method that raises a NotImplementedError. Useful when inheriting from another adapter.
131 132 133 134 |
# File 'lib/moneta/proxy.rb', line 131 def not_supports(*features) @features_mask = (features_mask | features).freeze super end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
93 94 95 96 |
# File 'lib/moneta/proxy.rb', line 93 def clear( = {}) adapter.clear() self end |
#close ⇒ Object
Explicitly close the store
41 42 43 |
# File 'lib/moneta/proxy.rb', line 41 def close adapter.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.
36 37 38 |
# File 'lib/moneta/proxy.rb', line 36 def create(key, value, = {}) adapter.create(key, value, ) end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
84 85 86 |
# File 'lib/moneta/proxy.rb', line 84 def delete(key, = {}) adapter.delete(key, ) 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.
21 22 23 24 25 26 27 28 |
# File 'lib/moneta/proxy.rb', line 21 def each_key(&block) raise NotImplementedError, "each_key is not supported on this proxy" \ unless supports? :each_key return enum_for(:each_key) { adapter.each_key.size } unless block_given? adapter.each_key(&block) self end |
#features ⇒ Array<Symbol>
Returns features list
120 121 122 |
# File 'lib/moneta/proxy.rb', line 120 def features @features ||= (self.class.features | adapter.features - self.class.features_mask).freeze 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.
104 105 106 |
# File 'lib/moneta/proxy.rb', line 104 def fetch_values(*keys, **, &defaults) adapter.fetch_values(*keys, **, &defaults) 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.
31 32 33 |
# File 'lib/moneta/proxy.rb', line 31 def increment(key, amount = 1, = {}) adapter.increment(key, amount, ) end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
16 17 18 |
# File 'lib/moneta/proxy.rb', line 16 def key?(key, = {}) adapter.key?(key, ) end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
56 57 58 |
# File 'lib/moneta/proxy.rb', line 56 def load(key, = {}) adapter.load(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.
114 115 116 117 |
# File 'lib/moneta/proxy.rb', line 114 def merge!(pairs, = {}, &block) adapter.merge!(pairs, , &block) 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.
109 110 111 |
# File 'lib/moneta/proxy.rb', line 109 def slice(*keys, **) adapter.slice(*keys, **) end |
#store(key, value, options = {}) ⇒ Object
Store value with key
71 72 73 |
# File 'lib/moneta/proxy.rb', line 71 def store(key, value, = {}) adapter.store(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.
99 100 101 |
# File 'lib/moneta/proxy.rb', line 99 def values_at(*keys, **) adapter.values_at(*keys, **) end |