Class: DataMapper::Adapters::RedisAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/redis_adapter.rb

Instance Method Summary collapse

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.

Parameters:

  • resources (Enumerable(Resource))

    The set of resources (model instances)



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.

Parameters:

  • collection (DataMapper::Collection)

    The query used to locate the resources to be deleted.

Returns:

  • (Array)

    An Array of Hashes containing the key-value pairs for each record



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.

Parameters:

  • query (Query)

    The query to be used to seach for the resources

Returns:

  • (Array)

    An Array of Hashes containing the key-value pairs for each record



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.

Parameters:

  • attributes (Hash)

    A set of key-value pairs of the attributes to update the resources with.

  • collection (DataMapper::Collection)

    The query that should be used to find the resource(s) to update.



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