Class: SuperModel::Redis
- Inherits:
-
Base
- Object
- Base
- SuperModel::Redis
show all
- Defined in:
- lib/supermodel/redis.rb
Instance Attribute Summary
Attributes inherited from Base
#attributes
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#==, attributes, #clone, create, destroy, destroy_all, #dup, #eql?, #exists?, #has_attribute?, #hash, #id, #id=, #initialize, #known_attributes, known_attributes, #load, method_missing, #new?, raw_find, records, #respond_to?, #respond_to_without_attributes?, #save, #save!, update, #update_attribute, #update_attributes
#save_with_validation
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class SuperModel::Base
Class Method Details
.all ⇒ Object
51
52
53
|
# File 'lib/supermodel/redis.rb', line 51
def all
from_ids(redis.set_members(redis_key))
end
|
.count ⇒ Object
47
48
49
|
# File 'lib/supermodel/redis.rb', line 47
def count
redis.set_count(redis_key)
end
|
.delete_all ⇒ Object
55
56
57
|
# File 'lib/supermodel/redis.rb', line 55
def delete_all
raise "Not implemented"
end
|
.find(id) ⇒ Object
29
30
31
32
33
34
35
36
37
|
# File 'lib/supermodel/redis.rb', line 29
def find(id)
if redis.set_member?(redis_key, id)
res = self.new(:id => id)
res.redis_get
res
else
raise(UnknownRecord)
end
end
|
.find_by_attribute(key, value) ⇒ Object
59
60
61
62
63
64
65
|
# File 'lib/supermodel/redis.rb', line 59
def find_by_attribute(key, value)
item_ids = redis.set_members(redis_key(key, value))
return if item_ids.empty?
res = self.new(:id => item_ids.first)
res.redis_get
res
end
|
.first ⇒ Object
39
40
41
|
# File 'lib/supermodel/redis.rb', line 39
def first
all.first
end
|
.indexes(*indexes) ⇒ Object
8
9
10
|
# File 'lib/supermodel/redis.rb', line 8
def indexes(*indexes)
@known_indexes = indexes.map(&:to_s)
end
|
.known_indexes ⇒ Object
12
13
14
|
# File 'lib/supermodel/redis.rb', line 12
def known_indexes
@known_indexes ||= []
end
|
.last ⇒ Object
43
44
45
|
# File 'lib/supermodel/redis.rb', line 43
def last
all.last
end
|
.redis ⇒ Object
4
5
6
|
# File 'lib/supermodel/redis.rb', line 4
def redis
@redis ||= ::Redis.new
end
|
.redis_key(*args) ⇒ Object
24
25
26
27
|
# File 'lib/supermodel/redis.rb', line 24
def redis_key(*args)
args.unshift(self.name.downcase)
args.join(":")
end
|
.serialize(*attributes) ⇒ Object
16
17
18
|
# File 'lib/supermodel/redis.rb', line 16
def serialize(*attributes)
@serialized_attributes = attributes.map(&:to_s)
end
|
.serialized_attributes ⇒ Object
20
21
22
|
# File 'lib/supermodel/redis.rb', line 20
def serialized_attributes
@serialized_attributes ||= []
end
|
Instance Method Details
#destroy ⇒ Object
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/supermodel/redis.rb', line 77
def destroy
return if new?
destroy_indexes
redis.set_delete(self.class.redis_key, self.id)
attributes.keys.each do |key|
redis.delete(redis_key(key))
end
end
|
#redis_get ⇒ Object
139
140
141
142
143
144
|
# File 'lib/supermodel/redis.rb', line 139
def redis_get
known_attributes.each do |key|
result = deserialize_attribute(key, redis.get(redis_key(key)))
send("#{key}=", result)
end
end
|