module TopModel::Redis::ClassMethods

Public Class Methods

extended(base) click to toggle source
# File lib/topmodel/redis.rb, line 4
def self.extended(base)
  base.class_eval do
    #class_inheritable_array :indexed_attributes
    class_attribute :indexed_attributes
    self.indexed_attributes = []
              
    #class_inheritable_hash :redis_options
    class_attribute :redis_options
    self.redis_options = {}
  end
end

Public Instance Methods

all() click to toggle source
# File lib/topmodel/redis.rb, line 65
def all
  from_ids(redis.sort(redis_key))
end
count() click to toggle source
# File lib/topmodel/redis.rb, line 61
def count
  redis.scard(redis_key)
end
delete_all() click to toggle source
# File lib/topmodel/redis.rb, line 73
def delete_all
  raise "Not implemented"
end
exists?(id) click to toggle source
# File lib/topmodel/redis.rb, line 57
def exists?(id)
  redis.sismember(redis_key, id.to_s)
end
find(id) click to toggle source
# File lib/topmodel/redis.rb, line 37
def find(id)
  if redis.sismember(redis_key, id.to_s)
    existing(:id => id)
  else
    raise UnknownRecord, "Couldn't find #{self.name} with ID=#{id}"
  end
end
find_all_by_attribute(key, value) click to toggle source
# File lib/topmodel/redis.rb, line 83
def find_all_by_attribute(key, value)
  from_ids(redis.sort(redis_key(key, value.to_s)))
end
find_by_attribute(key, value) click to toggle source
# File lib/topmodel/redis.rb, line 77
def find_by_attribute(key, value)
  item_ids = redis.sort(redis_key(key, value.to_s))
  item_id  = item_ids.first
  item_id && existing(:id => item_id)
end
first() click to toggle source
# File lib/topmodel/redis.rb, line 45
def first
  item_ids = redis.sort(redis_key, :order => "ASC", :limit => [0, 1])
  item_id  = item_ids.first
  item_id && existing(:id => item_id)        
end
indexes(*indexes) click to toggle source
# File lib/topmodel/redis.rb, line 28
def indexes(*indexes)
  self.indexed_attributes += indexes.map(&:to_s)
end
last() click to toggle source
# File lib/topmodel/redis.rb, line 51
def last
  item_ids = redis.sort(redis_key, :order => "DESC", :limit => [0, 1])
  item_id  = item_ids.first
  item_id && existing(:id => item_id)        
end
namespace() click to toggle source
# File lib/topmodel/redis.rb, line 16
def namespace
  @namespace ||= self.name.downcase
end
namespace=(namespace) click to toggle source
# File lib/topmodel/redis.rb, line 20
def namespace=(namespace)
  @namespace = namespace
end
redis() click to toggle source
# File lib/topmodel/redis.rb, line 24
def redis
  @redis ||= ::Redis.new(redis_options)
end
redis_key(*args) click to toggle source
# File lib/topmodel/redis.rb, line 32
def redis_key(*args)
  args.unshift(self.namespace)
  args.join(":")
end
select() click to toggle source
# File lib/topmodel/redis.rb, line 69
def select
  raise "Not implemented"
end

Protected Instance Methods

existing(atts = {}) click to toggle source
# File lib/topmodel/redis.rb, line 92
def existing(atts = {})
  item = self.new(atts)
  item.new_record = false
  item.redis_get
  item
end
from_ids(ids) click to toggle source
# File lib/topmodel/redis.rb, line 88
def from_ids(ids)
  ids.map {|id| existing(:id => id) }
end