Skip to content

Instantly share code, notes, and snippets.

@ahawkins
Created February 15, 2014 13:48
Show Gist options
  • Select an option

  • Save ahawkins/9019525 to your computer and use it in GitHub Desktop.

Select an option

Save ahawkins/9019525 to your computer and use it in GitHub Desktop.
class RedisMap
def initialize(redis)
@redis = redis
end
def clear
redis.del key
fail 'failed to delete' if redis.exists key
end
def all(klass)
read.all klass
end
def get(klass, id)
read.get klass, id
end
def set(record)
map = read
map.set record
write map
end
def delete(record)
map = read
map.delete record
write map
end
private
def key
'repo'
end
def redis
@redis
end
def read
value = redis.get key
value ? YAML.load(value) : RecordMap.new
end
def write(map)
redis.set key, YAML.dump(map)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment