tinymongo 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -47,25 +47,31 @@ To create TinyMongo config file (config/tinymongo.yml) and initializer file (con
47
47
  end
48
48
  end
49
49
 
50
- Person.drop # empty drop collection
50
+ Person.drop # empty person collection
51
51
 
52
- p = Person.create(:name => 'John', :age => 20) # create John
53
- p.make_child # make Baby and set it as John's child
54
- p.grow_up # increment age by 1 = 21
52
+ p = Person.create(:name => 'John', :age => 20)
53
+ p.make_child
54
+ p.grow_up
55
55
 
56
56
  Person.find.each do |person|
57
- puts person.name # print Baby and John
57
+ puts person.name
58
58
  end
59
59
 
60
- puts Person.find_one(:name => 'John').age # print 21
60
+ Person.find_one(:name => 'John').age
61
61
 
62
62
  Person.create(:name => 'Jim')
63
63
  Person.create(:name => 'Pam')
64
64
 
65
- puts Person.find.to_a.map { |person| person.name } # print Baby; John; Jim; Pam
65
+ Person.find.to_a.map { |person| person.name }
66
66
 
67
- # Sort by name (ascending), Skip Baby and Jim, limit result by 1, print => John
68
- puts Person.find.sort({:name => 1}).skip(2).limit(1).next_document.name
67
+ Person.find.has_next?
68
+
69
+ Person.find.sort({:name => 1}).skip(2).limit(1).next!.name
70
+
71
+ Person.find({:name => 'John'}, {:age => 1}) # select only age field
72
+
73
+ Person.find.count # count ignores skip and limit
74
+ Person.find.skip(1).size # size is affected by skip and limit
69
75
 
70
76
  == Copyright
71
77
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.7
1
+ 0.1.8
@@ -14,65 +14,28 @@ module TinyMongo
14
14
  @_tinymongo_cursor
15
15
  end
16
16
 
17
- def batch_size
18
- @_tinymongo_cursor.batch_size
19
- end
20
-
21
- def collection
22
- @_tinymongo_cursor.collection
23
- end
24
-
25
- def fields
26
- @_tinymongo_cursor.fields
27
- end
28
-
29
- def full_collection_name
30
- @_tinymongo_cursor.full_collection_name
31
- end
32
-
33
- def hint
34
- @_tinymongo_cursor.hint
35
- end
36
-
37
- def order
38
- @_tinymongo_cursor.order
39
- end
40
-
41
- def selector
42
- @_tinymongo_cursor.selector
43
- end
44
-
45
- def snapshot
46
- @_tinymongo_cursor.snapshot
47
- end
48
-
49
- def timeout
50
- @_tinymongo_cursor.timeout
51
- end
52
-
53
- # instance methods in Mongo::Cursor
54
-
55
- def close
56
- @_tinymongo_cursor.close
57
- end
58
-
59
- def closed?
60
- @_tinymongo_cursor.closed?
61
- end
62
-
63
17
  def count
64
18
  @_tinymongo_cursor.count
65
19
  end
66
20
 
21
+ def size
22
+ num = (count - skip)
23
+ num = (num > 0) ? (((num > limit) && limit != 0) ? limit : num) : 0
24
+ end
25
+
67
26
  def each
68
27
  num_returned = 0
69
28
  while(has_next? && (@_tinymongo_cursor.instance_variable_get(:@limit) <= 0 ||
70
29
  num_returned < @_tinymongo_cursor.instance_variable_get(:@limit)))
71
- yield next_document
30
+ yield next!
72
31
  num_returned += 1
73
32
  end
74
33
  end
75
34
 
35
+ def forEach(*args)
36
+ each(*args)
37
+ end
38
+
76
39
  def explain
77
40
  @_tinymongo_cursor.explain
78
41
  end
@@ -81,21 +44,25 @@ module TinyMongo
81
44
  @_tinymongo_cursor.has_next?
82
45
  end
83
46
 
47
+ def hasNext
48
+ has_next?
49
+ end
50
+
84
51
  def limit(*args)
85
52
  call_and_wrap_retval_in_tinymongo_cursor(:limit, args)
86
53
  end
87
54
 
88
- def next_document
55
+ def next!
89
56
  doc = @_tinymongo_cursor.next_document
90
- @_tinymongo_model_class.new(doc) if doc
57
+ Helper.deserialize_hashes_in(doc)
91
58
  end
92
59
 
93
- def query_options_hash
94
- @_tinymongo_cursor.query_options_hash
60
+ def next_document
61
+ next!
95
62
  end
96
63
 
97
- def query_opts
98
- @_tinymongo_cursor.query_opts
64
+ def nextDocument
65
+ next!
99
66
  end
100
67
 
101
68
  def skip(*args)
@@ -104,13 +71,7 @@ module TinyMongo
104
71
 
105
72
  def sort(*args)
106
73
  if(args.length > 0 && (args[0].instance_of? Hash))
107
- sort_array = []
108
-
109
- args[0].each_pair do |key, value|
110
- sort_array << [key, convert_ascending_descending_to_numeric(value)]
111
- end
112
-
113
- args[0] = sort_array
74
+ args[0] = args[0].map { |k, v| [k, convert_ascending_descending_to_numeric(v)] }
114
75
  end
115
76
 
116
77
  call_and_wrap_retval_in_tinymongo_cursor(:sort, args)
@@ -120,7 +81,7 @@ module TinyMongo
120
81
  return [] if @_tinymongo_cursor.nil?
121
82
 
122
83
  hashes = @_tinymongo_cursor.to_a
123
- hashes.map { |hash| @_tinymongo_model_class.new(hash) if hash }
84
+ hashes.map { |hash| Helper.deserialize_hashes_in(hash) }
124
85
  end
125
86
 
126
87
  protected
@@ -19,4 +19,10 @@ module TinyMongo
19
19
  super('Modifier operations are not allowed on objects that are not yet saved!')
20
20
  end
21
21
  end
22
+
23
+ class DeserializationError < Error
24
+ def initialize(class_name)
25
+ super("Unable to deserialize hash into #{class_name}!")
26
+ end
27
+ end
22
28
  end
@@ -1,85 +1,78 @@
1
1
  module TinyMongo
2
2
  module Helper
3
3
  class << self
4
- def deep_copy_hash(hash)
5
- new_hash = {}
6
- hash.each_pair do |key, obj|
7
- new_hash[key] = if(obj.instance_of? Hash)
8
- deep_copy_hash(obj)
9
- elsif(obj.instance_of? Array)
10
- deep_copy_array(obj)
11
- else
12
- begin
13
- obj.dup
14
- rescue
15
- obj
16
- end
17
- end
18
- end
19
- new_hash
4
+ def constantize(class_name)
5
+ return unless class_name.instance_of? String
6
+ class_name.split('::').inject(Object) { |mod, klass| mod.const_get(klass) }
20
7
  end
21
-
22
- def deep_copy_array(array)
23
- new_array = []
24
- array.each do |obj|
25
- new_array << if(obj.instance_of? Hash)
26
- deep_copy_hash(obj)
27
- elsif(obj.instance_of? Array)
28
- deep_copy_array(obj)
29
- else
30
- begin
31
- obj.dup
32
- rescue
33
- obj
34
- end
8
+
9
+ def deep_copy(obj)
10
+ if(obj.kind_of? Hash)
11
+ Hash[obj.map { |k,v| [k.to_s, deep_copy(v)] }]
12
+ elsif(obj.kind_of? Array)
13
+ obj.map { |o| deep_copy(o)}
14
+ else
15
+ begin
16
+ obj.dup
17
+ rescue
18
+ obj
35
19
  end
36
20
  end
37
- new_array
38
21
  end
39
-
22
+
40
23
  def stringify_keys_in_hash(hash)
41
- new_hash = {}
42
- hash.each_pair { |key, value| new_hash[key.to_s] = value }
24
+ new_hash = Hash[hash.map { |k,v| [k.to_s, v] }]
43
25
  new_hash['_id'] = bson_object_id(new_hash['_id']) if(new_hash['_id'])
44
26
  new_hash
45
27
  end
46
28
 
47
29
  def symbolify_keys_in_hash(hash)
48
- new_hash = {}
49
- hash.each_pair { |key, value| new_hash[key.to_sym] = value }
30
+ new_hash = Hash[hash.map { |k,v| [k.to_sym, v] }]
50
31
  new_hash[:_id] = bson_object_id(new_hash[:_id]) if(new_hash[:_id])
51
32
  new_hash
52
33
  end
53
34
 
54
- def hashify_models_in(obj)
55
- if(obj.instance_of? Hash)
56
- hashify_models_in_hash(obj)
57
- elsif(obj.instance_of? Array)
58
- hashify_models_in_array(obj)
59
- elsif(obj.kind_of? TinyMongo::Model)
60
- obj.to_hash
35
+ def deserialize_hashes_in(obj)
36
+ if(obj.kind_of? Hash)
37
+ new_hash = Hash[obj.map { |k,v| [k.to_s, deserialize_hashes_in(v)] }]
38
+ class_name = new_hash['_tinymongo_model_class_name']
39
+ if(class_name)
40
+ begin
41
+ klass = constantize(new_hash['_tinymongo_model_class_name'])
42
+ if(klass.new.kind_of? TinyMongo::Model)
43
+ deserialized_obj = klass.new(new_hash)
44
+ else
45
+ raise
46
+ end
47
+ rescue
48
+ raise DeserializationError, class_name
49
+ end
50
+ deserialized_obj
51
+ else
52
+ new_hash
53
+ end
54
+ elsif(obj.kind_of? Array)
55
+ obj.map { |o| deserialize_hashes_in(o) }
61
56
  else
62
57
  obj
63
58
  end
64
59
  end
65
60
 
66
- def hashify_models_in_array(array)
67
- new_array = array.map { |value| hashify_models_in(value) }
68
- end
69
-
70
- def hashify_models_in_hash(hash)
71
- new_hash = {}
72
- hash.each_pair do |key,value|
73
- key_s = key.to_s
74
- if(key_s == '_id')
75
- new_hash[key_s] = bson_object_id(value)
76
- else
77
- new_hash[key_s] = hashify_models_in(value)
78
- end
61
+ def hashify_models_in(obj)
62
+ if(obj.kind_of? Hash)
63
+ Hash[obj.map do |k,v|
64
+ key = k.to_s
65
+ (key == '_id') ? [key, bson_object_id(v)] : [key, hashify_models_in(v)]
66
+ end]
67
+ elsif(obj.kind_of? Array)
68
+ obj.map { |o| hashify_models_in(o) }
69
+ elsif(obj.kind_of? TinyMongo::Model)
70
+ hashify_models_in(obj.to_hash)
71
+ else
72
+ obj
79
73
  end
80
- new_hash
81
74
  end
82
-
75
+
83
76
  def bson_object_id(id)
84
77
  if(id.instance_of? BSON::ObjectID)
85
78
  id
@@ -40,21 +40,31 @@ module TinyMongo
40
40
  end
41
41
  end
42
42
 
43
- def find(*args)
44
- new_args = args.map {|arg| Helper.hashify_models_in(arg) }
45
- Cursor.new(collection.find(*new_args), self)
43
+ def find(query={}, fields=nil, limit=nil, skip=nil)
44
+ query = Helper.hashify_models_in(query)
45
+ fields = Helper.hashify_models_in(fields)
46
+
47
+ add_tinymongo_model_class_name_key_to_fields(fields)
48
+ Cursor.new(collection.find(query, {:fields => fields, :limit => limit, :skip => skip}), self)
46
49
  end
47
-
48
- def find_one(*args)
49
- if((args.length > 0) && (args.compact.length == 0))
50
- return nil
51
- elsif((args.length == 1) && ([BSON::ObjectID, String].include? args[0].class))
52
- hash = collection.find_one({'_id' => Helper.bson_object_id(args[0])})
50
+
51
+ def find_one(query={}, fields=nil)
52
+ return nil unless query
53
+
54
+ if([BSON::ObjectID, String].include? query.class)
55
+ query = {'_id' => Helper.bson_object_id(query)}
53
56
  else
54
- new_args = args.map {|arg| Helper.hashify_models_in(arg) }
55
- hash = collection.find_one(*new_args)
57
+ query = Helper.hashify_models_in(query)
58
+ fields = Helper.hashify_models_in(fields)
56
59
  end
57
- hash ? self.new(hash) : nil
60
+
61
+ add_tinymongo_model_class_name_key_to_fields(fields)
62
+ hash = collection.find_one(query, {:fields => fields})
63
+ hash ? Helper.deserialize_hashes_in(hash) : nil
64
+ end
65
+
66
+ def findOne(*args)
67
+ find_one(*args)
58
68
  end
59
69
 
60
70
  def create(hash={})
@@ -88,7 +98,9 @@ module TinyMongo
88
98
  end
89
99
 
90
100
  def initialize(hash={})
91
- @_tinymongo_hash = (Helper.deep_copy_hash(self.class.instance_variable_get(:@_tinymongo_defaults)).merge(Helper.stringify_keys_in_hash(hash)) || {}) if hash
101
+ @_tinymongo_hash = (Helper.deep_copy(self.class.instance_variable_get(:@_tinymongo_defaults)).merge(Helper.stringify_keys_in_hash(hash)) || {}) if hash
102
+ set_tinymongo_model_class_name_in_hash(@_tinymongo_hash)
103
+ self
92
104
  end
93
105
 
94
106
  def _id
@@ -99,14 +111,6 @@ module TinyMongo
99
111
  @_tinymongo_hash['_id'] = Helper.bson_object_id(val)
100
112
  end
101
113
 
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
-
110
114
  def ==(another)
111
115
  (self.instance_variable_get(:@_tinymongo_hash) == another.instance_variable_get(:@_tinymongo_hash)) &&
112
116
  (self.kind_of? TinyMongo::Model) && (another.kind_of? TinyMongo::Model)
@@ -121,7 +125,7 @@ module TinyMongo
121
125
  end
122
126
 
123
127
  def to_hash
124
- @_tinymongo_hash.dup
128
+ hash = @_tinymongo_hash.dup
125
129
  end
126
130
 
127
131
  def to_param
@@ -131,19 +135,19 @@ module TinyMongo
131
135
  def reload
132
136
  if(@_tinymongo_hash['_id'])
133
137
  obj = collection.find_one({ '_id' => @_tinymongo_hash['_id'] })
134
- @_tinymongo_hash = Helper.stringify_keys_in_hash(obj) if(obj)
138
+ @_tinymongo_hash = Hash[obj.map { |k,v| [k.to_s, Helper.deserialize_hashes_in(v)] }] if(obj)
135
139
  end
136
140
  end
137
141
 
138
142
  def save
139
143
  if(@_tinymongo_hash['_id'].nil?) # new
140
- oid = collection.save(@_tinymongo_hash)
144
+ oid = collection.save(Helper.hashify_models_in(@_tinymongo_hash))
141
145
  if(oid)
142
146
  @_tinymongo_hash.delete(:_id)
143
147
  @_tinymongo_hash['_id'] = oid
144
148
  end
145
149
  else # update
146
- collection.update({ '_id' => @_tinymongo_hash['_id'] }, @_tinymongo_hash, :upsert => true)
150
+ collection.update({ '_id' => @_tinymongo_hash['_id'] }, Helper.hashify_models_in(@_tinymongo_hash), :upsert => true)
147
151
  reload
148
152
  end
149
153
  return self
@@ -184,14 +188,22 @@ module TinyMongo
184
188
  def push(hash={})
185
189
  do_modifier_operation_and_reload('$push', hash)
186
190
  end
187
-
191
+
188
192
  def push_all(hash={})
189
193
  do_modifier_operation_and_reload('$pushAll', hash)
190
194
  end
195
+
196
+ def pushAll(*args)
197
+ push_all(*args)
198
+ end
191
199
 
192
200
  def add_to_set(hash={})
193
201
  do_modifier_operation_and_reload('$addToSet', hash)
194
202
  end
203
+
204
+ def addToSet(*args)
205
+ add_to_set(*args)
206
+ end
195
207
 
196
208
  def pop(hash={})
197
209
  do_modifier_operation_and_reload('$pop', hash)
@@ -204,12 +216,30 @@ module TinyMongo
204
216
  def pull_all(hash={})
205
217
  do_modifier_operation_and_reload('$pullAll', hash)
206
218
  end
219
+
220
+ def pullAll(*args)
221
+ pull_all(*args)
222
+ end
207
223
 
208
224
  protected
209
225
  def do_modifier_operation_and_reload(operator, hash)
210
226
  raise ModifierOperationError unless @_tinymongo_hash['_id']
211
- collection.update({ '_id' => @_tinymongo_hash['_id'] }, { operator => Helper.hashify_models_in_hash(hash) })
227
+ collection.update({ '_id' => @_tinymongo_hash['_id'] }, { operator => Helper.hashify_models_in(hash) })
212
228
  reload
213
229
  end
230
+
231
+ def set_tinymongo_model_class_name_in_hash(hash)
232
+ hash['_tinymongo_model_class_name'] = self.class.to_s
233
+ hash
234
+ end
235
+
236
+ def self.add_tinymongo_model_class_name_key_to_fields(fields)
237
+ if(fields.kind_of? Hash)
238
+ fields['_tinymongo_model_class_name'] = 1
239
+ elsif(fields.kind_of? Array)
240
+ fields << '_tinymongo_model_class_name'
241
+ end
242
+ fields
243
+ end
214
244
  end
215
245
  end
@@ -38,24 +38,30 @@ class TinyMongoTest < Test::Unit::TestCase
38
38
  obj1 = Dummy.new(:foo => 'hello', :bar => 'world')
39
39
  obj2 = Dummy.new(:foo => 'love', :bar => 'ek')
40
40
  hash = { :obj1 => obj1, :obj2 => obj2, :hash => { :obj => obj1 }, :array => [obj1, obj2, 'yay'], :val => 'pete' }
41
- assert_equal({ 'obj1' => { 'foo' => 'hello', 'bar' => 'world' },
42
- 'obj2' => { 'foo' => 'love', 'bar' => 'ek' },
43
- 'hash' => { 'obj' => { 'foo' => 'hello', 'bar' => 'world' } },
44
- 'array' => [ { 'foo' => 'hello', 'bar' => 'world' }, { 'foo' => 'love', 'bar' => 'ek' }, 'yay' ],
41
+ obj1_hash = { 'foo' => 'hello', 'bar' => 'world', '_tinymongo_model_class_name' => 'Dummy' }
42
+ obj2_hash = { 'foo' => 'love', 'bar' => 'ek', '_tinymongo_model_class_name' => 'Dummy' }
43
+
44
+ assert_equal({ 'obj1' => obj1_hash,
45
+ 'obj2' => obj2_hash,
46
+ 'hash' => { 'obj' => obj1_hash },
47
+ 'array' => [ obj1_hash, obj2_hash, 'yay' ],
45
48
  'val' => 'pete' },
46
- TinyMongo::Helper.hashify_models_in_hash(hash))
49
+ TinyMongo::Helper.hashify_models_in(hash))
47
50
  end
48
51
 
49
52
  def test_helper_hashify_models_in_array
50
53
  obj1 = Dummy.new(:foo => 'hello', :bar => 'world')
51
54
  obj2 = Dummy.new(:foo => 'love', :bar => 'ek')
52
55
  array = [ obj1, obj2, { :obj => obj1 }, [obj1, obj2, 'yay'], 'pete' ]
53
- assert_equal([ { 'foo' => 'hello', 'bar' => 'world' },
54
- { 'foo' => 'love', 'bar' => 'ek' },
55
- { 'obj' => { 'foo' => 'hello', 'bar' => 'world' } },
56
- [ { 'foo' => 'hello', 'bar' => 'world' }, { 'foo' => 'love', 'bar' => 'ek' }, 'yay' ],
56
+ obj1_hash = { 'foo' => 'hello', 'bar' => 'world', '_tinymongo_model_class_name' => 'Dummy' }
57
+ obj2_hash = { 'foo' => 'love', 'bar' => 'ek', '_tinymongo_model_class_name' => 'Dummy' }
58
+
59
+ assert_equal([ obj1_hash,
60
+ obj2_hash,
61
+ { 'obj' => obj1_hash },
62
+ [ obj1_hash, obj2_hash, 'yay' ],
57
63
  'pete' ],
58
- TinyMongo::Helper.hashify_models_in_array(array))
64
+ TinyMongo::Helper.hashify_models_in(array))
59
65
  end
60
66
 
61
67
  def test_mongo_key
@@ -94,6 +100,7 @@ class TinyMongoTest < Test::Unit::TestCase
94
100
  obj = Dummy.new(hash)
95
101
  obj.save # save to db
96
102
  hash['_id'] = obj._id # add _id to hash
103
+ hash['_tinymongo_model_class_name'] = 'Dummy'
97
104
  result = TinyMongo.db['dummies'].find_one({'_id' => obj._id})
98
105
  assert_equal TinyMongo::Helper.stringify_keys_in_hash(hash), result # compare
99
106
  end
@@ -103,6 +110,7 @@ class TinyMongoTest < Test::Unit::TestCase
103
110
 
104
111
  obj = Dummy.create(hash) # save to db
105
112
  hash['_id'] = obj._id # add _id to hash
113
+ hash['_tinymongo_model_class_name'] = 'Dummy'
106
114
 
107
115
  result = TinyMongo.db['dummies'].find_one({'_id' => obj._id})
108
116
  assert_equal TinyMongo::Helper.stringify_keys_in_hash(hash), result, result # compare
@@ -162,43 +170,66 @@ class TinyMongoTest < Test::Unit::TestCase
162
170
  assert_equal [obj3], found2
163
171
  end
164
172
 
165
- def test_cursor_close
166
- Dummy.create
167
- cursor = Dummy.find
168
- assert_equal true, cursor.close
169
- end
170
-
171
- def test_cursor_closed?
172
- Dummy.create
173
- cursor = Dummy.find
174
- cursor.close
175
- assert_equal true, cursor.closed?
173
+ def test_find_fields
174
+ Dummy.create('foo' => 'hello', 'bar' => 'world')
175
+ found = Dummy.find({}, {'foo' => 1}).next!
176
+ assert_equal 'hello', found.foo
177
+ assert_equal nil, found.bar
176
178
  end
177
-
178
-
179
+
179
180
  def test_cursor_count
180
- Dummy.create
181
- Dummy.create
182
- Dummy.create
181
+ 3.times { Dummy.create }
183
182
  cursor = Dummy.find
184
183
  assert_equal 3, cursor.count
185
184
  end
186
185
 
187
186
  def test_cursor_limit
188
- Dummy.create
189
- Dummy.create
190
- Dummy.create
187
+ 3.times { Dummy.create }
191
188
  cursor = Dummy.find.limit(1)
192
189
  assert_equal 1, cursor.to_a.size
193
190
  end
194
191
 
195
192
  def test_cursor_limit_count
196
- Dummy.create
197
- Dummy.create
198
- Dummy.create
193
+ 3.times { Dummy.create }
199
194
  cursor = Dummy.find.limit(2)
200
195
  assert_equal 2, cursor.limit
201
196
  end
197
+
198
+ def test_cursor_size
199
+ 10.times { Dummy.create }
200
+ cursor = Dummy.find
201
+ assert_equal 10, cursor.size
202
+ end
203
+
204
+ def test_cursor_size_skip
205
+ 10.times { Dummy.create }
206
+ cursor = Dummy.find.skip(4)
207
+ assert_equal 6, cursor.size
208
+ end
209
+
210
+ def test_cursor_size_limit
211
+ 10.times { Dummy.create }
212
+ cursor = Dummy.find.limit(5)
213
+ assert_equal 5, cursor.size
214
+ end
215
+
216
+ def test_cursor_size_skip_limit
217
+ 10.times { Dummy.create }
218
+ cursor = Dummy.find.skip(3).limit(5)
219
+ assert_equal 5, cursor.size
220
+ end
221
+
222
+ def test_cursor_size_skip_too_much
223
+ 10.times { Dummy.create }
224
+ cursor = Dummy.find.skip(10)
225
+ assert_equal 0, cursor.size
226
+ end
227
+
228
+ def test_cursor_size_skip_limit_out_of_bounds
229
+ 10.times { Dummy.create }
230
+ cursor = Dummy.find.skip(7).limit(5)
231
+ assert_equal 3, cursor.size
232
+ end
202
233
 
203
234
  def test_cursor_each
204
235
  Dummy.create('foo' => 1)
@@ -224,16 +255,16 @@ class TinyMongoTest < Test::Unit::TestCase
224
255
  cursor = Dummy.find
225
256
  assert_equal false, cursor.has_next?
226
257
  end
227
-
228
- def test_cursor_next_document
258
+
259
+ def test_cursor_next!
229
260
  obj = Dummy.create
230
261
  cursor = Dummy.find
231
- assert_equal obj, cursor.next_document
262
+ assert_equal obj, cursor.next!
232
263
  end
233
264
 
234
- def test_cursor_next_document_nil
265
+ def test_cursor_next_nil
235
266
  cursor = Dummy.find
236
- assert_equal nil, cursor.next_document
267
+ assert_equal nil, cursor.next!
237
268
  end
238
269
 
239
270
  def test_cursor_skip
@@ -242,7 +273,7 @@ class TinyMongoTest < Test::Unit::TestCase
242
273
  obj = Dummy.create('foo' => 3)
243
274
 
244
275
  cursor = Dummy.find.skip(2)
245
- assert_equal obj, cursor.next_document
276
+ assert_equal obj, cursor.next!
246
277
  end
247
278
 
248
279
  def test_cursor_skip_all
@@ -251,7 +282,7 @@ class TinyMongoTest < Test::Unit::TestCase
251
282
  obj = Dummy.create('foo' => 3)
252
283
 
253
284
  cursor = Dummy.find.skip(3)
254
- assert_equal nil, cursor.next_document
285
+ assert_equal nil, cursor.next!
255
286
  end
256
287
 
257
288
  def test_cursor_skip_count
@@ -338,12 +369,7 @@ class TinyMongoTest < Test::Unit::TestCase
338
369
 
339
370
  def test_to_hash
340
371
  obj = Dummy.create('foo' => 'hello')
341
- assert_equal({'_id' => obj._id, 'foo' => 'hello'}, obj.to_hash)
342
- end
343
-
344
- def test_id
345
- obj = Dummy.create
346
- assert_equal obj._id.to_s, obj.id
372
+ assert_equal({'_id' => obj._id, 'foo' => 'hello', '_tinymongo_model_class_name' => 'Dummy'}, obj.to_hash)
347
373
  end
348
374
 
349
375
  def test_to_param
@@ -455,4 +481,15 @@ class TinyMongoTest < Test::Unit::TestCase
455
481
  assert_equal [3], TinyMongo.db['dummies'].find_one()['foo']
456
482
  end
457
483
 
484
+ def test_deserialize
485
+ Dummy.create('foo' => 'array', 'bar' => [Dummy.create('foo' => 'hello'), Dummy.create('foo' => 'world')])
486
+ d = Dummy.find_one({'foo' => 'array'})
487
+ assert_equal 'Dummy', d.class.to_s
488
+ assert_equal 'array', d.foo
489
+ assert_equal 'Dummy', d.bar[0].class.to_s
490
+ assert_equal 'hello', d.bar[0].foo
491
+ assert_equal 'Dummy', d.bar[1].class.to_s
492
+ assert_equal 'world', d.bar[1].foo
493
+ end
494
+
458
495
  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.7"
8
+ s.version = "0.1.8"
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-28}
12
+ s.date = %q{2010-07-29}
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
- - 7
9
- version: 0.1.7
8
+ - 8
9
+ version: 0.1.8
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-28 00:00:00 +09:00
17
+ date: 2010-07-29 00:00:00 +09:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency