class Kayvee::Store

Represents a simple key value store. Provides multiple backing stores.

@example Basic Useage

store = Kayvee::Store.new(:s3, aws_access_key: '', ... )
key = store.set('hello', 'world')
key.read
  => 'world'

store = Kayvee::Store.new(:redis, host: 'redis://locahost')
key = store.set('hello', 'world')
key.read
  => 'world'

@see Kayvee::Clients::S3 @see Kayvee::Clients::Redis @see Kayvee::Clients::Memory @see Kayvee::Clients::Test

Constants

ClientNotFound

Raised when a client implementation can not be found.

Attributes

client[R]

The internal client driving the store

Public Class Methods

new(client = :s3, options = {}) click to toggle source

@param [Symbol] client the symbol representing the client to use. :s3, :redis, :memory, :test @param [Options] options the config for the client implementation

@see Kayvee::Clients::S3 @see Kayvee::Clients::Redis @see Kayvee::Clients::Memory @see Kayvee::Clients::Test

# File lib/kayvee/store.rb, line 33
def initialize(client = :s3, options = {})
  load_client(client)
  @client = instantiate_client(client, options)
end

Public Instance Methods

[](path)
Alias for: get
[]=(path, value)
Alias for: set
clear() click to toggle source

Clear the underlying store

# File lib/kayvee/store.rb, line 62
def clear
  @client.clear
end
get(path) click to toggle source

Gets a given keys value

@param [String] path the path to set

@return [Key] the value of the key

# File lib/kayvee/store.rb, line 56
def get(path)
  Kayvee::Key.new(@client, path)
end
Also aliased as: []
set(path, value) click to toggle source

Sets a given key to a given value

@param [String] path the path to set @param [String] value the value to set

@return [Key] the newly created or modified key object

# File lib/kayvee/store.rb, line 44
def set(path, value)
  key = Kayvee::Key.new(@client, path)
  key.write(value)
  key
end
Also aliased as: []=
size() click to toggle source
# File lib/kayvee/store.rb, line 66
def size
  @client.size
end

Private Instance Methods

instantiate_client(client, options) click to toggle source
# File lib/kayvee/store.rb, line 72
def instantiate_client(client, options)
  begin
    klass = "Kayvee::Clients::#{client.capitalize}".constantize
    klass.new(options)
  rescue NameError
    raise ClientNotFound.new
  end
end
load_client(client) click to toggle source
# File lib/kayvee/store.rb, line 81
def load_client(client)
  begin
    require "kayvee/clients/#{client}"
  rescue LoadError => load_error
    raise LoadError.new("Cannot find Kayvee::Clients::#{client.capitalize} adapter '#{client}' (#{load_error.message})")
  end
end