Module: SuperModel::Redis::ClassMethods

Defined in:
lib/supermodel/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
# File 'lib/supermodel/redis.rb', line 4

def self.extended(base)
  base.class_eval do
    class_inheritable_array :indexed_attributes
    self.indexed_attributes = []
              
    class_inheritable_hash :redis_options
    self.redis_options = {}
  end
end

Instance Method Details

#allObject



63
64
65
# File 'lib/supermodel/redis.rb', line 63

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

#countObject



59
60
61
# File 'lib/supermodel/redis.rb', line 59

def count
  redis.scard(redis_key)
end

#delete_allObject



71
72
73
# File 'lib/supermodel/redis.rb', line 71

def delete_all
  raise "Not implemented"
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/supermodel/redis.rb', line 55

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

#find(id) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/supermodel/redis.rb', line 35

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



81
82
83
# File 'lib/supermodel/redis.rb', line 81

def find_all_by_attribute(key, value)
  from_ids(redis.sort(redis_key(key, value.to_s)))
end

#find_by_attribute(key, value) ⇒ Object



75
76
77
78
79
# File 'lib/supermodel/redis.rb', line 75

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



43
44
45
46
47
# File 'lib/supermodel/redis.rb', line 43

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



26
27
28
# File 'lib/supermodel/redis.rb', line 26

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

#lastObject



49
50
51
52
53
# File 'lib/supermodel/redis.rb', line 49

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



14
15
16
# File 'lib/supermodel/redis.rb', line 14

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

#namespace=(namespace) ⇒ Object



18
19
20
# File 'lib/supermodel/redis.rb', line 18

def namespace=(namespace)
  @namespace = namespace
end

#redisObject



22
23
24
# File 'lib/supermodel/redis.rb', line 22

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

#redis_key(*args) ⇒ Object



30
31
32
33
# File 'lib/supermodel/redis.rb', line 30

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

#selectObject



67
68
69
# File 'lib/supermodel/redis.rb', line 67

def select
  raise "Not implemented"
end