Module: Horcrux::Methods
Overview
Implements the optional methods of a Horcrux adapter.
Class Method Summary collapse
Instance Method Summary collapse
-
#delete_all(*keys) ⇒ Object
Public: Deletes the given keys.
-
#fetch(key) ⇒ Object
Public: Either gets the value of the key, or sets it if it doesn’t exist.
-
#get_all(*keys) ⇒ Object
Public: Gets all the values for the given keys.
-
#initialize(client, serializer = nil) ⇒ Object
Public: Sets up an adapter with the client.
-
#key?(key) ⇒ Boolean
Public: Determines if the key is set.
-
#key_for(key) ⇒ Object
Public: Transforms the given application key to the internal key that the storage system uses.
-
#set_all(values) ⇒ Object
Public: Sets the given values.
Class Method Details
.included(klass) ⇒ Object
7 8 9 |
# File 'lib/horcrux.rb', line 7 def self.included(klass) klass.send :attr_reader, :client, :serializer end |
Instance Method Details
#delete_all(*keys) ⇒ Object
Public: Deletes the given keys.
@adapter.delete_all 'a', 'b'
# => [true, false]
keys - One or more String keys.
Returns an Array of Booleans specifying whether the deletes were successful.
61 62 63 |
# File 'lib/horcrux.rb', line 61 def delete_all(*keys) keys.map { |k| delete(k) } end |
#fetch(key) ⇒ Object
Public: Either gets the value of the key, or sets it if it doesn’t exist.
# if 'some-cache' is not set, call #slow_method
@adapter.fetch('some-cache') { slow_method }
key - A String key.
Yields if the key does not exist. The key is set to the return value of the block. Returns the Object value.
84 85 86 87 88 89 90 |
# File 'lib/horcrux.rb', line 84 def fetch(key) get(key) || begin value = yield set(key, value) value end end |
#get_all(*keys) ⇒ Object
Public: Gets all the values for the given keys.
@adapter.get_all('a', 'b')
# => ['1', '2']
keys - One or more String keys.
Returns an Array of unpacked values in the order of their associated keys.
32 33 34 |
# File 'lib/horcrux.rb', line 32 def get_all(*keys) keys.map { |k| get(k) } end |
#initialize(client, serializer = nil) ⇒ Object
Public: Sets up an adapter with the client.
client - This is the object that the adapter uses to store and
retrieve data.
serializer - An object that responds to #pack and #unpack for
serializing and deserializing values. Default: a
StringSerializer.
18 19 20 21 |
# File 'lib/horcrux.rb', line 18 def initialize(client, serializer = nil) @client = client @serializer = serializer || StringSerializer end |
#key?(key) ⇒ Boolean
Public: Determines if the key is set.
key - A String key.
Returns true if the key is set, or false.
70 71 72 |
# File 'lib/horcrux.rb', line 70 def key?(key) !get(key).nil? end |
#key_for(key) ⇒ Object
Public: Transforms the given application key to the internal key that the storage system uses.
key - The String key.
Returns the String internal key for the adapter.
98 99 100 |
# File 'lib/horcrux.rb', line 98 def key_for(key) key.to_s end |
#set_all(values) ⇒ Object
Public: Sets the given values.
@adapter.set_all 'a' => '1', 'b' => '2'
# => ['a', 'b']
values - A Hash of String keys and Object values.
Returns an Array of the successfully written String keys.
44 45 46 47 48 49 50 |
# File 'lib/horcrux.rb', line 44 def set_all(values) good_keys = [] values.each do |key, value| good_keys << key if set(key, value) end good_keys end |