Class: DataMapper::Adapters::RedisAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- DataMapper::Adapters::RedisAdapter
- Defined in:
- lib/redis_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/redis_adapter.rb', line 18 def create(resources) resources.each do |resource| initialize_identity_field(resource, @redis.incr("#{resource.model}:#{redis_key_for(resource.model)}:serial")) @redis.set_add("#{resource.model}:#{redis_key_for(resource.model)}:all", resource.key) end update_attributes(resources) end |
#delete(collection) ⇒ Array
Destroys all the records matching the given query. “DELETE” in SQL.
81 82 83 84 85 86 87 88 |
# File 'lib/redis_adapter.rb', line 81 def delete(collection) collection.query.filter_records(records_for(collection.query)).each do |record| collection.query.model.properties.each do |p| @redis.delete("#{collection.query.model}:#{record[redis_key_for(collection.query.model)]}:#{p.name}") end @redis.set_delete("#{collection.query.model}:#{redis_key_for(collection.query.model)}:all", record[redis_key_for(collection.query.model)]) 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 |
# File 'lib/redis_adapter.rb', line 39 def read(query) records = records_for(query).each do |record| query.fields.each do |property| next if query.model.key.include?(property.name) record[property.name.to_s] = property.typecast(@redis["#{query.model}:#{record[redis_key_for(query.model)]}:#{property.name}"]) end end records = query.match_records(records) records = query.sort_records(records) records = query.limit_records(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.
65 66 67 68 |
# File 'lib/redis_adapter.rb', line 65 def update(attributes, collection) attributes = attributes_as_fields(attributes) read(collection.query).each { |r| r.update(attributes) } end |