Class: DataMapper::Adapters::RedisAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- DataMapper::Adapters::RedisAdapter
- Defined in:
- lib/dm-redis-adapter/adapter.rb
Instance Method Summary collapse
-
#create(resources) ⇒ Object
Used by DataMapper to put records into the redis data-store: "INSERT" in SQL-speak.
-
#delete(collection) ⇒ Array
Destroys all the records matching the given query.
-
#read(query) ⇒ Array
Looks up one record or a collection of records from the data-store: "SELECT" in SQL.
-
#update(attributes, collection) ⇒ Object
Used by DataMapper to update the attributes on existing records in the redis data-store: "UPDATE" in SQL-speak.
Instance Method Details
#create(resources) ⇒ Object
Used by DataMapper to put records into the redis data-store: "INSERT" in SQL-speak. It takes an array of the resources (model instances) to be saved. Resources each have a key that can be used to quickly look them up later without searching.
18 19 20 21 22 23 24 25 |
# File 'lib/dm-redis-adapter/adapter.rb', line 18 def create(resources) storage_name = resources.first.model.storage_name resources.each do |resource| initialize_serial(resource, @redis.incr("#{storage_name}:#{redis_key_for(resource.model)}:serial")) @redis.sadd(key_set_for(resource.model), resource.key.join) end update_attributes(resources) end |
#delete(collection) ⇒ Array
Destroys all the records matching the given query. "DELETE" in SQL.
93 94 95 96 97 98 99 100 101 |
# File 'lib/dm-redis-adapter/adapter.rb', line 93 def delete(collection) collection.each do |record| @redis.del("#{collection.query.model.storage_name}:#{record[redis_key_for(collection.query.model)]}") @redis.srem(key_set_for(collection.query.model), record[redis_key_for(collection.query.model)]) record.model.properties.select {|p| p.index}.each do |p| @redis.srem("#{collection.query.model.storage_name}:#{p.name}:#{encode(record[p.name])}", record[redis_key_for(collection.query.model)]) end end end |
#read(query) ⇒ Array
Looks up one record or a collection of records from the data-store: "SELECT" in SQL.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dm-redis-adapter/adapter.rb', line 39 def read(query) storage_name = query.model.storage_name records = records_for(query) records.each do |record| record_data = @redis.hgetall("#{storage_name}:#{record[redis_key_for(query.model)]}") query.fields.each do |property| next if query.model.key.include?(property) and query.model.key.size == 1 name = property.name.to_s value = record_data[name] # Integers are stored as Strings in Redis. If there's a # string coming out that should be an integer, convert it # now. All other typecasting is handled by datamapper # separately. record[name] = [Integer, Date].include?(property.primitive) ? property.typecast( value ) : value record end end query.filter_records(records) end |
#update(attributes, collection) ⇒ Object
Used by DataMapper to update the attributes on existing records in the redis data-store: "UPDATE" in SQL-speak. It takes a hash of the attributes to update with, as well as a collection object that specifies which resources should be updated.
74 75 76 77 78 79 80 |
# File 'lib/dm-redis-adapter/adapter.rb', line 74 def update(attributes, collection) attributes = attributes_as_fields(attributes) records_to_update = records_for(collection.query) records_to_update.each {|r| r.update(attributes)} update_attributes(collection) end |