tinymongo 0.1.3 → 0.1.4
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/VERSION +1 -1
- data/lib/tinymongo/model.rb +22 -16
- data/lib/tinymongo/modifiers.rb +9 -9
- data/test/test_tinymongo.rb +70 -18
- data/tinymongo.gemspec +1 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.4
|
data/lib/tinymongo/model.rb
CHANGED
@@ -39,30 +39,32 @@ module TinyMongo
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def find(*args)
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
end
|
42
|
+
return [] if((args.size > 0) && (args.compact.size == 0))
|
43
|
+
|
44
|
+
new_args = args.map {|arg| Helper.hashify_models_in(arg) }
|
46
45
|
cursor = collection.find(*new_args)
|
47
46
|
|
48
|
-
if(cursor
|
47
|
+
if(cursor)
|
49
48
|
hashes = cursor.to_a
|
50
49
|
objs = hashes.map { |hash| self.new(hash) }
|
51
50
|
end
|
52
51
|
|
53
|
-
cursor ? objs :
|
52
|
+
cursor ? objs : []
|
54
53
|
end
|
55
54
|
|
56
55
|
def find_one(*args)
|
57
|
-
if(
|
56
|
+
if((args.size > 0) && (args.compact.size == 0))
|
57
|
+
return nil
|
58
|
+
elsif((args.size == 1) && ([BSON::ObjectID, String].include? args[0].class))
|
58
59
|
hash = collection.find_one({'_id' => Helper.bson_object_id(args[0])})
|
59
60
|
else
|
60
|
-
|
61
|
+
new_args = args.map {|arg| Helper.hashify_models_in(arg) }
|
62
|
+
hash = collection.find_one(*new_args)
|
61
63
|
end
|
62
64
|
hash ? self.new(hash) : nil
|
63
65
|
end
|
64
66
|
|
65
|
-
def create(hash)
|
67
|
+
def create(hash={})
|
66
68
|
obj = self.new(hash)
|
67
69
|
obj.save
|
68
70
|
end
|
@@ -71,6 +73,10 @@ module TinyMongo
|
|
71
73
|
collection.remove({ '_id' => Helper.bson_object_id(id)})
|
72
74
|
end
|
73
75
|
|
76
|
+
def destroy(id)
|
77
|
+
delete(id)
|
78
|
+
end
|
79
|
+
|
74
80
|
def drop
|
75
81
|
collection.drop
|
76
82
|
end
|
@@ -100,6 +106,10 @@ module TinyMongo
|
|
100
106
|
@_tinymongo_hash['_id'] = Helper.bson_object_id(val)
|
101
107
|
end
|
102
108
|
|
109
|
+
def ==(another)
|
110
|
+
self.to_hash == another.to_hash
|
111
|
+
end
|
112
|
+
|
103
113
|
def db
|
104
114
|
self.db
|
105
115
|
end
|
@@ -109,7 +119,7 @@ module TinyMongo
|
|
109
119
|
end
|
110
120
|
|
111
121
|
def to_hash
|
112
|
-
@_tinymongo_hash.
|
122
|
+
@_tinymongo_hash.dup
|
113
123
|
end
|
114
124
|
|
115
125
|
def reload
|
@@ -138,7 +148,7 @@ module TinyMongo
|
|
138
148
|
save
|
139
149
|
end
|
140
150
|
|
141
|
-
def update_attributes(hash)
|
151
|
+
def update_attributes(hash={})
|
142
152
|
hash.each_pair { |key, value| send(key.to_s + '=', value) }
|
143
153
|
save
|
144
154
|
end
|
@@ -152,11 +162,7 @@ module TinyMongo
|
|
152
162
|
def destroy
|
153
163
|
delete
|
154
164
|
end
|
155
|
-
|
156
|
-
def destroy(id)
|
157
|
-
delete(id)
|
158
|
-
end
|
159
|
-
|
165
|
+
|
160
166
|
include Modifiers
|
161
167
|
end
|
162
168
|
end
|
data/lib/tinymongo/modifiers.rb
CHANGED
@@ -1,38 +1,38 @@
|
|
1
1
|
module TinyMongo
|
2
2
|
module Modifiers
|
3
|
-
def inc(hash)
|
3
|
+
def inc(hash={})
|
4
4
|
do_modifier_operation_and_reload('$inc', hash)
|
5
5
|
end
|
6
6
|
|
7
|
-
def set(hash)
|
7
|
+
def set(hash={})
|
8
8
|
do_modifier_operation_and_reload('$set', hash)
|
9
9
|
end
|
10
10
|
|
11
|
-
def unset(hash)
|
11
|
+
def unset(hash={})
|
12
12
|
do_modifier_operation_and_reload('$unset', hash)
|
13
13
|
end
|
14
14
|
|
15
|
-
def push(hash)
|
15
|
+
def push(hash={})
|
16
16
|
do_modifier_operation_and_reload('$push', hash)
|
17
17
|
end
|
18
18
|
|
19
|
-
def push_all(hash)
|
19
|
+
def push_all(hash={})
|
20
20
|
do_modifier_operation_and_reload('$pushAll', hash)
|
21
21
|
end
|
22
22
|
|
23
|
-
def add_to_set(hash)
|
23
|
+
def add_to_set(hash={})
|
24
24
|
do_modifier_operation_and_reload('$addToSet', hash)
|
25
25
|
end
|
26
26
|
|
27
|
-
def pop(hash)
|
27
|
+
def pop(hash={})
|
28
28
|
do_modifier_operation_and_reload('$pop', hash)
|
29
29
|
end
|
30
30
|
|
31
|
-
def pull(hash)
|
31
|
+
def pull(hash={})
|
32
32
|
do_modifier_operation_and_reload('$pull', hash)
|
33
33
|
end
|
34
34
|
|
35
|
-
def pull_all(hash)
|
35
|
+
def pull_all(hash={})
|
36
36
|
do_modifier_operation_and_reload('$pullAll', hash)
|
37
37
|
end
|
38
38
|
|
data/test/test_tinymongo.rb
CHANGED
@@ -103,32 +103,84 @@ class TinyMongoTest < Test::Unit::TestCase
|
|
103
103
|
assert_equal 'hello', result['bar']
|
104
104
|
end
|
105
105
|
|
106
|
-
def
|
107
|
-
|
108
|
-
|
109
|
-
|
106
|
+
def test_find_nothing
|
107
|
+
found = Dummy.find()
|
108
|
+
assert_equal [], found
|
109
|
+
end
|
110
110
|
|
111
|
-
|
112
|
-
|
111
|
+
def test_find_all_one
|
112
|
+
obj = Dummy.create('foo' => 'hello')
|
113
|
+
found = Dummy.find()
|
114
|
+
assert_equal [obj], found
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_find_all_many
|
118
|
+
obj = Dummy.create('foo' => 'hello')
|
119
|
+
obj2 = Dummy.create('foo' => 'hello')
|
120
|
+
found = Dummy.find()
|
121
|
+
assert_equal [obj, obj2], found
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_find_among_many
|
125
|
+
obj = Dummy.create('foo' => 'hello')
|
126
|
+
obj2 = Dummy.create('foo' => 'hello')
|
127
|
+
obj3 = Dummy.create('foo' => 'bye')
|
128
|
+
found1 = Dummy.find({'foo' => 'hello'})
|
129
|
+
found2 = Dummy.find({'foo' => 'bye'})
|
130
|
+
assert_equal [obj, obj2], found1
|
131
|
+
assert_equal [obj3], found2
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_find_one
|
135
|
+
obj = Dummy.create('foo' => 'hello')
|
136
|
+
found = Dummy.find_one()
|
137
|
+
assert_equal obj, found
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_find_one_nil
|
141
|
+
found = Dummy.find_one(nil)
|
142
|
+
assert_equal nil, found
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_find_one_using_id
|
146
|
+
obj = Dummy.create('foo' => 'hello')
|
147
|
+
found = Dummy.find_one(obj._id)
|
148
|
+
assert_equal obj, found
|
113
149
|
end
|
114
150
|
|
115
151
|
def test_find_one_using_id_string
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
152
|
+
obj = Dummy.create('foo' => 'hello')
|
153
|
+
found = Dummy.find_one(obj._id.to_s)
|
154
|
+
assert_equal obj, found
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_find_one_using_id_in_hash
|
158
|
+
obj = Dummy.create('foo' => 'hello')
|
159
|
+
found = Dummy.find_one({'_id' => obj._id})
|
160
|
+
assert_equal obj, found
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_find_one_using_id_string_in_hash
|
164
|
+
obj = Dummy.create('foo' => 'hello')
|
165
|
+
found = Dummy.find_one({'_id' => obj._id.to_s})
|
166
|
+
assert_equal obj, found
|
122
167
|
end
|
123
168
|
|
124
169
|
def test_find_one_using_hash
|
125
|
-
|
126
|
-
|
170
|
+
obj = Dummy.create('foo' => 'hello')
|
127
171
|
found = Dummy.find_one({'foo' => 'hello'})
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
172
|
+
assert_equal obj, found
|
173
|
+
end
|
174
|
+
|
175
|
+
def test_to_hash
|
176
|
+
obj = Dummy.create('foo' => 'hello')
|
177
|
+
assert_equal({'_id' => obj._id, 'foo' => 'hello'}, obj.to_hash)
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_eq
|
181
|
+
obj = Dummy.create('foo' => 'hello')
|
182
|
+
obj2 = Dummy.find_one()
|
183
|
+
assert_equal obj, obj2
|
132
184
|
end
|
133
185
|
|
134
186
|
def test_count
|
data/tinymongo.gemspec
CHANGED