lib/seymour/feed.rb in seymour-0.0.7 vs lib/seymour/feed.rb in seymour-0.0.8
- old
+ new
@@ -2,100 +2,64 @@
class Feed
attr_accessor :owner
class << self
- @@feed_classes = []
+ def key(&block)
+ define_method('key_to_store', &block)
+ end
+
def distribute(activity)
activity.distribute
end
def inherited(subclass)
- @@feed_classes << subclass
+ feed_classes << subclass
end
def feed_classes
- @@feed_classes
+ @@feed_classes ||= []
end
- end
- def initialize(owner)
- @owner = owner
- end
+ def store(store_type)
+ @store_type = "seymour/store/#{store_type}".camelize.constantize
+ end
- def activity_ids
- redis.lrange(key, 0, max_size).map{|id| id.to_i }
- end
-
- def push(activity)
- perform_push(activity.id) if should_push?(activity)
- end
-
- def bulk_push(activities)
- activities.each do |activity|
- push(activity)
+ def store_type
+ @store_type ||= Seymour::Store::List
end
end
- def remove(activity)
- remove_id activity.id
+ def initialize(owner)
+ @owner = owner
end
- def remove_id(activity_id)
- redis.lrem(key, 0, activity_id)
- end
+ delegate :key, :key=, :push, :sort, :sort!, :insert_and_order, :bulk_push,
+ :remove, :remove_id, :remove_all, :ids, :activity_ids, :to => :store
- def sort!(options = {})
- sort({ :order => "DESC", :store => key }.merge(options)) # replaces itself with sorted list
- end
+ delegate :union, :intersect, :to => :store
- def sort(options = {})
- redis.sort(key, options)
+ def store
+ @store ||= self.class.store_type.new(key_to_store)
end
- def sorted_push(activities)
- bulk_push(activities)
- sort!
- end
- alias_method :insert_and_order, :sorted_push
-
protected
- def redis
- @redis ||= Seymour.redis
+ def key_to_store
+ "#{owner_name}:#{id_for_key}/#{feed_name}"
end
- def remove_all
- redis.del(key)
- end
-
def owner_name
owner.class.name
end
- def key
- "#{owner.class.name}:#{id_for_key}/#{feed_name}"
- end
-
def id_for_key
owner.id
end
def feed_name
self.class.name.downcase
end
- def max_size
- 100
- end
-
- def should_push?(activity)
- !activity_ids.include?(activity.id)
- end
-
- def perform_push(id)
- redis.lpush(key, id)
- redis.ltrim(key, 0, max_size)
- end
end
end