Class: Juvet::Adapters::RedisAdapter
- Inherits:
-
Object
- Object
- Juvet::Adapters::RedisAdapter
- Defined in:
- lib/juvet/adapters/redis_adapter.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
- #create(entity) ⇒ Object
- #destroy(id) ⇒ Object
- #find(id) ⇒ Object
-
#initialize(collection, options = {}) ⇒ RedisAdapter
constructor
A new instance of RedisAdapter.
- #persist(entity) ⇒ Object
- #update(entity) ⇒ Object
Constructor Details
#initialize(collection, options = {}) ⇒ RedisAdapter
Returns a new instance of RedisAdapter.
9 10 11 12 |
# File 'lib/juvet/adapters/redis_adapter.rb', line 9 def initialize(collection, ={}) @collection = collection @redis = Redis.new end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
7 8 9 |
# File 'lib/juvet/adapters/redis_adapter.rb', line 7 def collection @collection end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
7 8 9 |
# File 'lib/juvet/adapters/redis_adapter.rb', line 7 def redis @redis end |
Instance Method Details
#create(entity) ⇒ Object
14 15 16 |
# File 'lib/juvet/adapters/redis_adapter.rb', line 14 def create(entity) update_attributes entity end |
#destroy(id) ⇒ Object
18 19 20 |
# File 'lib/juvet/adapters/redis_adapter.rb', line 18 def destroy(id) redis.del collection_key(id) end |
#find(id) ⇒ Object
22 23 24 25 26 |
# File 'lib/juvet/adapters/redis_adapter.rb', line 22 def find(id) attributes = redis.get(collection_key(id)) return nil if attributes.nil? collection.entity.new ({ id: id }).merge(JSON.load(attributes)) end |
#persist(entity) ⇒ Object
28 29 30 |
# File 'lib/juvet/adapters/redis_adapter.rb', line 28 def persist(entity) find(entity.id).nil? ? create(entity) : update_attributes(entity) end |
#update(entity) ⇒ Object
32 33 34 35 |
# File 'lib/juvet/adapters/redis_adapter.rb', line 32 def update(entity) raise EntityNotFoundError if find(entity.id).nil? update_attributes entity end |