tinymongo 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,10 @@
2
2
 
3
3
  Simple MongoDB wrapper
4
4
 
5
+ == Notice
6
+
7
+ This gem is not yet ready for production use.
8
+
5
9
  == Install
6
10
 
7
11
  gem install tinymongo
@@ -20,18 +24,18 @@ To create TinyMongo config file (config/tinymongo.yml) and initializer file (con
20
24
  == Example
21
25
 
22
26
  class Person < TinyMongo::Model
23
- mongo_collection :people # optional if using Rails
27
+ mongo_collection :people # optional if using Rails
24
28
  mongo_key :name
25
29
  mongo_key :age
26
30
  mongo_key :children
27
31
 
28
32
  def make_child
29
33
  child = Person.create(:name => 'Baby', :age => 0)
30
- push({:children => child})
34
+ push({:children => child}) # push child into children array
31
35
  end
32
36
 
33
37
  def grow_up
34
- inc({:age => 1})
38
+ inc({:age => 1}) # increments age by 1
35
39
  end
36
40
 
37
41
  def set_stuff(n,a,c)
@@ -43,6 +47,26 @@ To create TinyMongo config file (config/tinymongo.yml) and initializer file (con
43
47
  end
44
48
  end
45
49
 
50
+ Person.drop # empty drop collection
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
55
+
56
+ Person.find.each do |person|
57
+ puts person.name # print Baby and John
58
+ end
59
+
60
+ puts Person.find_one(:name => 'John').age # print 21
61
+
62
+ Person.create(:name => 'Jim')
63
+ Person.create(:name => 'Pam')
64
+
65
+ puts Person.find.to_a.map { |person| person.name } # print Baby; John; Jim; Pam
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
69
+
46
70
  == Copyright
47
71
 
48
72
  Copyright (c) 2010 Peter Jihoon Kim. See LICENSE for details.
data/Rakefile CHANGED
@@ -11,8 +11,8 @@ begin
11
11
  s.homepage = "http://github.com/petejkim/tinymongo"
12
12
  s.authors = ["Peter Jihoon Kim"]
13
13
  s.email = "raingrove@gmail.com"
14
- s.add_dependency 'mongo'
15
- s.add_dependency 'bson'
14
+ s.add_dependency(%q<mongo>, [">= 1.0.5"])
15
+ s.add_dependency(%q<bson>, [">= 1.0.4"])
16
16
  end
17
17
 
18
18
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
@@ -49,4 +49,5 @@ end
49
49
 
50
50
  require 'tinymongo/errors'
51
51
  require 'tinymongo/helper'
52
+ require 'tinymongo/cursor'
52
53
  require 'tinymongo/model'
@@ -0,0 +1,146 @@
1
+ module TinyMongo
2
+ class Cursor
3
+ include Mongo::Conversions
4
+ include Enumerable
5
+
6
+ # instance attributes in Mongo::Cursor
7
+
8
+ def initialize(cursor, model_class)
9
+ @_tinymongo_cursor = cursor
10
+ @_tinymongo_model_class = model_class
11
+ end
12
+
13
+ def mongo_cursor
14
+ @_tinymongo_cursor
15
+ end
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
+ def count
64
+ @_tinymongo_cursor.count
65
+ end
66
+
67
+ def each
68
+ num_returned = 0
69
+ while(has_next? && (@_tinymongo_cursor.instance_variable_get(:@limit) <= 0 ||
70
+ num_returned < @_tinymongo_cursor.instance_variable_get(:@limit)))
71
+ yield next_document
72
+ num_returned += 1
73
+ end
74
+ end
75
+
76
+ def explain
77
+ @_tinymongo_cursor.explain
78
+ end
79
+
80
+ def has_next?
81
+ @_tinymongo_cursor.has_next?
82
+ end
83
+
84
+ def limit(*args)
85
+ call_and_wrap_retval_in_tinymongo_cursor(:limit, args)
86
+ end
87
+
88
+ def next_document
89
+ doc = @_tinymongo_cursor.next_document
90
+ @_tinymongo_model_class.new(doc)
91
+ end
92
+
93
+ def query_options_hash
94
+ @_tinymongo_cursor.query_options_hash
95
+ end
96
+
97
+ def query_opts
98
+ @_tinymongo_cursor.query_opts
99
+ end
100
+
101
+ def skip(*args)
102
+ call_and_wrap_retval_in_tinymongo_cursor(:skip, args)
103
+ end
104
+
105
+ def sort(*args)
106
+ 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
114
+ end
115
+
116
+ call_and_wrap_retval_in_tinymongo_cursor(:sort, args)
117
+ end
118
+
119
+ def to_a
120
+ return [] if @_tinymongo_cursor.nil?
121
+
122
+ hashes = @_tinymongo_cursor.to_a
123
+ hashes.map { |hash| @_tinymongo_model_class.new(hash) }
124
+ end
125
+
126
+ protected
127
+ def call_and_wrap_retval_in_tinymongo_cursor(method_name, args)
128
+ result = @_tinymongo_cursor.send(method_name, *args)
129
+ if(result.kind_of? Mongo::Cursor)
130
+ @_tinymongo_cursor = result
131
+ self
132
+ else
133
+ result
134
+ end
135
+ end
136
+
137
+ def convert_ascending_descending_to_numeric(val)
138
+ case(val)
139
+ when 'ascending', 'asc', 1
140
+ 'ascending'
141
+ when 'descending', 'desc', -1
142
+ 'descending'
143
+ end
144
+ end
145
+ end
146
+ end
@@ -1,5 +1,3 @@
1
- require 'tinymongo/modifiers'
2
-
3
1
  module TinyMongo
4
2
  class Model
5
3
  class << self
@@ -9,13 +7,8 @@ module TinyMongo
9
7
  key_name_s = key_name.to_s
10
8
  key_name_sym = key_name.to_sym
11
9
 
12
- define_method(key_name_sym) do
13
- instance_variable_get("@_tinymongo_hash")[key_name_s]
14
- end
15
-
16
- define_method("#{key_name_s}=".to_sym) do |val|
17
- instance_variable_get("@_tinymongo_hash")[key_name_s] = val
18
- end
10
+ define_method(key_name_sym) { instance_variable_get(:@_tinymongo_hash)[key_name_s] }
11
+ define_method("#{key_name_s}=") { |val| instance_variable_get(:@_tinymongo_hash)[key_name_s] = val }
19
12
  end
20
13
  end
21
14
  end
@@ -39,23 +32,14 @@ module TinyMongo
39
32
  end
40
33
 
41
34
  def find(*args)
42
- return [] if((args.size > 0) && (args.compact.size == 0))
43
-
44
35
  new_args = args.map {|arg| Helper.hashify_models_in(arg) }
45
- cursor = collection.find(*new_args)
46
-
47
- if(cursor)
48
- hashes = cursor.to_a
49
- objs = hashes.map { |hash| self.new(hash) }
50
- end
51
-
52
- cursor ? objs : []
36
+ Cursor.new(collection.find(*new_args), self)
53
37
  end
54
38
 
55
39
  def find_one(*args)
56
- if((args.size > 0) && (args.compact.size == 0))
40
+ if((args.length > 0) && (args.compact.length == 0))
57
41
  return nil
58
- elsif((args.size == 1) && ([BSON::ObjectID, String].include? args[0].class))
42
+ elsif((args.length == 1) && ([BSON::ObjectID, String].include? args[0].class))
59
43
  hash = collection.find_one({'_id' => Helper.bson_object_id(args[0])})
60
44
  else
61
45
  new_args = args.map {|arg| Helper.hashify_models_in(arg) }
@@ -95,7 +79,7 @@ module TinyMongo
95
79
  end
96
80
 
97
81
  def initialize(hash={})
98
- @_tinymongo_hash = Helper.stringify_keys_in_hash(hash)
82
+ @_tinymongo_hash = Helper.stringify_keys_in_hash(hash) || {}
99
83
  end
100
84
 
101
85
  def _id
@@ -163,6 +147,47 @@ module TinyMongo
163
147
  delete
164
148
  end
165
149
 
166
- include Modifiers
150
+ def inc(hash={})
151
+ do_modifier_operation_and_reload('$inc', hash)
152
+ end
153
+
154
+ def set(hash={})
155
+ do_modifier_operation_and_reload('$set', hash)
156
+ end
157
+
158
+ def unset(hash={})
159
+ do_modifier_operation_and_reload('$unset', hash)
160
+ end
161
+
162
+ def push(hash={})
163
+ do_modifier_operation_and_reload('$push', hash)
164
+ end
165
+
166
+ def push_all(hash={})
167
+ do_modifier_operation_and_reload('$pushAll', hash)
168
+ end
169
+
170
+ def add_to_set(hash={})
171
+ do_modifier_operation_and_reload('$addToSet', hash)
172
+ end
173
+
174
+ def pop(hash={})
175
+ do_modifier_operation_and_reload('$pop', hash)
176
+ end
177
+
178
+ def pull(hash={})
179
+ do_modifier_operation_and_reload('$pull', hash)
180
+ end
181
+
182
+ def pull_all(hash={})
183
+ do_modifier_operation_and_reload('$pullAll', hash)
184
+ end
185
+
186
+ protected
187
+ def do_modifier_operation_and_reload(operator, hash)
188
+ raise ModifierOperationError unless self._id
189
+ collection.update({ '_id' => self._id }, { operator => Helper.hashify_models_in_hash(hash) })
190
+ reload
191
+ end
167
192
  end
168
193
  end
@@ -104,20 +104,20 @@ class TinyMongoTest < Test::Unit::TestCase
104
104
  end
105
105
 
106
106
  def test_find_nothing
107
- found = Dummy.find()
107
+ found = Dummy.find().to_a
108
108
  assert_equal [], found
109
109
  end
110
110
 
111
111
  def test_find_all_one
112
112
  obj = Dummy.create('foo' => 'hello')
113
- found = Dummy.find()
113
+ found = Dummy.find().to_a
114
114
  assert_equal [obj], found
115
115
  end
116
116
 
117
117
  def test_find_all_many
118
118
  obj = Dummy.create('foo' => 'hello')
119
119
  obj2 = Dummy.create('foo' => 'hello')
120
- found = Dummy.find()
120
+ found = Dummy.find().to_a
121
121
  assert_equal [obj, obj2], found
122
122
  end
123
123
 
@@ -125,12 +125,121 @@ class TinyMongoTest < Test::Unit::TestCase
125
125
  obj = Dummy.create('foo' => 'hello')
126
126
  obj2 = Dummy.create('foo' => 'hello')
127
127
  obj3 = Dummy.create('foo' => 'bye')
128
- found1 = Dummy.find({'foo' => 'hello'})
129
- found2 = Dummy.find({'foo' => 'bye'})
128
+ found1 = Dummy.find({'foo' => 'hello'}).to_a
129
+ found2 = Dummy.find({'foo' => 'bye'}).to_a
130
130
  assert_equal [obj, obj2], found1
131
131
  assert_equal [obj3], found2
132
132
  end
133
133
 
134
+ def test_cursor_close
135
+ Dummy.create
136
+ cursor = Dummy.find
137
+ assert_equal true, cursor.close
138
+ end
139
+
140
+ def test_cursor_closed?
141
+ Dummy.create
142
+ cursor = Dummy.find
143
+ cursor.close
144
+ assert_equal true, cursor.closed?
145
+ end
146
+
147
+
148
+ def test_cursor_count
149
+ Dummy.create
150
+ Dummy.create
151
+ Dummy.create
152
+ cursor = Dummy.find
153
+ assert_equal 3, cursor.count
154
+ end
155
+
156
+ def test_cursor_limit
157
+ Dummy.create
158
+ Dummy.create
159
+ Dummy.create
160
+ cursor = Dummy.find.limit(1)
161
+ assert_equal 1, cursor.to_a.size
162
+ end
163
+
164
+ def test_cursor_limit_count
165
+ Dummy.create
166
+ Dummy.create
167
+ Dummy.create
168
+ cursor = Dummy.find.limit(2)
169
+ assert_equal 2, cursor.limit
170
+ end
171
+
172
+ def test_cursor_each
173
+ Dummy.create('foo' => 1)
174
+ Dummy.create('foo' => 2)
175
+ Dummy.create('foo' => 3)
176
+
177
+ cursor = Dummy.find
178
+ num = 0
179
+ cursor.each do |x|
180
+ num += x.foo
181
+ end
182
+
183
+ assert_equal 6, num
184
+ end
185
+
186
+ def test_cursor_has_next?
187
+ Dummy.create
188
+ cursor = Dummy.find
189
+ assert_equal true, cursor.has_next?
190
+ end
191
+
192
+ def test_cursor_next_document
193
+ obj = Dummy.create
194
+ cursor = Dummy.find
195
+ assert_equal obj, cursor.next_document
196
+ end
197
+
198
+ def test_cursor_skip
199
+ Dummy.create('foo' => 1)
200
+ Dummy.create('foo' => 2)
201
+ obj = Dummy.create('foo' => 3)
202
+
203
+ cursor = Dummy.find.skip(2)
204
+ assert_equal obj, cursor.next_document
205
+ end
206
+
207
+ def test_cursor_skip_count
208
+ Dummy.create('foo' => 1)
209
+ Dummy.create('foo' => 2)
210
+ obj = Dummy.create('foo' => 3)
211
+
212
+ cursor = Dummy.find.skip(2)
213
+ assert_equal 2, cursor.skip
214
+ end
215
+
216
+ def test_cursor_to_a
217
+ obj1 = Dummy.create
218
+ obj2 = Dummy.create
219
+ obj3 = Dummy.create
220
+
221
+ cursor = Dummy.find
222
+ assert_equal [obj1, obj2, obj3], cursor.to_a
223
+ end
224
+
225
+ def test_cursor_sort
226
+ obj1 = Dummy.create('foo' => 3)
227
+ obj2 = Dummy.create('foo' => 2)
228
+ obj3 = Dummy.create('foo' => 1)
229
+
230
+ cursor = Dummy.find.sort(['foo', 'ascending'])
231
+ assert_equal [obj3, obj2, obj1], cursor.to_a
232
+ end
233
+
234
+ def test_cursor_sort_hash
235
+ obj1 = Dummy.create('foo' => 3)
236
+ obj2 = Dummy.create('foo' => 2)
237
+ obj3 = Dummy.create('foo' => 1)
238
+
239
+ cursor = Dummy.find.sort({'foo' => 1})
240
+ assert_equal [obj3, obj2, obj1], cursor.to_a
241
+ end
242
+
134
243
  def test_find_one
135
244
  obj = Dummy.create('foo' => 'hello')
136
245
  found = Dummy.find_one()
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{tinymongo}
8
- s.version = "0.1.4"
8
+ s.version = "0.1.5"
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-22}
12
+ s.date = %q{2010-07-23}
13
13
  s.description = %q{Simple MongoDB wrapper}
14
14
  s.email = %q{raingrove@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -28,10 +28,10 @@ Gem::Specification.new do |s|
28
28
  "lib/generators/templates/tinymongo.yml.erb",
29
29
  "lib/generators/tinymongo_generator.rb",
30
30
  "lib/tinymongo.rb",
31
+ "lib/tinymongo/cursor.rb",
31
32
  "lib/tinymongo/errors.rb",
32
33
  "lib/tinymongo/helper.rb",
33
34
  "lib/tinymongo/model.rb",
34
- "lib/tinymongo/modifiers.rb",
35
35
  "test/test_helper.rb",
36
36
  "test/test_tinymongo.rb",
37
37
  "tinymongo.gemspec"
@@ -51,15 +51,15 @@ Gem::Specification.new do |s|
51
51
  s.specification_version = 3
52
52
 
53
53
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
- s.add_runtime_dependency(%q<mongo>, [">= 0"])
55
- s.add_runtime_dependency(%q<bson>, [">= 0"])
54
+ s.add_runtime_dependency(%q<mongo>, [">= 1.0.5"])
55
+ s.add_runtime_dependency(%q<bson>, [">= 1.0.4"])
56
56
  else
57
- s.add_dependency(%q<mongo>, [">= 0"])
58
- s.add_dependency(%q<bson>, [">= 0"])
57
+ s.add_dependency(%q<mongo>, [">= 1.0.5"])
58
+ s.add_dependency(%q<bson>, [">= 1.0.4"])
59
59
  end
60
60
  else
61
- s.add_dependency(%q<mongo>, [">= 0"])
62
- s.add_dependency(%q<bson>, [">= 0"])
61
+ s.add_dependency(%q<mongo>, [">= 1.0.5"])
62
+ s.add_dependency(%q<bson>, [">= 1.0.4"])
63
63
  end
64
64
  end
65
65
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 4
9
- version: 0.1.4
8
+ - 5
9
+ version: 0.1.5
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-22 00:00:00 +09:00
17
+ date: 2010-07-23 00:00:00 +09:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -26,8 +26,10 @@ dependencies:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  segments:
29
+ - 1
29
30
  - 0
30
- version: "0"
31
+ - 5
32
+ version: 1.0.5
31
33
  type: :runtime
32
34
  version_requirements: *id001
33
35
  - !ruby/object:Gem::Dependency
@@ -39,8 +41,10 @@ dependencies:
39
41
  - - ">="
40
42
  - !ruby/object:Gem::Version
41
43
  segments:
44
+ - 1
42
45
  - 0
43
- version: "0"
46
+ - 4
47
+ version: 1.0.4
44
48
  type: :runtime
45
49
  version_requirements: *id002
46
50
  description: Simple MongoDB wrapper
@@ -64,10 +68,10 @@ files:
64
68
  - lib/generators/templates/tinymongo.yml.erb
65
69
  - lib/generators/tinymongo_generator.rb
66
70
  - lib/tinymongo.rb
71
+ - lib/tinymongo/cursor.rb
67
72
  - lib/tinymongo/errors.rb
68
73
  - lib/tinymongo/helper.rb
69
74
  - lib/tinymongo/model.rb
70
- - lib/tinymongo/modifiers.rb
71
75
  - test/test_helper.rb
72
76
  - test/test_tinymongo.rb
73
77
  - tinymongo.gemspec
@@ -1,46 +0,0 @@
1
- module TinyMongo
2
- module Modifiers
3
- def inc(hash={})
4
- do_modifier_operation_and_reload('$inc', hash)
5
- end
6
-
7
- def set(hash={})
8
- do_modifier_operation_and_reload('$set', hash)
9
- end
10
-
11
- def unset(hash={})
12
- do_modifier_operation_and_reload('$unset', hash)
13
- end
14
-
15
- def push(hash={})
16
- do_modifier_operation_and_reload('$push', hash)
17
- end
18
-
19
- def push_all(hash={})
20
- do_modifier_operation_and_reload('$pushAll', hash)
21
- end
22
-
23
- def add_to_set(hash={})
24
- do_modifier_operation_and_reload('$addToSet', hash)
25
- end
26
-
27
- def pop(hash={})
28
- do_modifier_operation_and_reload('$pop', hash)
29
- end
30
-
31
- def pull(hash={})
32
- do_modifier_operation_and_reload('$pull', hash)
33
- end
34
-
35
- def pull_all(hash={})
36
- do_modifier_operation_and_reload('$pullAll', hash)
37
- end
38
-
39
- protected
40
- def do_modifier_operation_and_reload(operator, hash)
41
- raise ModifierOperationError unless self._id
42
- collection.update({ '_id' => self._id }, { operator => Helper.hashify_models_in_hash(hash) })
43
- reload
44
- end
45
- end
46
- end