redismodel 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/redismodel.rb +19 -28
- data/test/class_mathod_tests.rb +12 -1
- data/test/models/person.rb +2 -2
- data/test/property_type_tests.rb +6 -0
- data/test/test_helper.rb +8 -1
- 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.5'
|
15
15
|
DEFAULT_CONFIG = {
|
16
16
|
:atomic => true,
|
17
17
|
:default_sort => "created_at"
|
@@ -37,7 +37,8 @@ class RedisModel
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
-
class
|
40
|
+
class RecordNotFound < RuntimeError; end
|
41
|
+
class PropertyTypeIncorrect < RuntimeError; end
|
41
42
|
|
42
43
|
def self.connection(klass)
|
43
44
|
hash = {}
|
@@ -54,7 +55,7 @@ class RedisModel
|
|
54
55
|
# Casts a object type as a compatible type for redis, or reverses the process.
|
55
56
|
#
|
56
57
|
# Examples:
|
57
|
-
# #cast(
|
58
|
+
# #cast( Fixnum, "1" ) => 1
|
58
59
|
# #cast( Array, [1,2,3] ) => "[1,2,3]"
|
59
60
|
# #cast( Hash, {:a => 1, 'b' => "2", :c => ['c']} ) => "--- \nb: \"2\"\n:c: \n- c\n:a: 1\n"
|
60
61
|
# #cast( Hash, "--- \nb: \"2\"\n:c: \n- c\n:a: 1\n" ) => {"b"=>"2", :c=>["c"], :a=>1}
|
@@ -64,7 +65,7 @@ class RedisModel
|
|
64
65
|
klass = klass.to_s
|
65
66
|
v_class = value.class.to_s
|
66
67
|
|
67
|
-
if klass == "
|
68
|
+
if klass == "Fixnum" && v_class == "String"
|
68
69
|
value.to_i
|
69
70
|
elsif (klass == "Array" || klass == "Hash") && v_class == "String"
|
70
71
|
YAML.load(value)
|
@@ -93,14 +94,15 @@ class RedisModel
|
|
93
94
|
end
|
94
95
|
|
95
96
|
|
96
|
-
def self.blank_record
|
97
|
-
|
97
|
+
def self.blank_record(hash={})
|
98
|
+
blanks = {
|
99
|
+
String => "",
|
100
|
+
Array => [],
|
101
|
+
Hash => {}
|
102
|
+
}
|
98
103
|
self.properties.to_a.map do |c|
|
99
|
-
|
100
|
-
|
101
|
-
unless blank.nil?
|
102
|
-
hash[c[0]] = blank
|
103
|
-
end
|
104
|
+
blank = blanks[c[1]]
|
105
|
+
hash[c[0]] = blank unless blank.nil?
|
104
106
|
end
|
105
107
|
hash.delete(:id)
|
106
108
|
hash
|
@@ -128,24 +130,21 @@ class RedisModel
|
|
128
130
|
|
129
131
|
redis.set "#{self.class}:#{id}:id", id
|
130
132
|
redis.incr "#{self.class}:_meta:count"
|
131
|
-
|
132
|
-
else
|
133
|
-
|
133
|
+
else
|
134
134
|
self.send("id=", hash[:id])
|
135
135
|
if get(:id).nil?
|
136
|
-
raise RedisModel::
|
136
|
+
raise RedisModel::RecordNotFound
|
137
137
|
else
|
138
138
|
self.reload
|
139
139
|
end
|
140
|
-
|
141
140
|
end
|
142
141
|
end
|
143
142
|
|
144
143
|
|
145
144
|
# Custom model inspect in the format of:
|
146
|
-
# #<MyModel id: "
|
145
|
+
# #<MyModel:8a80300a9b id: "8a80300a9b...", created_at: "2010-08-01T01:32:11+01:00">
|
147
146
|
def inspect
|
148
|
-
"#<#{self.class} #{self.class.properties.to_a.map{|c| "#{c[0].to_s}: #{get(c[0]).inspect}"}.join(", ")}>"
|
147
|
+
"#<#{self.class}:#{self.id} #{self.class.properties.to_a.delete_if{|c| c[0] == :id}.map{|c| "#{c[0].to_s}: #{g=get(c[0]); v=g.inspect; g.class == String && v.size > 10 ? v[0..(v.size-2)]+'..."' : v}"}.join(", ")}>"
|
149
148
|
end
|
150
149
|
|
151
150
|
|
@@ -195,7 +194,7 @@ class RedisModel
|
|
195
194
|
#
|
196
195
|
# class MyModel < RedisModel
|
197
196
|
# property :name, String
|
198
|
-
# property :age,
|
197
|
+
# property :age, Fixnum
|
199
198
|
# end
|
200
199
|
#
|
201
200
|
def self.property(name, type)
|
@@ -295,6 +294,7 @@ class RedisModel
|
|
295
294
|
# Writes a property value to redis, and updates #updated_at if required.
|
296
295
|
def set(property, value)
|
297
296
|
c = self.class.type(property)
|
297
|
+
raise RedisModel::PropertyTypeIncorrect if value.class != c
|
298
298
|
value = RedisModel.cast c, value
|
299
299
|
redis.set "#{self.class}:#{id}:#{property}", value
|
300
300
|
@_data[property] = value
|
@@ -322,15 +322,6 @@ class RedisModel
|
|
322
322
|
properties[property]
|
323
323
|
end
|
324
324
|
|
325
|
-
def self.blank_value(type)
|
326
|
-
blanks = {
|
327
|
-
String => "",
|
328
|
-
Array => [],
|
329
|
-
Hash => {}
|
330
|
-
}
|
331
|
-
return blanks[type]
|
332
|
-
end
|
333
|
-
|
334
325
|
|
335
326
|
# Keeps track of the properties declared via #property
|
336
327
|
def self.properties
|
data/test/class_mathod_tests.rb
CHANGED
@@ -4,13 +4,19 @@ class ClassMethodTests < Test::Unit::TestCase
|
|
4
4
|
def setup
|
5
5
|
Person.destroy_all
|
6
6
|
@person1 = Person.new(:name => "ashley", :age => 18)
|
7
|
-
@person2 = Person.new(:name => "ashley", :age => 18)
|
7
|
+
@person2 = Person.new(:name => "ashley", :age => 18)
|
8
8
|
end
|
9
9
|
|
10
10
|
def test_find
|
11
11
|
assert Person.find(@person1.id).name == @person1.name
|
12
12
|
end
|
13
13
|
|
14
|
+
def test_invalid_find_raises_error
|
15
|
+
assert_raises RedisModel::RecordNotFound do
|
16
|
+
Person.find("abcd")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
14
20
|
def test_destroy_all
|
15
21
|
assert Person.count == 2
|
16
22
|
Person.destroy_all
|
@@ -29,6 +35,11 @@ class ClassMethodTests < Test::Unit::TestCase
|
|
29
35
|
assert all[0].name == @person1.name
|
30
36
|
end
|
31
37
|
|
38
|
+
def test_all_when_no_records_exist
|
39
|
+
Person.destroy_all
|
40
|
+
assert Person.all == []
|
41
|
+
end
|
42
|
+
|
32
43
|
def test_search
|
33
44
|
Person.search(:age, 19)
|
34
45
|
end
|
data/test/models/person.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# require 'rubygems'
|
2
|
-
#
|
2
|
+
# require 'redismodel'
|
3
3
|
# require File.dirname(__FILE__) + '/../../lib/redismodel'
|
4
4
|
|
5
5
|
class Person < RedisModel
|
@@ -7,7 +7,7 @@ class Person < RedisModel
|
|
7
7
|
# :port => 5678
|
8
8
|
|
9
9
|
property :name, String
|
10
|
-
property :age,
|
10
|
+
property :age, Fixnum
|
11
11
|
property :dob, DateTime
|
12
12
|
property :favorite_games, Array
|
13
13
|
property :test_hash, Hash
|
data/test/property_type_tests.rb
CHANGED
@@ -14,6 +14,12 @@ class ColumnTypeTests < Test::Unit::TestCase
|
|
14
14
|
# Person.destroy_all
|
15
15
|
# end
|
16
16
|
|
17
|
+
def test_doesnt_allow_incorrect_types
|
18
|
+
assert_raises RedisModel::PropertyTypeIncorrect do
|
19
|
+
@person.dob = [1,2,3]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
17
23
|
def test_string
|
18
24
|
assert @person.name == "ashley"
|
19
25
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
require 'stringio'
|
2
2
|
require 'test/unit'
|
3
|
-
|
3
|
+
|
4
|
+
if ARGV.include?("g")
|
5
|
+
require 'rubygems'
|
6
|
+
require 'redismodel'
|
7
|
+
else
|
8
|
+
require File.dirname(__FILE__) + '/../lib/redismodel' unless defined? RedisModel
|
9
|
+
end
|
10
|
+
|
4
11
|
Dir.glob("test/models/*.rb").each{|f| require f}
|
5
12
|
Dir.glob("test/*_tests.rb").each{|f| require f}
|