Permalink
Browse files

Centralized Redis key creation

  • Loading branch information...
1 parent bfccd82 commit 4169f0c355a2c1cec3a4483f4e57532efdeccd97 @cmer committed Jul 10, 2012
@@ -1,6 +1,44 @@
module Socialization
module RedisStores
class Base
+
+ class << self
+ protected
+ def generate_actor_key(actor, victim, type)
+ keys = key_type_to_class_names(type)
+ raise ArgumentError.new("`#{keys[1]}` needs to be an acts_as_#{keys[1]} objects, not a class.") if victim.class == Class
+ unless actor.nil?
+ "#{keys[0].pluralize.capitalize}:#{victim.class}:#{victim.id}:#{actor.class == Class ? actor : actor.class}"
+ else
+ "#{keys[0].pluralize.capitalize}:#{victim.class}:#{victim.id}"
+ end
+ end
+
+ def generate_victim_key(actor, victim, type)
+ keys = key_type_to_class_names(type)
+ raise ArgumentError.new("`#{keys[0]}` needs to be an acts_as_#{keys[0]} objects, not a class.") if actor.class == Class
+ unless victim.nil?
+ "#{keys[1].pluralize.capitalize}:#{actor.class}:#{actor.id}:#{victim.class == Class ? victim : victim.class}"
+ else
+ "#{keys[1].pluralize.capitalize}:#{actor.class}:#{actor.id}"
+ end
+ end
+
+ private
+ def key_type_to_class_names(type)
+ case type
+ when :follow
+ ['follower', 'followable']
+ when :like
+ ['liker', 'likeable']
+ when :mention
+ ['mentioner', 'mentionable']
+ else
+ raise NotImplementedError
+ end
+ end
+ end
+
end
end
end
@@ -72,25 +72,11 @@ def followables(follower, klass, opts = {})
private
def generate_followers_key(follower, followable)
- raise ArgumentError.new("`followable` needs to be an acts_as_followable objecs, not a class.") if followable.class == Class
- follower_class = if follower.class == Class
- follower
- else
- follower.class
- end
-
- "Followers:#{followable.class}:#{followable.id}:#{follower_class}"
+ generate_actor_key(follower, followable, :follow)
end
def generate_followables_key(follower, followable)
- raise ArgumentError.new("`follower` needs to be an acts_as_follower object, not a class.") if follower.class == Class
- followable_class = if followable.class == Class
- followable
- else
- followable.class
- end
-
- "Followables:#{follower.class}:#{follower.id}:#{followable_class}"
+ generate_victim_key(follower, followable, :follow)
end
end # class << self
@@ -103,25 +103,11 @@ def call_after_destroy_hooks(liker, likeable)
end
def generate_likers_key(liker, likeable)
- raise ArgumentError.new("`likeable` needs to be an acts_as_likeable objecs, not a class.") if likeable.class == Class
- liker_class = if liker.class == Class
- liker
- else
- liker.class
- end
-
- "Likers:#{likeable.class}:#{likeable.id}:#{liker_class}"
+ generate_actor_key(liker, likeable, :like)
end
def generate_likeables_key(liker, likeable)
- raise ArgumentError.new("`liker` needs to be an acts_as_liker object, not a class.") if liker.class == Class
- likeable_class = if likeable.class == Class
- likeable
- else
- likeable.class
- end
-
- "Likeables:#{liker.class}:#{liker.id}:#{likeable_class}"
+ generate_victim_key(liker, likeable, :like)
end
end # class << self
@@ -72,25 +72,11 @@ def mentionables(mentioner, klass, opts = {})
private
def generate_mentioners_key(mentioner, mentionable)
- raise ArgumentError.new("`mentionable` needs to be an acts_as_mentionable objecs, not a class.") if mentionable.class == Class
- mentioner_class = if mentioner.class == Class
- mentioner
- else
- mentioner.class
- end
-
- "Mentioners:#{mentionable.class}:#{mentionable.id}:#{mentioner_class}"
+ generate_actor_key(mentioner, mentionable, :mention)
end
def generate_mentionables_key(mentioner, mentionable)
- raise ArgumentError.new("`mentioner` needs to be an acts_as_mentioner object, not a class.") if mentioner.class == Class
- mentionable_class = if mentionable.class == Class
- mentionable
- else
- mentionable.class
- end
-
- "Mentionables:#{mentioner.class}:#{mentioner.id}:#{mentionable_class}"
+ generate_victim_key(mentioner, mentionable, :mention)
end
end # class << self
@@ -21,8 +21,8 @@ class RedisFollowStoreTest < Test::Unit::TestCase
context "#follow!" do
should "create a Follow record" do
@klass.follow!(@follower, @followable)
- assert_equal ["#{@follower.id}"], Socialization.redis.smembers(followers_key)
- assert_equal ["#{@followable.id}"], Socialization.redis.smembers(followables_key)
+ assert_equal ["#{@follower.id}"], Socialization.redis.smembers(followers_key(@follower, @followable))
+ assert_equal ["#{@followable.id}"], Socialization.redis.smembers(followables_key(@follower, @followable))
end
should "touch follower when instructed" do
@@ -61,8 +61,8 @@ class RedisFollowStoreTest < Test::Unit::TestCase
context "#follows?" do
should "return true when follow exists" do
- Socialization.redis.sadd followers_key, @follower.id
- Socialization.redis.sadd followables_key, @followable.id
+ Socialization.redis.sadd followers_key(@follower, @followable), @follower.id
+ Socialization.redis.sadd followables_key(@follower, @followable), @followable.id
assert_true @klass.follows?(@follower, @followable)
end
@@ -116,6 +116,10 @@ class RedisFollowStoreTest < Test::Unit::TestCase
should "return valid key when follower is a class" do
assert_equal "Followers:ImAFollowable:#{@followable.id}:ImAFollower", followers_key(@follower.class, @followable)
end
+
+ should "return valid key when follower is nil" do
+ assert_equal "Followers:ImAFollowable:#{@followable.id}", followers_key(nil, @followable)
+ end
end
context "#generate_followables_key" do
@@ -126,8 +130,11 @@ class RedisFollowStoreTest < Test::Unit::TestCase
should "return valid key when followable is a class" do
assert_equal "Followables:ImAFollower:#{@follower.id}:ImAFollowable", followables_key(@follower, @followable.class)
end
- end
+ should "return valid key when followable is nil" do
+ assert_equal "Followables:ImAFollower:#{@follower.id}", followables_key(@follower, nil)
+ end
+ end
end
# Helpers
@@ -140,14 +147,10 @@ def assert_match_followable(follow_record, followable)
end
def followers_key(follower = nil, followable = nil)
- follower ||= @follower
- followable ||= @followable
@klass.send(:generate_followers_key, follower, followable)
end
def followables_key(follower = nil, followable = nil)
- follower ||= @follower
- followable ||= @followable
@klass.send(:generate_followables_key, follower, followable)
end
end
@@ -21,8 +21,8 @@ class RedisLikeStoreTest < Test::Unit::TestCase
context "#like!" do
should "create a Like record" do
@klass.like!(@liker, @likeable)
- assert_equal ["#{@liker.id}"], Socialization.redis.smembers(likers_key)
- assert_equal ["#{@likeable.id}"], Socialization.redis.smembers(likeables_key)
+ assert_equal ["#{@liker.id}"], Socialization.redis.smembers(likers_key(@liker, @likeable))
+ assert_equal ["#{@likeable.id}"], Socialization.redis.smembers(likeables_key(@liker, @likeable))
end
should "touch liker when instructed" do
@@ -61,8 +61,8 @@ class RedisLikeStoreTest < Test::Unit::TestCase
context "#likes?" do
should "return true when like exists" do
- Socialization.redis.sadd likers_key, @liker.id
- Socialization.redis.sadd likeables_key, @likeable.id
+ Socialization.redis.sadd likers_key(@liker, @likeable), @liker.id
+ Socialization.redis.sadd likeables_key(@liker, @likeable), @likeable.id
assert_true @klass.likes?(@liker, @likeable)
end
@@ -116,6 +116,10 @@ class RedisLikeStoreTest < Test::Unit::TestCase
should "return valid key when liker is a class" do
assert_equal "Likers:ImALikeable:#{@likeable.id}:ImALiker", likers_key(@liker.class, @likeable)
end
+
+ should "return valid key when liker is nil" do
+ assert_equal "Likers:ImALikeable:#{@likeable.id}", likers_key(nil, @likeable)
+ end
end
context "#generate_likeables_key" do
@@ -126,6 +130,10 @@ class RedisLikeStoreTest < Test::Unit::TestCase
should "return valid key when likeable is a class" do
assert_equal "Likeables:ImALiker:#{@liker.id}:ImALikeable", likeables_key(@liker, @likeable.class)
end
+
+ should "return valid key when likeable is nil" do
+ assert_equal "Likeables:ImALiker:#{@liker.id}", likeables_key(@liker, nil)
+ end
end
end
@@ -140,14 +148,10 @@ def assert_match_likeable(like_record, likeable)
end
def likers_key(liker = nil, likeable = nil)
- liker ||= @liker
- likeable ||= @likeable
@klass.send(:generate_likers_key, liker, likeable)
end
def likeables_key(liker = nil, likeable = nil)
- liker ||= @liker
- likeable ||= @likeable
@klass.send(:generate_likeables_key, liker, likeable)
end
end
@@ -21,8 +21,8 @@ class RedisMentionStoreTest < Test::Unit::TestCase
context "#mention!" do
should "create a Mention record" do
@klass.mention!(@mentioner, @mentionable)
- assert_equal ["#{@mentioner.id}"], Socialization.redis.smembers(mentioners_key)
- assert_equal ["#{@mentionable.id}"], Socialization.redis.smembers(mentionables_key)
+ assert_equal ["#{@mentioner.id}"], Socialization.redis.smembers(mentioners_key(@mentioner, @mentionable))
+ assert_equal ["#{@mentionable.id}"], Socialization.redis.smembers(mentionables_key(@mentioner, @mentionable))
end
should "touch mentioner when instructed" do
@@ -61,8 +61,8 @@ class RedisMentionStoreTest < Test::Unit::TestCase
context "#mentions?" do
should "return true when mention exists" do
- Socialization.redis.sadd mentioners_key, @mentioner.id
- Socialization.redis.sadd mentionables_key, @mentionable.id
+ Socialization.redis.sadd mentioners_key(@mentioner, @mentionable), @mentioner.id
+ Socialization.redis.sadd mentionables_key(@mentioner, @mentionable), @mentionable.id
assert_true @klass.mentions?(@mentioner, @mentionable)
end
@@ -116,6 +116,10 @@ class RedisMentionStoreTest < Test::Unit::TestCase
should "return valid key when mentioner is a class" do
assert_equal "Mentioners:ImAMentionable:#{@mentionable.id}:ImAMentioner", mentioners_key(@mentioner.class, @mentionable)
end
+
+ should "return valid key when mentioner is nil" do
+ assert_equal "Mentioners:ImAMentionable:#{@mentionable.id}", mentioners_key(nil, @mentionable)
+ end
end
context "#generate_mentionables_key" do
@@ -126,6 +130,10 @@ class RedisMentionStoreTest < Test::Unit::TestCase
should "return valid key when mentionable is a class" do
assert_equal "Mentionables:ImAMentioner:#{@mentioner.id}:ImAMentionable", mentionables_key(@mentioner, @mentionable.class)
end
+
+ should "return valid key when mentionable is nil" do
+ assert_equal "Mentionables:ImAMentioner:#{@mentioner.id}", mentionables_key(@mentioner, nil)
+ end
end
end
@@ -140,14 +148,10 @@ def assert_match_mentionable(mention_record, mentionable)
end
def mentioners_key(mentioner = nil, mentionable = nil)
- mentioner ||= @mentioner
- mentionable ||= @mentionable
@klass.send(:generate_mentioners_key, mentioner, mentionable)
end
def mentionables_key(mentioner = nil, mentionable = nil)
- mentioner ||= @mentioner
- mentionable ||= @mentionable
@klass.send(:generate_mentionables_key, mentioner, mentionable)
end
end

0 comments on commit 4169f0c

Please sign in to comment.