redismodel 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/redismodel.rb +42 -10
- data/test/class_mathod_tests.rb +0 -1
- data/test/models/person.rb +17 -2
- data/test/property_type_tests.rb +27 -6
- metadata +2 -2
data/lib/redismodel.rb
CHANGED
@@ -11,7 +11,7 @@ class DateTime
|
|
11
11
|
end
|
12
12
|
|
13
13
|
class RedisModel
|
14
|
-
VERSION = '0.1.
|
14
|
+
VERSION = '0.1.4'
|
15
15
|
DEFAULT_CONFIG = {
|
16
16
|
:atomic => true,
|
17
17
|
:default_sort => "created_at"
|
@@ -19,8 +19,7 @@ class RedisModel
|
|
19
19
|
|
20
20
|
module ArrayProxy
|
21
21
|
def <<(other)
|
22
|
-
|
23
|
-
@__model__.send(:set, @__property__, tmp)
|
22
|
+
@__model__.send(:set, @__property__, super)
|
24
23
|
super
|
25
24
|
end
|
26
25
|
|
@@ -32,7 +31,7 @@ class RedisModel
|
|
32
31
|
module HashProxy
|
33
32
|
def []=(a,b)
|
34
33
|
tmp = self.dup
|
35
|
-
tmp[a] =
|
34
|
+
tmp[a] = super
|
36
35
|
@__model__.send(:set, @__property__, tmp)
|
37
36
|
super
|
38
37
|
end
|
@@ -61,6 +60,7 @@ class RedisModel
|
|
61
60
|
# #cast( Hash, "--- \nb: \"2\"\n:c: \n- c\n:a: 1\n" ) => {"b"=>"2", :c=>["c"], :a=>1}
|
62
61
|
#
|
63
62
|
def self.cast(klass, value)
|
63
|
+
return value if value.nil?
|
64
64
|
klass = klass.to_s
|
65
65
|
v_class = value.class.to_s
|
66
66
|
|
@@ -93,22 +93,39 @@ class RedisModel
|
|
93
93
|
end
|
94
94
|
|
95
95
|
|
96
|
+
def self.blank_record
|
97
|
+
hash = {}
|
98
|
+
self.properties.to_a.map do |c|
|
99
|
+
# # value ||= self.class.blank_value(c)
|
100
|
+
blank = self.blank_value(c[1])
|
101
|
+
unless blank.nil?
|
102
|
+
hash[c[0]] = blank
|
103
|
+
end
|
104
|
+
end
|
105
|
+
hash.delete(:id)
|
106
|
+
hash
|
107
|
+
end
|
108
|
+
|
96
109
|
# Creates a new object. If `{:id => "..."}` is passed, it'll attempt to recover the record
|
97
110
|
# from the redis DB. If it can't find such a record, it'll raise RedisModel::RecordNotFound
|
98
111
|
def initialize(hash={})
|
99
112
|
@_data = {}
|
113
|
+
|
100
114
|
if hash[:id].nil?
|
115
|
+
hash = self.class.blank_record.merge(hash)
|
116
|
+
self.send("id=", generate_uniq_token)
|
101
117
|
|
102
|
-
self.send("id=", generate_uniq_token)
|
103
118
|
if self.class.properties.include?(:created_at)
|
104
119
|
set :created_at, DateTime.now
|
105
120
|
end
|
121
|
+
|
106
122
|
hash.to_a.each do |property|
|
107
123
|
begin
|
108
124
|
self.send("#{property[0]}=", property[1])
|
109
125
|
rescue
|
110
126
|
end
|
111
127
|
end
|
128
|
+
|
112
129
|
redis.set "#{self.class}:#{id}:id", id
|
113
130
|
redis.incr "#{self.class}:_meta:count"
|
114
131
|
|
@@ -121,7 +138,7 @@ class RedisModel
|
|
121
138
|
self.reload
|
122
139
|
end
|
123
140
|
|
124
|
-
end
|
141
|
+
end
|
125
142
|
end
|
126
143
|
|
127
144
|
|
@@ -130,7 +147,7 @@ class RedisModel
|
|
130
147
|
def inspect
|
131
148
|
"#<#{self.class} #{self.class.properties.to_a.map{|c| "#{c[0].to_s}: #{get(c[0]).inspect}"}.join(", ")}>"
|
132
149
|
end
|
133
|
-
|
150
|
+
|
134
151
|
|
135
152
|
# Updates all keys from redis
|
136
153
|
def reload
|
@@ -190,13 +207,19 @@ class RedisModel
|
|
190
207
|
end
|
191
208
|
|
192
209
|
if type == Array || type == Hash
|
193
|
-
|
210
|
+
case type.to_s
|
211
|
+
when "Array"
|
212
|
+
proxy = ArrayProxy
|
213
|
+
when "Hash"
|
214
|
+
proxy = HashProxy
|
215
|
+
end
|
194
216
|
define_method(name) do
|
195
217
|
value = get(name)
|
196
218
|
return value if value.is_a?(proxy)
|
197
219
|
value.tap do |value|
|
198
|
-
value.extend(proxy)
|
199
|
-
value.
|
220
|
+
value.extend(proxy)
|
221
|
+
value.instance_variable_set(:@__model__, self)
|
222
|
+
value.instance_variable_set(:@__property__, name)
|
200
223
|
end
|
201
224
|
end
|
202
225
|
else
|
@@ -299,6 +322,15 @@ class RedisModel
|
|
299
322
|
properties[property]
|
300
323
|
end
|
301
324
|
|
325
|
+
def self.blank_value(type)
|
326
|
+
blanks = {
|
327
|
+
String => "",
|
328
|
+
Array => [],
|
329
|
+
Hash => {}
|
330
|
+
}
|
331
|
+
return blanks[type]
|
332
|
+
end
|
333
|
+
|
302
334
|
|
303
335
|
# Keeps track of the properties declared via #property
|
304
336
|
def self.properties
|
data/test/class_mathod_tests.rb
CHANGED
data/test/models/person.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
|
+
# require 'rubygems'
|
2
|
+
# # require 'redismodel'
|
3
|
+
# require File.dirname(__FILE__) + '/../../lib/redismodel'
|
4
|
+
|
1
5
|
class Person < RedisModel
|
2
6
|
# config :host => "1.2.3.4",
|
3
7
|
# :port => 5678
|
4
|
-
|
8
|
+
|
5
9
|
property :name, String
|
6
10
|
property :age, Integer
|
7
11
|
property :dob, DateTime
|
@@ -9,4 +13,15 @@ class Person < RedisModel
|
|
9
13
|
property :test_hash, Hash
|
10
14
|
property :created_at, DateTime
|
11
15
|
property :updated_at, DateTime
|
12
|
-
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# ashley = Person.new(:name => "ashley")
|
19
|
+
# puts ashley.inspect
|
20
|
+
# ashley.favorite_games << "Portal"
|
21
|
+
# ashley.favorite_games << "WoW"
|
22
|
+
#
|
23
|
+
# me = Person.find( ashley.id )
|
24
|
+
# puts me.name
|
25
|
+
# #=> ashley
|
26
|
+
# puts me.favorite_games.inspect
|
27
|
+
# #=> ["Portal", "WoW"]
|
data/test/property_type_tests.rb
CHANGED
@@ -5,10 +5,9 @@ class ColumnTypeTests < Test::Unit::TestCase
|
|
5
5
|
@person = Person.new(
|
6
6
|
:name => "ashley",
|
7
7
|
:age => 18,
|
8
|
-
:dob => DateTime.parse("05-09-1991")
|
9
|
-
:favorite_games => ["Portal", "WoW", 101],
|
10
|
-
:test_hash => {:a => 1, 'b' => "2", :c => ["Portal", 1, :c, [1,2,3]]}
|
8
|
+
:dob => DateTime.parse("05-09-1991")
|
11
9
|
)
|
10
|
+
|
12
11
|
end
|
13
12
|
|
14
13
|
# def teardown
|
@@ -23,32 +22,42 @@ class ColumnTypeTests < Test::Unit::TestCase
|
|
23
22
|
assert @person.age == 18
|
24
23
|
end
|
25
24
|
|
25
|
+
def test_integer_increment
|
26
|
+
@person.age += 1
|
27
|
+
assert @person.age == 19
|
28
|
+
end
|
29
|
+
|
26
30
|
def test_datetime
|
27
31
|
assert @person.dob.class == DateTime
|
28
32
|
assert @person.dob.to_s == "1991-09-05T00:00:00+00:00"
|
29
33
|
end
|
30
34
|
|
31
35
|
def test_array
|
32
|
-
|
36
|
+
@person.favorite_games << "Portal"
|
37
|
+
@person.favorite_games << "WoW"
|
38
|
+
assert @person.favorite_games == ["Portal", "WoW"]
|
33
39
|
end
|
34
40
|
|
35
41
|
def test_add_to_array_left_left
|
42
|
+
@person.favorite_games = ["Portal", "WoW", 101]
|
36
43
|
@person.favorite_games << "Awesome"
|
37
44
|
assert @person.favorite_games.size == 4
|
38
45
|
end
|
39
46
|
|
40
47
|
def test_add_to_array_push
|
48
|
+
@person.favorite_games = ["Portal", "WoW", 101]
|
41
49
|
@person.favorite_games.push("Awesome")
|
42
50
|
assert @person.favorite_games.size == 4
|
43
51
|
end
|
44
52
|
|
45
53
|
def test_hash
|
54
|
+
@person.test_hash = {:a => 1, 'b' => "2", :c => ["Portal", 1, :c, [1,2,3]]}
|
46
55
|
assert @person.test_hash == {:a => 1, 'b' => "2", :c => ["Portal", 1, :c, [1,2,3]]}
|
47
56
|
end
|
48
57
|
|
49
58
|
def test_add_key_to_hash
|
50
|
-
@person.test_hash[:
|
51
|
-
assert @person.test_hash[:
|
59
|
+
@person.test_hash[:first_key] = 123
|
60
|
+
assert @person.test_hash[:first_key] == 123
|
52
61
|
end
|
53
62
|
|
54
63
|
def test_id
|
@@ -62,4 +71,16 @@ class ColumnTypeTests < Test::Unit::TestCase
|
|
62
71
|
def test_updated_at
|
63
72
|
assert @person.updated_at.class == DateTime
|
64
73
|
end
|
74
|
+
|
75
|
+
def test_inital_array_value
|
76
|
+
assert Person.new().favorite_games == []
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_inital_hash_value
|
80
|
+
assert Person.new().test_hash == {}
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_inital_string_value
|
84
|
+
assert Person.new().name == ""
|
85
|
+
end
|
65
86
|
end
|