Created
February 15, 2014 13:48
-
-
Save ahawkins/9019525 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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