Module: TopModel::Redis::ClassMethods

Defined in:
lib/topmodel/redis.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# 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

Instance Method Details

#allObject



65
66
67
# File 'lib/topmodel/redis.rb', line 65

def all
  from_ids(redis.sort(redis_key))
end

#countObject



61
62
63
# File 'lib/topmodel/redis.rb', line 61

def count
  redis.scard(redis_key)
end

#delete_allObject



73
74
75
# File 'lib/topmodel/redis.rb', line 73

def delete_all
  raise "Not implemented"
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/topmodel/redis.rb', line 57

def exists?(id)
  redis.sismember(redis_key, id.to_s)
end

#find(id) ⇒ Object



37
38
39
40
41
42
43
# 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) ⇒ Object



83
84
85
# 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) ⇒ Object



77
78
79
80
81
# 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

#firstObject



45
46
47
48
49
# 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) ⇒ Object



28
29
30
# File 'lib/topmodel/redis.rb', line 28

def indexes(*indexes)
  self.indexed_attributes += indexes.map(&:to_s)
end

#lastObject



51
52
53
54
55
# 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

#namespaceObject



16
17
18
# File 'lib/topmodel/redis.rb', line 16

def namespace
  @namespace ||= self.name.downcase
end

#namespace=(namespace) ⇒ Object



20
21
22
# File 'lib/topmodel/redis.rb', line 20

def namespace=(namespace)
  @namespace = namespace
end

#redisObject



24
25
26
# File 'lib/topmodel/redis.rb', line 24

def redis
  @redis ||= ::Redis.new(redis_options)
end

#redis_key(*args) ⇒ Object



32
33
34
35
# File 'lib/topmodel/redis.rb', line 32

def redis_key(*args)
  args.unshift(self.namespace)
  args.join(":")
end

#selectObject



69
70
71
# File 'lib/topmodel/redis.rb', line 69

def select
  raise "Not implemented"
end