Skip to content

Instantly share code, notes, and snippets.

@akdubya
Created April 17, 2009 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akdubya/97262 to your computer and use it in GitHub Desktop.
Save akdubya/97262 to your computer and use it in GitHub Desktop.
module Pastiche
class SpecStore
def initialize(store)
@store = store
end
class StoreError < RuntimeError; end
module Assertion
def assert(message, &block)
unless block.call
raise StoreError, message
end
end
end
include Assertion
## == Storage Features
def features
end
## == Key/Value API
# Retrieves a value for the given +key+.
def [](key)
assert('Store does not respond to #[]') { @store.respond_to?(:[]) }
@store[key]
end
# Stores the +value+ under the given +key+.
def []=(key, value)
assert('Store does not respond to #[]=') { @store.respond_to?(:[]=) }
@store[key] = value
end
# Returns +true+ if the given +key+ exists.
def key?(key)
assert('Store does not respond to #key?') { @store.respond_to?(:key?) }
response = @store.key?(key)
assert('Bad return value for #key? (must be true or false)') { [true, false].include?(response) }
response
end
# Deletes the given +key+ from storage.
def delete(key)
assert('Store does not respond to #delete') { @store.respond_to?(:delete) }
@store.delete(key)
end
def fetch(key, options={})
assert('Store does not respond to #fetch') { @store.respond_to?(:fetch) }
@store.fetch(key, options)
end
def fetch!(key, options={})
assert('Store does not respond to #fetch!') { @store.respond_to?(:fetch!) }
@store.fetch!(key, options)
end
def store(key, value, options={})
assert('Store does not respond to #store') { @store.respond_to?(:store) }
@store.store(key, value, options)
end
def clear
assert('Store does not respond to #clear') { @store.respond_to?(:clear) }
@store.clear
end
## == Enumeration API
def find(params={}, &block)
assert('Store does not respond to #find') { @store.respond_to?(:find) }
response = @store.find(params, &block)
if response
assert('Bad return value for #find (must be key/value or nil)') do
response.kind_of?(Array) && response.length == 2
end
end
response
end
def find_all(params={}, &block)
assert('Store does not respond to #find_all') { @store.respond_to?(:find_all) }
response = @store.find_all(params, &block)
assert('Bad return value for #find_all (must be Array)') { response.kind_of?(Array) }
if response.any?
assert('Bad return value for #find_all (must be key/value pairs)') do
response.first.kindof?(Array) && response.first.length == 2
end
end
response
end
alias_method :select, :find_all
def delete_if(params={}, &block)
assert('Store does not respond to #delete_if') { @store.respond_to?(:delete_if) }
response = @store.delete_if(params, &block)
assert('Bad return value for #delete_if (must be Array)') { response.kind_of?(Array) }
if response.any?
assert('Bad return value for #find_all (must be key/value pairs)') do
response.first.kindof?(Array) && response.first.length == 2
end
end
response
end
## == Extended API
def query(params={}, &block)
end
def mfetch(keys)
end
def mstore(keys, values)
end
def mdelete(keys)
end
def copy(source_key, dest_key, options={})
end
def page(params={}, &block)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment