-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings-ohm.rb
74 lines (65 loc) · 1.19 KB
/
settings-ohm.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require "ohm"
class Setting < Ohm::Model
attribute :klass
attribute :data
# lookup
def self.[](sym, return_obj = false)
if sym.is_a? Symbol
obj = super(sym) || create(:id => sym)
if return_obj
obj
else
obj.data
end
else
false
end
end
def self.[]=(sym, data)
if sym.is_a? Symbol
obj = self.[] sym, true
obj.data = data
obj.save
obj.data
else
false
end
end
def data
if (read_local(:klass))
# lookup class from saved :klass string
k = Kernel.const_get(read_local(:klass))
# call typeconversion method on redis-saved string
if (k == Fixnum || k == Bignum)
read_local(:data).to_i
elsif (k == Float)
read_local(:data).to_f
elsif (k == String)
read_local(:data).to_s
elsif (k == TrueClass)
true
elsif (k == FalseClass)
false
else
nil
end
else
nil
end
end
# save data, retrieve class from object if not passed in
def data=(obj, klass = obj.class)
write_local(:klass, klass.to_s)
write_local(:data, obj.to_s)
@klass = klass.to_s
@data = obj.to_s
end
def klass
read_local(:klass)
end
private
def klass=(klass)
write_local(:klass, klass.to_s)
@klass = klass
end
end