Class: Horcrux::Memory

Inherits:
Object
  • Object
show all
Includes:
Methods
Defined in:
lib/horcrux.rb

Instance Method Summary collapse

Methods included from Methods

#delete_all, #fetch, #get_all, included, #key_for, #set_all

Constructor Details

#initialize(client = {}, serializer = NullSerializer) ⇒ Memory

Sample Horcrux adapter that stores unmodified values in a ruby Hash.

client - Optional Hash.



131
132
133
134
# File 'lib/horcrux.rb', line 131

def initialize(client = {}, serializer = NullSerializer)
  @client = client
  @serializer = serializer
end

Instance Method Details

#delete(key) ⇒ Object

Public: Deletes the value for the given key.

key - The String key.

Returns true if a value was deleted, or false.



170
171
172
# File 'lib/horcrux.rb', line 170

def delete(key)
  !client.delete(key_for(key)).nil?
end

#get(key) ⇒ Object

Public: Gets the value for the given key.

key - The String key.

Returns the Object value.



150
151
152
# File 'lib/horcrux.rb', line 150

def get(key)
  serializer.load client[key_for(key)]
end

#key?(key) ⇒ Boolean

Public: Uses Hash#key? to check the existence of a key.

key - String key.

Returns true if the key exists, or false.

Returns:

  • (Boolean)


141
142
143
# File 'lib/horcrux.rb', line 141

def key?(key)
  client.key? key_for(key)
end

#set(key, value) ⇒ Object

Public: Sets the value for the given key.

key - The String key. value - The Object value.

Returns true if the operation succeeded, or false.



160
161
162
163
# File 'lib/horcrux.rb', line 160

def set(key, value)
  client[key_for(key)] = serializer.dump(value)
  true
end