Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

merging in refactoring branch, renaming to dm-redis

  • Loading branch information...
commit 5a4ef80ce51886210740004ef56370c8dfcabb48 2 parents ef9a973 + bbfb455
@whoahbot authored
View
2  Gemfile
@@ -8,6 +8,6 @@ gem "jeweler", ">= 1.4.0"
gem "rspec", "~> 1.3.0"
gem "rake", ">= 0.8.7"
gem "hiredis", "~> 0.3.0"
-#gem "ruby-debug19"
+gem "ruby-debug19"
# gem "data_objects", ">= 0.10.1"
# gem "do_postgres", ">= 0.10.1"
View
15 Gemfile.lock
@@ -2,7 +2,9 @@ GEM
remote: http://rubygems.org/
specs:
addressable (2.2.6)
+ archive-tar-minitar (0.5.2)
bcrypt-ruby (3.0.1)
+ columnize (0.3.6)
dm-core (1.2.0)
addressable (~> 2.2.6)
dm-types (1.2.0)
@@ -25,10 +27,22 @@ GEM
rubyforge (>= 2.0.0)
json (1.5.4)
json_pure (1.4.6)
+ linecache19 (0.5.12)
+ ruby_core_source (>= 0.1.4)
multi_json (1.0.3)
rake (0.8.7)
redis (2.2.0)
rspec (1.3.1)
+ ruby-debug-base19 (0.11.25)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby_core_source (>= 0.1.4)
+ ruby-debug19 (0.11.6)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby-debug-base19 (>= 0.11.19)
+ ruby_core_source (0.1.5)
+ archive-tar-minitar (>= 0.5.2)
rubyforge (2.0.4)
json_pure (>= 1.1.7)
stringex (1.3.0)
@@ -46,3 +60,4 @@ DEPENDENCIES
rake (>= 0.8.7)
redis (>= 2.2)
rspec (~> 1.3.0)
+ ruby-debug19
View
6 README.textile
@@ -1,4 +1,4 @@
-h1. dm-redis-adapter
+h1. dm-redis
This is a <a href="http://datamapper.org">DataMapper</a> adapter for the <a href="http://redis.io/">Redis</a> key-value store.
@@ -26,7 +26,7 @@ Prerequisites:
Install the dm-redis adapter:
<pre>
<code>
- > gem install dm-redis-adapter
+ > gem install dm-redis
</code>
</pre>
@@ -38,7 +38,7 @@ Setup your adapter, define your models and properties:
<code>
require 'rubygems'
require 'dm-core'
- require 'dm-redis-adapter'
+ require 'dm-redis'
DataMapper.setup(:default, {:adapter => "redis"})
View
6 Rakefile
@@ -10,11 +10,11 @@ end
require 'spec/rake/spectask'
-GEM = 'dm-redis-adapter'
-GEM_NAME = 'dm-redis-adapter'
+GEM = 'dm-redis'
+GEM_NAME = 'dm-redis'
AUTHORS = ['Dan Herrera']
EMAIL = "whoahbot@gmail.com"
-HOMEPAGE = "http://github.com/whoahbot/dm-redis-adapter"
+HOMEPAGE = "http://github.com/whoahbot/dm-redis"
SUMMARY = "DataMapper adapter for the Redis key-value database"
begin
View
2  VERSION
@@ -1 +1 @@
-0.5.3
+0.6.0
View
14 dm-redis-adapter.gemspec → dm-redis.gemspec
@@ -4,8 +4,8 @@
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
- s.name = %q{dm-redis-adapter}
- s.version = "0.5.3"
+ s.name = %q{dm-redis}
+ s.version = "0.6.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Dan Herrera"]
@@ -19,18 +19,19 @@ Gem::Specification.new do |s|
"MIT-LICENSE",
"README.textile",
"Rakefile",
- "lib/dm-redis-adapter.rb",
- "lib/dm-redis-adapter/adapter.rb",
- "lib/dm-redis-adapter/spec/setup.rb",
+ "lib/dm-redis.rb",
+ "lib/dm-redis/adapter.rb",
+ "lib/dm-redis/spec/setup.rb",
"spec/dm_redis_associations_spec.rb",
"spec/dm_redis_finding_spec.rb",
"spec/dm_redis_inheritenance_spec.rb",
"spec/dm_redis_spec.rb",
"spec/dm_redis_validations_spec.rb",
+ "spec/self_referential_spec.rb",
"spec/spec_helper.rb",
"spec/textual_keys_spec.rb"
]
- s.homepage = %q{http://github.com/whoahbot/dm-redis-adapter}
+ s.homepage = %q{http://github.com/whoahbot/dm-redis}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.6.2}
@@ -41,6 +42,7 @@ Gem::Specification.new do |s|
"spec/dm_redis_inheritenance_spec.rb",
"spec/dm_redis_spec.rb",
"spec/dm_redis_validations_spec.rb",
+ "spec/self_referential_spec.rb",
"spec/spec_helper.rb",
"spec/textual_keys_spec.rb"
]
View
1  lib/dm-redis-adapter.rb
@@ -1 +0,0 @@
-require 'dm-redis-adapter/adapter'
View
1  lib/dm-redis.rb
@@ -0,0 +1 @@
+require 'dm-redis/adapter'
View
117 lib/dm-redis-adapter/adapter.rb → lib/dm-redis/adapter.rb
@@ -1,7 +1,6 @@
require 'redis/connection/hiredis'
require 'redis'
require 'base64'
-require 'dm-core'
module DataMapper
module Adapters
@@ -92,12 +91,11 @@ def update(attributes, collection)
#
# @api semipublic
def delete(collection)
- storage_name = collection.query.model.storage_name
collection.each do |record|
- @redis.del("#{storage_name}:#{record[redis_key_for(collection.query.model)]}")
+ @redis.del("#{collection.query.model.to_s.downcase}:#{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("#{storage_name}:#{p.name}:#{encode(record[p.name])}", record[redis_key_for(collection.query.model)])
+ @redis.srem("#{collection.query.model.to_s.downcase}:#{p.name}:#{encode(record[p.name])}", record[redis_key_for(collection.query.model)])
end
end
end
@@ -114,6 +112,7 @@ def delete(collection)
def update_attributes(resources)
storage_name = resources.first.model.storage_name
resources.each do |resource|
+
model = resource.model
attributes = resource.dirty_attributes
@@ -124,6 +123,7 @@ def update_attributes(resources)
properties_to_set = []
properties_to_del = []
+
fields = model.properties(self.name).select {|property| attributes.key?(property)}
fields.each do |property|
value = attributes[property]
@@ -171,29 +171,14 @@ def records_for(query)
keys
end
- ##
- # Find records that match have a matching value
- #
- # @param [DataMapper::Query] query
- # The query used to locate the resources to be deleted.
- #
- # @param [DataMapper::Operation] the operation for the query
- #
- # @api private
- def perform_query(query, operand)
- storage_name = query.model.storage_name
-
- matched_records = []
- #p 'query: ' + query.inspect
- #p 'operand: ' + operand.inspect
-
+ def find_subject_and_value(query, operand)
if operand.is_a?(DataMapper::Query::Conditions::NotOperation)
subject = operand.first.subject
value = operand.first.value
elsif operand.subject.is_a?(DataMapper::Associations::ManyToOne::Relationship)
subject = operand.subject.child_key.first
value = if operand.is_a?(DataMapper::Query::Conditions::InclusionComparison)
- operand.value.map{|v|v[operand.subject.parent_key.first.name]}
+ operand.value.map{|v| v[operand.subject.parent_key.first.name]}
else
operand.value[operand.subject.parent_key.first.name]
end
@@ -206,46 +191,76 @@ def perform_query(query, operand)
subject = subject.child_key.first
end
- #p 'subject: ' + subject.inspect
- #p 'value: ' + value.inspect
+ return subject, value
+ end
+
+ ##
+ # Find records that match have a matching value
+ #
+ # @param [DataMapper::Query] query
+ # The query used to locate the resources to be deleted.
+ #
+ # @param [DataMapper::Operation] the operation for the query
+ #
+ # @api private
+ def perform_query(query, operand)
+ storage_name = query.model.storage_name
+ matched_records = []
+ subject, value = find_subject_and_value(query, operand)
- if query.model.key.include?(subject)
- if operand.is_a?(DataMapper::Query::Conditions::NotOperation)
- @redis.smembers(key_set_for(query.model)).each do |key|
- if operand.matches?(subject.typecast(key))
- matched_records << {redis_key_for(query.model) => key}
+ case operand
+ when DataMapper::Query::Conditions::NotOperation
+ if query.model.key.include?(subject)
+ @redis.smembers(key_set_for(query.model)).each do |key|
+ if operand.matches?(subject.typecast(key))
+ matched_records << {redis_key_for(query.model) => key}
+ end
end
+ else
+ search_all_resources(query, operand, subject, matched_records)
end
- elsif operand.is_a?(DataMapper::Query::Conditions::InclusionComparison)
- value.each do |val|
- if @redis.sismember(key_set_for(query.model), val)
- matched_records << {redis_key_for(query.model) => val}
+ when DataMapper::Query::Conditions::InclusionComparison
+ if query.model.key.include?(subject)
+ value.each do |val|
+ if @redis.sismember(key_set_for(query.model), val)
+ matched_records << {redis_key_for(query.model) => val}
+ end
end
- end
- elsif @redis.sismember(key_set_for(query.model), value)
- matched_records << {redis_key_for(query.model) => value}
- end
- elsif subject.respond_to?( :index ) && subject.index
- if operand.is_a?(DataMapper::Query::Conditions::NotOperation)
- elsif operand.is_a?(DataMapper::Query::Conditions::InclusionComparison)
- value.each do |val|
- find_indexed_matches(subject, val).each do |k|
- matched_records << {redis_key_for(query.model) => k, "#{subject.name}" => val}
+ elsif subject.respond_to?(:index) && subject.index
+ value.each do |val|
+ find_indexed_matches(subject, val).each do |k|
+ matched_records << {redis_key_for(query.model) => k, "#{subject.name}" => val}
+ end
end
+ else
+ search_all_resources(query, operand, subject, matched_records)
end
- else
- find_indexed_matches(subject, value).each do |k|
- matched_records << {redis_key_for(query.model) => k, "#{subject.name}" => value}
+ when DataMapper::Query::Conditions::EqualToComparison
+ if query.model.key.include?(subject)
+ if @redis.sismember(key_set_for(query.model), value)
+ matched_records << {redis_key_for(query.model) => value}
+ end
+ elsif subject.respond_to?(:index) && subject.index
+ find_indexed_matches(subject, value).each do |k|
+ matched_records << {redis_key_for(query.model) => k, "#{subject.name}" => value}
+ end
end
+ else # worst case here, loop through all members, typecast and match
+ search_all_resources(query, operand, subject, matched_records)
end
- else # worst case, loop through each record and match
- @redis.smembers(key_set_for(query.model)).each do |key|
- if operand.matches?(subject.typecast(@redis.hget("#{storage_name}:#{key}", subject.name)))
- matched_records << {redis_key_for(query.model) => key}
- end
+ matched_records
+ end
+
+ ##
+ # Searches through each key :(
+ #
+ # @api private
+ def search_all_resources(query, operand, subject, matched_records)
+ @redis.smembers(key_set_for(query.model)).each do |key|
+ if operand.matches?(subject.typecast(@redis.hget("#{subject.model.storage_name}:#{key}", subject.name)))
+ matched_records << {redis_key_for(query.model) => key}
end
end
- matched_records
end
##
View
2  lib/dm-redis-adapter/spec/setup.rb → lib/dm-redis/spec/setup.rb
@@ -1,4 +1,4 @@
-require 'dm-redis-adapter'
+require 'dm-redis'
require 'dm-core/spec/setup'
module DataMapper
View
9 spec/dm_redis_finding_spec.rb
@@ -38,11 +38,10 @@ class Page
found.should == [@b,@c,@d]
end
- # it fails
- #it 'should find Page.all :parent_id.not => @a.id' do
- # found = Page.all :parent_id.not => @a.id
- # found.should == [@a,@d]
- #end
+ it 'should find Page.all :parent_id.not => @a.id' do
+ found = Page.all :parent_id.not => @a.id
+ found.should == [@a,@d]
+ end
it 'should find Page.all :parent => @a' do
found = Page.all :parent => @a
View
50 spec/self_referential_spec.rb
@@ -0,0 +1,50 @@
+require 'spec_helper'
+
+describe DataMapper::Adapters::RedisAdapter do
+ before(:all) do
+ @adapter = DataMapper.setup(:default, {
+ :adapter => "redis",
+ :db => 15
+ })
+ end
+
+ after(:all) do
+ Redis.new(:db => 15).flushdb
+ end
+
+ describe "referential assosciations" do
+ it "should allow the assosciation to go both ways" do
+ class User
+ class Link
+ include DataMapper::Resource
+
+ # the person who is doing the following
+ belongs_to :follower, 'User', :key => true
+
+ # the person who is being followed
+ belongs_to :followed, 'User', :key => true
+ end
+
+ include DataMapper::Resource
+
+ property :id, Serial
+ property :full_name, String, :length => 255, :required => true
+ property :email, String, :length => 320, :required => true
+
+ has n, :links_to_followed_users, 'User::Link', :child_key => [:follower_id]
+ has n, :links_to_followers, 'User::Link', :child_key => [:followed_id]
+ has n, :followed_users, User, :through => :links_to_followed_users, :via => :followed
+ has n, :followers, User, :through => :links_to_followers, :via => :follower
+ end
+
+ DataMapper.finalize
+
+ user1 = User.create(:email => "joe@example.com", :full_name => "joe")
+ user2 = User.create(:email => "bob@example.com", :full_name => "bob")
+
+ user1.followed_users << user2
+ user1.save
+ #user2.links_to_followers.count.should == 1
+ end
+ end
+end
View
2  spec/spec_helper.rb
@@ -10,7 +10,7 @@
require 'dm-core'
require 'dm-core/spec/shared/adapter_spec'
-require 'dm-redis-adapter/spec/setup'
+require 'dm-redis/spec/setup'
ENV['ADAPTER'] = 'redis'
ENV['ADAPTER_SUPPORTS'] = 'all'
Please sign in to comment.
Something went wrong with that request. Please try again.