tinymongo 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +3 -3
- data/VERSION +1 -1
- data/lib/tinymongo/cursor.rb +2 -2
- data/lib/tinymongo/model.rb +37 -15
- data/test/test_tinymongo.rb +66 -1
- data/tinymongo.gemspec +2 -2
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -26,11 +26,11 @@ To create TinyMongo config file (config/tinymongo.yml) and initializer file (con
|
|
26
26
|
class Person < TinyMongo::Model
|
27
27
|
mongo_collection :people # optional if using Rails
|
28
28
|
mongo_key :name
|
29
|
-
mongo_key :age
|
30
|
-
mongo_key :children
|
29
|
+
mongo_key :age, :default => 0
|
30
|
+
mongo_key :children, :default => []
|
31
31
|
|
32
32
|
def make_child
|
33
|
-
child = Person.create(:name => 'Baby'
|
33
|
+
child = Person.create(:name => 'Baby')
|
34
34
|
push({:children => child}) # push child into children array
|
35
35
|
end
|
36
36
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.6
|
data/lib/tinymongo/cursor.rb
CHANGED
@@ -87,7 +87,7 @@ module TinyMongo
|
|
87
87
|
|
88
88
|
def next_document
|
89
89
|
doc = @_tinymongo_cursor.next_document
|
90
|
-
@_tinymongo_model_class.new(doc)
|
90
|
+
@_tinymongo_model_class.new(doc) if doc
|
91
91
|
end
|
92
92
|
|
93
93
|
def query_options_hash
|
@@ -120,7 +120,7 @@ module TinyMongo
|
|
120
120
|
return [] if @_tinymongo_cursor.nil?
|
121
121
|
|
122
122
|
hashes = @_tinymongo_cursor.to_a
|
123
|
-
hashes.map { |hash| @_tinymongo_model_class.new(hash) }
|
123
|
+
hashes.map { |hash| @_tinymongo_model_class.new(hash) if hash }
|
124
124
|
end
|
125
125
|
|
126
126
|
protected
|
data/lib/tinymongo/model.rb
CHANGED
@@ -2,13 +2,22 @@ module TinyMongo
|
|
2
2
|
class Model
|
3
3
|
class << self
|
4
4
|
def mongo_key(*args)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
default = nil
|
6
|
+
@_tinymongo_defaults = {} if @_tinymongo_defaults.nil?
|
7
|
+
|
8
|
+
args.each do |arg|
|
9
|
+
default = arg[:default] || arg['default'] if(arg.instance_of? Hash)
|
10
|
+
end
|
11
|
+
|
12
|
+
args.each do |arg|
|
13
|
+
if([Symbol, String].include? arg.class)
|
14
|
+
key_name_s = arg.to_s
|
15
|
+
key_name_sym = arg.to_sym
|
9
16
|
|
10
17
|
define_method(key_name_sym) { instance_variable_get(:@_tinymongo_hash)[key_name_s] }
|
11
18
|
define_method("#{key_name_s}=") { |val| instance_variable_get(:@_tinymongo_hash)[key_name_s] = val }
|
19
|
+
|
20
|
+
@_tinymongo_defaults[key_name_s] = default if default
|
12
21
|
end
|
13
22
|
end
|
14
23
|
end
|
@@ -79,7 +88,7 @@ module TinyMongo
|
|
79
88
|
end
|
80
89
|
|
81
90
|
def initialize(hash={})
|
82
|
-
@_tinymongo_hash = Helper.stringify_keys_in_hash(hash) || {}
|
91
|
+
@_tinymongo_hash = (self.class.instance_variable_get(:@_tinymongo_defaults).merge(Helper.stringify_keys_in_hash(hash)) || {}) if hash
|
83
92
|
end
|
84
93
|
|
85
94
|
def _id
|
@@ -90,8 +99,17 @@ module TinyMongo
|
|
90
99
|
@_tinymongo_hash['_id'] = Helper.bson_object_id(val)
|
91
100
|
end
|
92
101
|
|
102
|
+
def id
|
103
|
+
@_tinymongo_hash['_id'].to_s
|
104
|
+
end
|
105
|
+
|
106
|
+
def id=(val)
|
107
|
+
@_tinymongo_hash['_id'] = Helper.bson_object_id(val)
|
108
|
+
end
|
109
|
+
|
93
110
|
def ==(another)
|
94
|
-
self.
|
111
|
+
(self.instance_variable_get(:@_tinymongo_hash) == another.instance_variable_get(:@_tinymongo_hash)) &&
|
112
|
+
(self.kind_of? TinyMongo::Model) && (another.kind_of? TinyMongo::Model)
|
95
113
|
end
|
96
114
|
|
97
115
|
def db
|
@@ -106,22 +124,26 @@ module TinyMongo
|
|
106
124
|
@_tinymongo_hash.dup
|
107
125
|
end
|
108
126
|
|
127
|
+
def to_param
|
128
|
+
@_tinymongo_hash['_id'].to_s
|
129
|
+
end
|
130
|
+
|
109
131
|
def reload
|
110
|
-
if(
|
111
|
-
obj = collection.find_one({ '_id' =>
|
132
|
+
if(@_tinymongo_hash['_id'])
|
133
|
+
obj = collection.find_one({ '_id' => @_tinymongo_hash['_id'] })
|
112
134
|
@_tinymongo_hash = Helper.stringify_keys_in_hash(obj) if(obj)
|
113
135
|
end
|
114
136
|
end
|
115
137
|
|
116
138
|
def save
|
117
|
-
if(
|
139
|
+
if(@_tinymongo_hash['_id'].nil?) # new
|
118
140
|
oid = collection.save(@_tinymongo_hash)
|
119
141
|
if(oid)
|
120
142
|
@_tinymongo_hash.delete(:_id)
|
121
|
-
|
143
|
+
@_tinymongo_hash['_id'] = oid
|
122
144
|
end
|
123
145
|
else # update
|
124
|
-
collection.update({ '_id' =>
|
146
|
+
collection.update({ '_id' => @_tinymongo_hash['_id'] }, @_tinymongo_hash, :upsert => true)
|
125
147
|
reload
|
126
148
|
end
|
127
149
|
return self
|
@@ -138,8 +160,8 @@ module TinyMongo
|
|
138
160
|
end
|
139
161
|
|
140
162
|
def delete
|
141
|
-
if(
|
142
|
-
collection.remove({ '_id' =>
|
163
|
+
if(@_tinymongo_hash['_id'])
|
164
|
+
collection.remove({ '_id' => @_tinymongo_hash['_id'] })
|
143
165
|
end
|
144
166
|
end
|
145
167
|
|
@@ -185,8 +207,8 @@ module TinyMongo
|
|
185
207
|
|
186
208
|
protected
|
187
209
|
def do_modifier_operation_and_reload(operator, hash)
|
188
|
-
raise ModifierOperationError unless
|
189
|
-
collection.update({ '_id' =>
|
210
|
+
raise ModifierOperationError unless @_tinymongo_hash['_id']
|
211
|
+
collection.update({ '_id' => @_tinymongo_hash['_id'] }, { operator => Helper.hashify_models_in_hash(hash) })
|
190
212
|
reload
|
191
213
|
end
|
192
214
|
end
|
data/test/test_tinymongo.rb
CHANGED
@@ -10,6 +10,15 @@ class Dummy < TinyMongo::Model
|
|
10
10
|
mongo_key :bar
|
11
11
|
end
|
12
12
|
|
13
|
+
class DummyTwo < TinyMongo::Model
|
14
|
+
mongo_key :foo, :bar
|
15
|
+
end
|
16
|
+
|
17
|
+
class DummyDefault < TinyMongo::Model
|
18
|
+
mongo_key :foo, :default => 'hello'
|
19
|
+
mongo_key :bar, :default => 'world'
|
20
|
+
end
|
21
|
+
|
13
22
|
class TinyMongoTest < Test::Unit::TestCase
|
14
23
|
def setup
|
15
24
|
TinyMongo.db['dummies'].drop()
|
@@ -57,6 +66,28 @@ class TinyMongoTest < Test::Unit::TestCase
|
|
57
66
|
assert_equal 'world', m.bar
|
58
67
|
end
|
59
68
|
|
69
|
+
def test_mongo_two_keys_in_one_line
|
70
|
+
m = DummyTwo.new('foo' => 'hello')
|
71
|
+
m.bar = 'world'
|
72
|
+
|
73
|
+
assert_equal 'hello', m.foo
|
74
|
+
assert_equal 'world', m.bar
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_mongo_key_with_default_value
|
78
|
+
m = DummyDefault.new
|
79
|
+
|
80
|
+
assert_equal 'hello', m.foo
|
81
|
+
assert_equal 'world', m.bar
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_mongo_key_override_default_value
|
85
|
+
m = DummyDefault.new(:foo => 'world', :bar => 'hello')
|
86
|
+
|
87
|
+
assert_equal 'world', m.foo
|
88
|
+
assert_equal 'hello', m.bar
|
89
|
+
end
|
90
|
+
|
60
91
|
def test_save_new
|
61
92
|
hash = {'foo' => 'hello', 'bar' => 'world'}
|
62
93
|
|
@@ -189,11 +220,21 @@ class TinyMongoTest < Test::Unit::TestCase
|
|
189
220
|
assert_equal true, cursor.has_next?
|
190
221
|
end
|
191
222
|
|
223
|
+
def test_cursor_has_next_is_false
|
224
|
+
cursor = Dummy.find
|
225
|
+
assert_equal false, cursor.has_next?
|
226
|
+
end
|
227
|
+
|
192
228
|
def test_cursor_next_document
|
193
229
|
obj = Dummy.create
|
194
230
|
cursor = Dummy.find
|
195
231
|
assert_equal obj, cursor.next_document
|
196
232
|
end
|
233
|
+
|
234
|
+
def test_cursor_next_document_nil
|
235
|
+
cursor = Dummy.find
|
236
|
+
assert_equal nil, cursor.next_document
|
237
|
+
end
|
197
238
|
|
198
239
|
def test_cursor_skip
|
199
240
|
Dummy.create('foo' => 1)
|
@@ -203,6 +244,15 @@ class TinyMongoTest < Test::Unit::TestCase
|
|
203
244
|
cursor = Dummy.find.skip(2)
|
204
245
|
assert_equal obj, cursor.next_document
|
205
246
|
end
|
247
|
+
|
248
|
+
def test_cursor_skip_all
|
249
|
+
Dummy.create('foo' => 1)
|
250
|
+
Dummy.create('foo' => 2)
|
251
|
+
obj = Dummy.create('foo' => 3)
|
252
|
+
|
253
|
+
cursor = Dummy.find.skip(3)
|
254
|
+
assert_equal nil, cursor.next_document
|
255
|
+
end
|
206
256
|
|
207
257
|
def test_cursor_skip_count
|
208
258
|
Dummy.create('foo' => 1)
|
@@ -221,6 +271,11 @@ class TinyMongoTest < Test::Unit::TestCase
|
|
221
271
|
cursor = Dummy.find
|
222
272
|
assert_equal [obj1, obj2, obj3], cursor.to_a
|
223
273
|
end
|
274
|
+
|
275
|
+
def test_cursor_to_a_empty
|
276
|
+
cursor = Dummy.find
|
277
|
+
assert_equal [], cursor.to_a
|
278
|
+
end
|
224
279
|
|
225
280
|
def test_cursor_sort
|
226
281
|
obj1 = Dummy.create('foo' => 3)
|
@@ -286,8 +341,18 @@ class TinyMongoTest < Test::Unit::TestCase
|
|
286
341
|
assert_equal({'_id' => obj._id, 'foo' => 'hello'}, obj.to_hash)
|
287
342
|
end
|
288
343
|
|
344
|
+
def test_id
|
345
|
+
obj = Dummy.create
|
346
|
+
assert_equal obj._id.to_s, obj.id
|
347
|
+
end
|
348
|
+
|
349
|
+
def test_to_param
|
350
|
+
obj = Dummy.create
|
351
|
+
assert_equal obj._id.to_s, obj.to_param
|
352
|
+
end
|
353
|
+
|
289
354
|
def test_eq
|
290
|
-
obj = Dummy.create
|
355
|
+
obj = Dummy.create
|
291
356
|
obj2 = Dummy.find_one()
|
292
357
|
assert_equal obj, obj2
|
293
358
|
end
|
data/tinymongo.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{tinymongo}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Peter Jihoon Kim"]
|
12
|
-
s.date = %q{2010-07-
|
12
|
+
s.date = %q{2010-07-27}
|
13
13
|
s.description = %q{Simple MongoDB wrapper}
|
14
14
|
s.email = %q{raingrove@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 6
|
9
|
+
version: 0.1.6
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Peter Jihoon Kim
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-07-
|
17
|
+
date: 2010-07-27 00:00:00 +09:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|