tinymongo 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -73,6 +73,11 @@ To create TinyMongo config file (config/tinymongo.yml) and initializer file (con
73
73
  Person.find.count # count ignores skip and limit
74
74
  Person.find.skip(1).size # size is affected by skip and limit
75
75
 
76
+ Person.distinct(:name)
77
+
78
+ Person.ensure_index({:name => 1})
79
+ Person.drop_index({:name => 1})
80
+
76
81
  == Copyright
77
82
 
78
83
  Copyright (c) 2010 Peter Jihoon Kim. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.8
1
+ 0.1.9
@@ -2,7 +2,7 @@ require 'mongo'
2
2
 
3
3
  module TinyMongo
4
4
  class << self
5
- def configure(config)
5
+ def configure(config={})
6
6
  config = Helper.stringify_keys_in_hash(config)
7
7
 
8
8
  @host = config['host'] || 'localhost'
@@ -48,8 +48,8 @@ module TinyMongo
48
48
  has_next?
49
49
  end
50
50
 
51
- def limit(*args)
52
- call_and_wrap_retval_in_tinymongo_cursor(:limit, args)
51
+ def limit(number_to_return=nil)
52
+ call_and_wrap_retval_in_tinymongo_cursor(:limit, [number_to_return])
53
53
  end
54
54
 
55
55
  def next!
@@ -65,16 +65,18 @@ module TinyMongo
65
65
  next!
66
66
  end
67
67
 
68
- def skip(*args)
69
- call_and_wrap_retval_in_tinymongo_cursor(:skip, args)
68
+ def skip(number_to_skip=nil)
69
+ call_and_wrap_retval_in_tinymongo_cursor(:skip, [number_to_skip])
70
70
  end
71
71
 
72
- def sort(*args)
73
- if(args.length > 0 && (args[0].instance_of? Hash))
74
- args[0] = args[0].map { |k, v| [k, convert_ascending_descending_to_numeric(v)] }
72
+ def sort(key_or_list, direction=nil)
73
+ if(key_or_list.kind_of? Hash)
74
+ key_or_list = key_or_list.map { |k, v| [k.to_s, convert_ascending_descending(v)] }
75
+ elsif(key_or_list.kind_of? Array)
76
+ key_or_list = key_or_list.map { |o| (o.kind_of? Array) ? [o[0].to_s, convert_ascending_descending(o[1])] : nil }.compact
75
77
  end
76
78
 
77
- call_and_wrap_retval_in_tinymongo_cursor(:sort, args)
79
+ call_and_wrap_retval_in_tinymongo_cursor(:sort, [key_or_list, direction])
78
80
  end
79
81
 
80
82
  def to_a
@@ -95,12 +97,14 @@ module TinyMongo
95
97
  end
96
98
  end
97
99
 
98
- def convert_ascending_descending_to_numeric(val)
99
- case(val)
100
- when 'ascending', 'asc', 1
101
- 'ascending'
102
- when 'descending', 'desc', -1
103
- 'descending'
100
+ def convert_ascending_descending(val)
101
+ case(val.to_s)
102
+ when 'ascending', 'asc', '1', Mongo::ASCENDING.to_s
103
+ Mongo::ASCENDING
104
+ when 'descending', 'desc', '-1', Mongo::DESCENDING.to_s
105
+ Mongo::DESCENDING
106
+ else
107
+ val
104
108
  end
105
109
  end
106
110
  end
@@ -6,7 +6,7 @@ module TinyMongo
6
6
  @_tinymongo_defaults = {} if @_tinymongo_defaults.nil?
7
7
 
8
8
  args.each do |arg|
9
- default = arg[:default] || arg['default'] if(arg.instance_of? Hash)
9
+ default = arg[:default] || arg['default'] if(arg.kind_of? Hash)
10
10
  end
11
11
 
12
12
  args.each do |arg|
@@ -40,6 +40,18 @@ module TinyMongo
40
40
  end
41
41
  end
42
42
 
43
+ def full_name
44
+ "#{db.name}.#{collection.name}"
45
+ end
46
+
47
+ def get_full_name
48
+ full_name
49
+ end
50
+
51
+ def getFullName
52
+ full_name
53
+ end
54
+
43
55
  def find(query={}, fields=nil, limit=nil, skip=nil)
44
56
  query = Helper.hashify_models_in(query)
45
57
  fields = Helper.hashify_models_in(fields)
@@ -76,8 +88,12 @@ module TinyMongo
76
88
  collection.remove({ '_id' => Helper.bson_object_id(id)})
77
89
  end
78
90
 
79
- def destroy(id)
80
- delete(id)
91
+ def destroy(*args)
92
+ delete(*args)
93
+ end
94
+
95
+ def remove(*args)
96
+ delete(*args)
81
97
  end
82
98
 
83
99
  def drop
@@ -95,10 +111,69 @@ module TinyMongo
95
111
  def count
96
112
  collection.count
97
113
  end
114
+
115
+ def ensure_index(keys, options={})
116
+ if(keys.kind_of? Hash)
117
+ keys = keys.map { |k,v| [k.to_s, convert_ascending_descending_2d(v)]}
118
+ elsif(keys.kind_of? Array)
119
+ keys = keys.map { |o| (o.kind_of? Array) ? [o[0].to_s, convert_ascending_descending_2d(o[1])] : nil }.compact
120
+ end
121
+ options = Helper.symbolify_keys_in_hash(options)
122
+ collection.create_index(keys, options)
123
+ end
124
+
125
+ def ensureIndex(*args)
126
+ ensure_index(*args)
127
+ end
128
+
129
+ def drop_index(obj)
130
+ if(obj.instance_of? String)
131
+ index = obj
132
+ elsif(obj.kind_of? Hash)
133
+ index = get_index_name(obj)
134
+ elsif(obj.kind_of? Array)
135
+ index = get_index_name(Hash[obj])
136
+ else
137
+ return
138
+ end
139
+
140
+ collection.drop_index(index)
141
+ end
142
+
143
+ def dropIndex(*args)
144
+ drop_index(*args)
145
+ end
146
+
147
+ def drop_indexes
148
+ collection.drop_indexes
149
+ end
150
+
151
+ def dropIndexes
152
+ drop_indexes
153
+ end
154
+
155
+ def get_indexes
156
+ collection.index_information.map { |k,v| v }
157
+ end
158
+
159
+ def getIndexes
160
+ get_indexes
161
+ end
162
+
163
+ def distinct(key, query=nil)
164
+ if(query.kind_of? Hash)
165
+ query = Helper.hashify_models_in(query)
166
+ end
167
+ collection.distinct(key, query)
168
+ end
98
169
  end
99
170
 
100
171
  def initialize(hash={})
101
- @_tinymongo_hash = (Helper.deep_copy(self.class.instance_variable_get(:@_tinymongo_defaults)).merge(Helper.stringify_keys_in_hash(hash)) || {}) if hash
172
+ @_tinymongo_hash = {}
173
+ defaults = self.class.instance_variable_get(:@_tinymongo_defaults)
174
+ defaults = self.class.instance_variable_set(:@_tinymongo_defaults, {}) if(defaults.nil?)
175
+ hash = Helper.deep_copy(defaults).merge(Helper.stringify_keys_in_hash(hash || {}))
176
+ hash.each_pair { |key, value| set_using_setter(key, value) }
102
177
  set_tinymongo_model_class_name_in_hash(@_tinymongo_hash)
103
178
  self
104
179
  end
@@ -153,16 +228,16 @@ module TinyMongo
153
228
  return self
154
229
  end
155
230
 
156
- def update_attribute(name, value)
157
- send(name.to_s + '=', value)
231
+ def update_attribute(key, value)
232
+ set_using_setter(key, value)
158
233
  save
159
234
  end
160
235
 
161
236
  def update_attributes(hash={})
162
- hash.each_pair { |key, value| send(key.to_s + '=', value) }
237
+ hash.each_pair { |key, value| set_using_setter(key, value) }
163
238
  save
164
239
  end
165
-
240
+
166
241
  def delete
167
242
  if(@_tinymongo_hash['_id'])
168
243
  collection.remove({ '_id' => @_tinymongo_hash['_id'] })
@@ -241,5 +316,45 @@ module TinyMongo
241
316
  end
242
317
  fields
243
318
  end
319
+
320
+ def set_using_setter(key, value)
321
+ setter = key.to_s + '='
322
+ self.send(setter, value) if(self.respond_to? setter)
323
+ end
324
+
325
+ def self.convert_ascending_descending_2d(val)
326
+ case(val.to_s)
327
+ when 'ascending', 'asc', '1', Mongo::ASCENDING.to_s
328
+ Mongo::ASCENDING
329
+ when 'descending', 'desc', '-1', Mongo::DESCENDING.to_s
330
+ Mongo::DESCENDING
331
+ when '2d', Mongo::GEO2D.to_s
332
+ Mongo::GEO2D
333
+ else
334
+ val
335
+ end
336
+ end
337
+
338
+ def self.convert_ascending_descending_to_numeric(val)
339
+ case(val.to_s)
340
+ when 'ascending', 'asc', '1', Mongo::ASCENDING.to_s
341
+ 1
342
+ when 'descending', 'desc', '-1', Mongo::DESCENDING.to_s
343
+ -1
344
+ else
345
+ val
346
+ end
347
+ end
348
+
349
+ def self.get_index_name(hash)
350
+ name = ''
351
+ hash.each_pair do |k,v|
352
+ name += '_' if(name.length > 0)
353
+ name += k.to_s + '_'
354
+ v = convert_ascending_descending_to_numeric(v)
355
+ name += v.to_s if(v.kind_of? Integer)
356
+ end
357
+ name
358
+ end
244
359
  end
245
360
  end
@@ -21,7 +21,7 @@ end
21
21
 
22
22
  class TinyMongoTest < Test::Unit::TestCase
23
23
  def setup
24
- TinyMongo.db['dummies'].drop()
24
+ TinyMongo.db['dummies'].drop
25
25
  end
26
26
 
27
27
  def test_helper_stringify_keys_in_hash
@@ -116,6 +116,13 @@ class TinyMongoTest < Test::Unit::TestCase
116
116
  assert_equal TinyMongo::Helper.stringify_keys_in_hash(hash), result, result # compare
117
117
  end
118
118
 
119
+ def test_create_ignore_undefined_keys
120
+ obj = Dummy.create({'foo' => 'hello', 'bar' => 'world', 'fubar' => 'snafu'}) # save to db
121
+
122
+ result = TinyMongo.db['dummies'].find_one({'_id' => obj._id})
123
+ assert_equal TinyMongo::Helper.stringify_keys_in_hash({'foo' => 'hello', 'bar' => 'world', '_id' => obj._id, '_tinymongo_model_class_name' => 'Dummy'}), result, result # compare
124
+ end
125
+
119
126
  def test_save_update
120
127
  obj = Dummy.create('foo' => 'hello')
121
128
  obj.foo = 'bye'
@@ -313,10 +320,19 @@ class TinyMongoTest < Test::Unit::TestCase
313
320
  obj2 = Dummy.create('foo' => 2)
314
321
  obj3 = Dummy.create('foo' => 1)
315
322
 
316
- cursor = Dummy.find.sort(['foo', 'ascending'])
323
+ cursor = Dummy.find.sort('foo')
317
324
  assert_equal [obj3, obj2, obj1], cursor.to_a
318
325
  end
319
326
 
327
+ def test_cursor_sort_array
328
+ obj1 = Dummy.create('foo' => 3)
329
+ obj2 = Dummy.create('foo' => 2)
330
+ obj3 = Dummy.create('foo' => 1)
331
+
332
+ cursor = Dummy.find.sort([['foo', 'ascending']])
333
+ assert_equal [obj3, obj2, obj1], cursor.to_a
334
+ end
335
+
320
336
  def test_cursor_sort_hash
321
337
  obj1 = Dummy.create('foo' => 3)
322
338
  obj2 = Dummy.create('foo' => 2)
@@ -492,4 +508,64 @@ class TinyMongoTest < Test::Unit::TestCase
492
508
  assert_equal 'world', d.bar[1].foo
493
509
  end
494
510
 
511
+ def test_full_name
512
+ assert_equal 'tinymongo_test.dummies', Dummy.full_name
513
+ end
514
+
515
+ def test_get_indexes
516
+ Dummy.create
517
+ assert_equal [{'name' => '_id_', 'ns' => 'tinymongo_test.dummies', 'key' => {'_id' => 1}}], Dummy.get_indexes
518
+ end
519
+
520
+ def test_ensure_index
521
+ Dummy.create
522
+ assert_equal 'foo_1', Dummy.ensure_index({'foo' => 1})
523
+ assert_equal 'foo_1_bar_-1', Dummy.ensure_index({'foo' => 1, 'bar' => -1})
524
+ assert_equal [{"name" => "_id_", "ns" => "tinymongo_test.dummies", "key" => {"_id" => 1}},
525
+ {"name" => "foo_1", "ns" => "tinymongo_test.dummies", "key" => {"foo" => 1}},
526
+ {"name" => "foo_1_bar_-1", "ns" => "tinymongo_test.dummies", "key" => {"foo" => 1, "bar" => -1}}],
527
+ Dummy.get_indexes
528
+ end
529
+
530
+ def test_drop_index
531
+ Dummy.create
532
+ index_name = Dummy.ensure_index({'foo' => 1})
533
+ assert_equal [{"name" => "_id_", "ns" => "tinymongo_test.dummies", "key" => {"_id" => 1}},
534
+ {"name" => "foo_1", "ns" => "tinymongo_test.dummies", "key" => {"foo" => 1}}],
535
+ Dummy.get_indexes
536
+ Dummy.drop_index(index_name)
537
+ assert_equal [{'name' => '_id_', 'ns' => 'tinymongo_test.dummies', 'key' => {'_id' => 1}}], Dummy.get_indexes
538
+ end
539
+
540
+ def test_drop_index_hash
541
+ Dummy.create
542
+ index_name = Dummy.ensure_index({'foo' => 1, 'bar' => -1})
543
+ Dummy.drop_index({'foo' => 1, 'bar' => -1})
544
+ assert_equal [{'name' => '_id_', 'ns' => 'tinymongo_test.dummies', 'key' => {'_id' => 1}}], Dummy.get_indexes
545
+ end
546
+
547
+ def test_drop_index_array
548
+ Dummy.create
549
+ index_name = Dummy.ensure_index({'foo' => 1, 'bar' => -1})
550
+ Dummy.drop_index([['foo', 1], ['bar', -1]])
551
+ assert_equal [{'name' => '_id_', 'ns' => 'tinymongo_test.dummies', 'key' => {'_id' => 1}}], Dummy.get_indexes
552
+ end
553
+
554
+ def test_drop_indexes
555
+ Dummy.create
556
+ Dummy.ensure_index({'foo' => 1})
557
+ Dummy.ensure_index({'bar' => 1})
558
+ Dummy.drop_indexes
559
+ assert_equal [{'name' => '_id_', 'ns' => 'tinymongo_test.dummies', 'key' => {'_id' => 1}}], Dummy.get_indexes
560
+ end
561
+
562
+ def test_distinct
563
+ Dummy.create('foo' => 'hello', 'bar' => 'hello')
564
+ Dummy.create('foo' => 'hello', 'bar' => 'world')
565
+ Dummy.create('foo' => 'world', 'bar' => 'world')
566
+ Dummy.create('foo' => 'world', 'bar' => 'world')
567
+ assert_equal ['hello','world'], Dummy.distinct('foo')
568
+ assert_equal ['hello'], Dummy.distinct('foo', {'bar' => 'hello'})
569
+ assert_equal ['hello','world'], Dummy.distinct('foo', {'bar' => 'world'})
570
+ end
495
571
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{tinymongo}
8
- s.version = "0.1.8"
8
+ s.version = "0.1.9"
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"]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 8
9
- version: 0.1.8
8
+ - 9
9
+ version: 0.1.9
10
10
  platform: ruby
11
11
  authors:
12
12
  - Peter Jihoon Kim