Class: RedisModel
- Inherits:
-
Object
show all
- Includes:
- Comparable
- Defined in:
- lib/redis_model.rb
Overview
A basic Redis-backed Model implementation. Extend it by specifying your desired attr_accessors Enable timestamps by calling use_timestamps in your implementation Features auto-incremented ids and id namespacing Example:
class Zorro < Fondue::RedisModel
attr_accessor :sword, :mask
use_timestamps
end
z = Zorro.new(:sword => 'rapier', :mask => 'lame')
z.persist!
z
=>
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(params = {}) ⇒ RedisModel
Returns a new instance of RedisModel.
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/redis_model.rb', line 24
def initialize params = {}
params.map do |k,v|
begin
send(:"#{k}=",JSON.parse(v))
rescue JSON::ParserError
send(:"#{k}=",v)
end
end
self.id = self.id.to_i if self.id
if use_timestamps?
self.created_at = Time.parse(self.created_at) if self.created_at.is_a?(String)
self.updated_at = Time.parse(self.updated_at) if self.updated_at.is_a?(String)
end
end
|
Instance Attribute Details
#id ⇒ Object
the only default attribute
22
23
24
|
# File 'lib/redis_model.rb', line 22
def id
@id
end
|
Class Method Details
.all ⇒ Object
108
109
110
|
# File 'lib/redis_model.rb', line 108
def all
$redis.namespace(self)
end
|
.exists?(id) ⇒ Boolean
112
113
114
|
# File 'lib/redis_model.rb', line 112
def exists? id
$redis.exists(namespaced_id(id))
end
|
.expire_in(time) ⇒ Object
128
129
130
|
# File 'lib/redis_model.rb', line 128
def expire_in time
@expires_in = time
end
|
.load(id) ⇒ Object
Also known as:
find
116
117
118
119
|
# File 'lib/redis_model.rb', line 116
def load id
hash = $redis.hgetall(namespaced_id(id))
hash == {} ? nil : new(hash)
end
|
.namespaced_id(id) ⇒ Object
104
105
106
|
# File 'lib/redis_model.rb', line 104
def namespaced_id(id)
"#{self}::#{id}"
end
|
.use_timestamps ⇒ Object
122
123
124
125
126
|
# File 'lib/redis_model.rb', line 122
def use_timestamps
@use_timestamps = true
self.send(:attr_accessor, :created_at)
self.send(:attr_accessor, :updated_at)
end
|
Instance Method Details
#<=>(other) ⇒ Object
97
98
99
|
# File 'lib/redis_model.rb', line 97
def <=>(other)
self.to_hash <=> other.to_hash
end
|
#destroy! ⇒ Object
63
64
65
|
# File 'lib/redis_model.rb', line 63
def destroy!
$redis.del(namespaced_id)
end
|
#expires_in ⇒ Object
83
84
85
|
# File 'lib/redis_model.rb', line 83
def expires_in
self.class.instance_variable_get(:@expires_in)
end
|
#namespaced_id ⇒ Object
91
92
93
|
# File 'lib/redis_model.rb', line 91
def namespaced_id
self.class.namespaced_id(self.id)
end
|
#persist ⇒ Object
Also known as:
save
57
58
59
60
|
# File 'lib/redis_model.rb', line 57
def persist
persist!
self
end
|
#persist! ⇒ Object
Also known as:
save!
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/redis_model.rb', line 41
def persist!
is_new = id.nil? or !self.class.exists?(id)
if use_timestamps?
self.updated_at = Time.now
self.created_at = Time.now if is_new
end
self.id = generate_id if is_new
$redis.hmset(namespaced_id, *self.to_array)
$redis.expire(namespaced_id,expires_in) unless expires_in.nil?
return true
end
|
#reload ⇒ Object
67
68
69
|
# File 'lib/redis_model.rb', line 67
def reload
self.class.load(namespaced_id)
end
|
#to_array ⇒ Object
87
88
89
|
# File 'lib/redis_model.rb', line 87
def to_array
to_hash.to_a.flatten
end
|
#to_hash ⇒ Object
71
72
73
74
75
76
77
|
# File 'lib/redis_model.rb', line 71
def to_hash
instance_variables.inject({}) do |h,v|
value = instance_variable_get(v)
value = value.is_a?(String) ? value : value.to_json
h.update v.to_s.gsub('@','') => value
end
end
|
#use_timestamps? ⇒ Boolean
79
80
81
|
# File 'lib/redis_model.rb', line 79
def use_timestamps?
self.class.instance_variable_get(:@use_timestamps)
end
|