tinymongo 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010 Peter Jihoon Kim
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,48 @@
1
+ = TinyMongo
2
+
3
+ Simple MongoDB wrapper
4
+
5
+ == Install
6
+
7
+ gem install tinymongo
8
+
9
+ == Rails Setup
10
+
11
+ To create TinyMongo config file (config/tinymongo.yml), do the following:
12
+
13
+ rails generate tinymongo
14
+
15
+ == Connecting To MongoDB directly (for non-Rails projects)
16
+
17
+ TinyMongo.configure({:host => 'localhost', :database => 'test'})
18
+ TinyMongo.connect
19
+
20
+ == Example
21
+
22
+ class Person < TinyMongo::Model
23
+ mongo_collection :people # optional if using Rails
24
+ mongo_key :name
25
+ mongo_key :age
26
+ mongo_key :children
27
+
28
+ def make_child
29
+ child = Person.new(:name => 'Baby', :age => 0)
30
+
31
+ self.push({:children => child})
32
+ end
33
+
34
+ def grow_up
35
+ self.inc({:age => 1})
36
+ end
37
+
38
+ def set_stuff(n,a,c)
39
+ name = n
40
+ age = a
41
+ children = c
42
+ save
43
+ end
44
+ end
45
+
46
+ == Copyright
47
+
48
+ Copyright (c) 2010 Peter Jihoon Kim. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ begin
5
+ require 'jeweler'
6
+
7
+ Jeweler::Tasks.new do |s|
8
+ s.name = "tinymongo"
9
+ s.summary = "Simple MongoDB wrapper"
10
+ s.description = s.summary
11
+ s.homepage = "http://github.com/petejkim/tinymongo"
12
+ s.authors = ["Peter Jihoon Kim"]
13
+ s.email = "raingrove@gmail.com"
14
+ s.add_dependency 'mongo'
15
+ s.add_dependency 'bson'
16
+ end
17
+
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler - or one of its dependencies - is not available. " <<
21
+ "Install it with: sudo gem install jeweler -s http://gemcutter.org"
22
+ end
23
+
24
+ Rake::TestTask.new do |t|
25
+ t.libs << 'lib'
26
+ t.pattern = 'test/**/test_*.rb'
27
+ t.verbose = true
28
+ end
29
+
30
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'tinymongo'
2
+
3
+ TinyMongo.configure(YAML.load_file(Rails.root + 'config' + 'tinymongo.yml')[Rails.env])
4
+ TinyMongo.connect
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Creates TinyMongo config file
3
+
4
+ Example:
5
+ rails generate tinymongo
6
+
7
+ This will create:
8
+ config/tinymongo.yml
@@ -0,0 +1,23 @@
1
+ development:
2
+ host: localhost
3
+ port:
4
+ options:
5
+ database: <%= application_name %>_development
6
+ username:
7
+ password:
8
+
9
+ test:
10
+ host: localhost
11
+ port:
12
+ options:
13
+ database: <%= application_name %>_test
14
+ username:
15
+ password:
16
+
17
+ production:
18
+ host: localhost
19
+ port:
20
+ options:
21
+ database: <%= application_name %>_production
22
+ username:
23
+ password:
@@ -0,0 +1,16 @@
1
+ class TinymongoGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def generate_config
5
+ template "tinymongo.yml.erb", "config/tinymongo.yml"
6
+ end
7
+
8
+ private
9
+ def application_name # from Rails::Generators::NamedBase
10
+ if defined?(Rails) && Rails.application
11
+ Rails.application.class.name.split('::').first.underscore
12
+ else
13
+ "application"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,62 @@
1
+ module TinyMongo
2
+ module Helper
3
+ class << self
4
+ def stringify_keys_in_hash(hash)
5
+ new_hash = {}
6
+ hash.each_pair { |key, value| new_hash[key.to_s] = value }
7
+ new_hash['_id'] = bson_object_id(new_hash['_id']) if(new_hash['_id'])
8
+ new_hash
9
+ end
10
+
11
+ def symbolify_keys_in_hash(hash)
12
+ new_hash = {}
13
+ hash.each_pair { |key, value| new_hash[key.to_sym] = value }
14
+ new_hash[:_id] = bson_object_id(new_hash[:_id]) if(new_hash[:_id])
15
+ new_hash
16
+ end
17
+
18
+ def hashify_models_in(obj)
19
+ if(obj.instance_of? Hash)
20
+ hashify_models_in_hash(obj)
21
+ elsif(obj.instance_of? Array)
22
+ hashify_models_in_array(obj)
23
+ elsif(obj.kind_of? TinyMongo::Model)
24
+ obj.to_hash
25
+ else
26
+ obj
27
+ end
28
+ end
29
+
30
+ def hashify_models_in_array(array)
31
+ new_array = []
32
+ array.each do |value|
33
+ new_array << hashify_models_in(value)
34
+ end
35
+ new_array
36
+ end
37
+
38
+ def hashify_models_in_hash(hash)
39
+ new_hash = {}
40
+ hash.each_pair do |key,value|
41
+ key_s = key.to_s
42
+ if(key_s == '_id')
43
+ new_hash[key_s] = bson_object_id(value)
44
+ else
45
+ new_hash[key_s] = hashify_models_in(value)
46
+ end
47
+ end
48
+ new_hash
49
+ end
50
+
51
+ def bson_object_id(id)
52
+ if(id.instance_of? BSON::ObjectID)
53
+ id
54
+ elsif(id.instance_of? String)
55
+ BSON::ObjectID(id)
56
+ else
57
+ BSON::ObjectID(id.to_s)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,276 @@
1
+ module TinyMongo
2
+ class Model
3
+ class << self
4
+ def mongo_key(*args)
5
+ args.each do |key_name|
6
+ if([Symbol, String].include? key_name.class)
7
+ key_name_s = key_name.to_s
8
+ key_name_sym = key_name.to_sym
9
+
10
+ define_method(key_name_sym) do
11
+ instance_variable_get("@_tinymongo_hash")[key_name_s]
12
+ end
13
+
14
+ define_method("#{key_name_s}=".to_sym) do |val|
15
+ instance_variable_get("@_tinymongo_hash")[key_name_s] = val
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ def mongo_collection(name)
22
+ @_tinymongo_collection_name = name.to_s
23
+ end
24
+
25
+ def db
26
+ TinyMongo.db
27
+ end
28
+
29
+ def collection
30
+ if @_tinymongo_collection_name
31
+ TinyMongo.db[@_tinymongo_collection_name]
32
+ elsif(defined?(Rails))
33
+ TinyMongo.db[self.to_s.gsub('/','_').tableize]
34
+ else
35
+ TinyMongo.db[self.to_s]
36
+ end
37
+ end
38
+
39
+ def find(*args)
40
+ new_args = []
41
+ args.each do |arg|
42
+ new_args << Helper.hashify_models_in(arg)
43
+ end
44
+ collection.find(*new_args)
45
+ end
46
+
47
+ def find_one(*args)
48
+ if([BSON::ObjectID, String].include? args[0].class)
49
+ self.new(collection.find_one({'_id' => Helper.bson_object_id(args[0])}))
50
+ else
51
+ self.new(collection.find_one(*args))
52
+ end
53
+ end
54
+
55
+ def create(hash)
56
+ obj = self.new(hash)
57
+ obj.save
58
+ end
59
+
60
+ def delete(id)
61
+ collection.remove({ '_id' => Helper.bson_object_id(id)})
62
+ end
63
+
64
+ def delete_all
65
+ collection.drop
66
+ end
67
+
68
+ def destroy_all
69
+ delete_all
70
+ end
71
+
72
+ def count
73
+ collection.count
74
+ end
75
+ end
76
+
77
+ def _id
78
+ @_tinymongo_hash['_id']
79
+ end
80
+
81
+ def _id=(val)
82
+ @_tinymongo_hash['_id'] = Helper.bson_object_id(val)
83
+ end
84
+
85
+ def initialize(hash={})
86
+ @_tinymongo_hash = Helper.stringify_keys_in_hash(hash)
87
+ end
88
+
89
+ def db
90
+ self.db
91
+ end
92
+
93
+ def collection
94
+ self.class.collection
95
+ end
96
+
97
+ def to_hash
98
+ @_tinymongo_hash.clone
99
+ end
100
+
101
+ def save(v=true)
102
+
103
+ if((v && (defined?(Rails) && self.valid?) || (!defined?(Rails))) || (!v))
104
+ begin
105
+ obj = create_or_update
106
+ return obj
107
+ rescue
108
+ return false
109
+ end
110
+ else
111
+ return false
112
+ end
113
+ end
114
+
115
+ def update_attribute(name, value)
116
+ send(name.to_s + '=', value)
117
+ save(false)
118
+ end
119
+
120
+ def update_attributes(hash)
121
+ hash.each_pair { |key, value| send(key.to_s + '=', value) }
122
+ save
123
+ end
124
+
125
+ def delete
126
+ if(@_tinymongo_hash['_id'])
127
+ collection.remove({ '_id' => @_tinymongo_hash['_id'] })
128
+ end
129
+ end
130
+
131
+ def destroy
132
+ delete
133
+ end
134
+
135
+ def destroy(id)
136
+ delete(id)
137
+ end
138
+
139
+ def inc(hash)
140
+ hash.each_pair do |key, value|
141
+ key = key.to_s
142
+
143
+ if(@_tinymongo_hash[key] && (@_tinymongo_hash[key].kind_of? Numeric))
144
+ send(key + '=', (send(key) + value))
145
+ else
146
+ send(key + '=', value)
147
+ end
148
+ end
149
+
150
+ if(@_tinymongo_hash['_id'])
151
+ collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$inc' => Helper.hashify_models_in_hash(hash) })
152
+ end
153
+ end
154
+
155
+ def set(hash)
156
+ hash.each_pair { |key, value| send(key.to_s + '=', value) }
157
+
158
+ if(@_tinymongo_hash['_id'])
159
+ collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$set' => Helper.hashify_models_in_hash(hash) })
160
+ end
161
+ end
162
+
163
+ def unset(hash)
164
+ hash.each_key do |key|
165
+ @_tinymongo_hash.delete(key.to_s)
166
+ end
167
+
168
+ if(@_tinymongo_hash['_id'])
169
+ collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$unset' => Helper.hashify_models_in_hash(hash) })
170
+ end
171
+ end
172
+
173
+ def push(hash)
174
+ hash.each_pair do |key, value|
175
+ key = key.to_s
176
+
177
+ if(@_tinymongo_hash[key] && (@_tinymongo_hash[key].instance_of? Array))
178
+ send(key) << value
179
+ else
180
+ send(key + '=', value)
181
+ end
182
+ end
183
+
184
+ if(@_tinymongo_hash['_id'])
185
+ collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$push' => Helper.hashify_models_in_hash(hash) })
186
+ end
187
+ end
188
+
189
+ def push_all(hash)
190
+ hash.each_pair do |key, value|
191
+ key = key.to_s
192
+
193
+ if(@_tinymongo_hash[key] && (@_tinymongo_hash[key].instance_of? Array))
194
+ value.each { |v| send(key) << value }
195
+ else
196
+ send(key + '=', value)
197
+ end
198
+ end
199
+
200
+ if(@_tinymongo_hash['_id'])
201
+ collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$pushAll' => Helper.hashify_models_in_hash(hash) })
202
+ end
203
+ end
204
+
205
+ def add_to_set(hash)
206
+ hash.each_pair do |key, value|
207
+ key = key.to_s
208
+
209
+ if(!(@_tinymongo_hash[key].include? value) && (@_tinymongo_hash[key].instance_of? Array))
210
+ send(key) << value
211
+ end
212
+ end
213
+
214
+ if(@_tinymongo_hash['_id'])
215
+ collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$addToSet' => Helper.hashify_models_in_hash(hash) })
216
+ end
217
+ end
218
+
219
+ def pop(hash)
220
+ hash.each_pair do |key, value|
221
+ key = key.to_s
222
+
223
+ if(@_tinymongo_hash[key] && (@_tinymongo_hash[key].instance_of? Array))
224
+ if(value == 1)
225
+ send(key).pop
226
+ elsif(value == -1)
227
+ send(key).shift
228
+ end
229
+ end
230
+ end
231
+
232
+ if(@_tinymongo_hash['_id'])
233
+ collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$pop' => Helper.hashify_models_in_hash(hash) })
234
+ end
235
+ end
236
+
237
+ def pull(hash)
238
+ hash.each_pair do |key, value|
239
+ key = key.to_s
240
+ if(@_tinymongo_hash[key] && (@_tinymongo_hash[key].instance_of? Array))
241
+ send(key).delete_if { |v| v == value }
242
+ end
243
+ end
244
+
245
+ if(@_tinymongo_hash['_id'])
246
+ collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$pull' => Helper.hashify_models_in_hash(hash) })
247
+ end
248
+ end
249
+
250
+ def pull_all(hash)
251
+ hash.each_pair do |key, value|
252
+ key = key.to_s
253
+ if(@_tinymongo_hash[key] && (@_tinymongo_hash[key].instance_of? Array))
254
+ value.each do |v|
255
+ send(key).delete_if { |w| w == v }
256
+ end
257
+ end
258
+ end
259
+
260
+ if(@_tinymongo_hash['_id'])
261
+ collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$pullAll' => Helper.hashify_models_in_hash(hash) })
262
+ end
263
+ end
264
+
265
+ protected
266
+ def create_or_update
267
+ if(@_tinymongo_hash['_id'].nil?) # new
268
+ @_tinymongo_hash['_id'] = collection.save(@_tinymongo_hash)
269
+ else # update
270
+ collection.update({ '_id' => @_tinymongo_hash['_id'] }, @_tinymongo_hash)
271
+ end
272
+ return self
273
+ end
274
+
275
+ end
276
+ end
data/lib/tinymongo.rb ADDED
@@ -0,0 +1,43 @@
1
+ require 'mongo'
2
+
3
+ module TinyMongo
4
+ class << self
5
+ def configure(config)
6
+ config = Helper.stringify_keys_in_hash(config)
7
+
8
+ @host = config['host'] || 'localhost'
9
+ @port = config['port']
10
+ @options = config['options'] || {}
11
+ @database = config['database'] || 'mongo'
12
+ @username = config['username']
13
+ @password = config['password']
14
+ end
15
+
16
+ def db
17
+ @db
18
+ end
19
+
20
+ def connect
21
+ if defined?(PhusionPassenger) && @connection
22
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
23
+ @connection.connect_to_master if forked
24
+ end
25
+ end
26
+
27
+ if(@connection.nil?)
28
+ @connection = Mongo::Connection.new(@host, @port, @options)
29
+ @db = @connection.db(@database)
30
+
31
+ if(@username && @password)
32
+ auth = db.authenticate(@username, @password)
33
+ return nil unless(auth)
34
+ end
35
+ end
36
+
37
+ true
38
+ end
39
+ end
40
+ end
41
+
42
+ require 'tinymongo/helper'
43
+ require 'tinymongo/model'
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require 'tinymongo'
@@ -0,0 +1,232 @@
1
+ $: << (File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
2
+ require 'test_helper'
3
+
4
+ TinyMongo.configure({:host => 'localhost', :database => 'tinymongo_test'})
5
+ TinyMongo.connect
6
+
7
+ class Dummy < TinyMongo::Model
8
+ mongo_collection :dummies
9
+ mongo_key :foo
10
+ mongo_key :bar
11
+ end
12
+
13
+ class TinyMongoTest < Test::Unit::TestCase
14
+ def setup
15
+ TinyMongo.db['dummies'].drop()
16
+ end
17
+
18
+ def test_helper_stringify_keys_in_hash
19
+ hash = {:foo => 'hello', :bar => 'world'}
20
+ assert_equal({'foo' => 'hello', 'bar' => 'world'}, TinyMongo::Helper.stringify_keys_in_hash(hash))
21
+ end
22
+
23
+ def test_helper_symbolify_keys_in_hash
24
+ hash = {'foo' => 'hello', 'bar' => 'world'}
25
+ assert_equal({:foo => 'hello', :bar => 'world'}, TinyMongo::Helper.symbolify_keys_in_hash(hash))
26
+ end
27
+
28
+ def test_helper_hashify_models_in_hash
29
+ obj1 = Dummy.new(:foo => 'hello', :bar => 'world')
30
+ obj2 = Dummy.new(:foo => 'love', :bar => 'ek')
31
+ hash = { :obj1 => obj1, :obj2 => obj2, :hash => { :obj => obj1 }, :array => [obj1, obj2, 'yay'], :val => 'pete' }
32
+ assert_equal({ 'obj1' => { 'foo' => 'hello', 'bar' => 'world' },
33
+ 'obj2' => { 'foo' => 'love', 'bar' => 'ek' },
34
+ 'hash' => { 'obj' => { 'foo' => 'hello', 'bar' => 'world' } },
35
+ 'array' => [ { 'foo' => 'hello', 'bar' => 'world' }, { 'foo' => 'love', 'bar' => 'ek' }, 'yay' ],
36
+ 'val' => 'pete' },
37
+ TinyMongo::Helper.hashify_models_in_hash(hash))
38
+ end
39
+
40
+ def test_helper_hashify_models_in_array
41
+ obj1 = Dummy.new(:foo => 'hello', :bar => 'world')
42
+ obj2 = Dummy.new(:foo => 'love', :bar => 'ek')
43
+ array = [ obj1, obj2, { :obj => obj1 }, [obj1, obj2, 'yay'], 'pete' ]
44
+ assert_equal([ { 'foo' => 'hello', 'bar' => 'world' },
45
+ { 'foo' => 'love', 'bar' => 'ek' },
46
+ { 'obj' => { 'foo' => 'hello', 'bar' => 'world' } },
47
+ [ { 'foo' => 'hello', 'bar' => 'world' }, { 'foo' => 'love', 'bar' => 'ek' }, 'yay' ],
48
+ 'pete' ],
49
+ TinyMongo::Helper.hashify_models_in_array(array))
50
+ end
51
+
52
+ def test_mongo_key
53
+ m = Dummy.new('foo' => 'hello')
54
+ m.bar = 'world'
55
+
56
+ assert_equal 'hello', m.foo
57
+ assert_equal 'world', m.bar
58
+ end
59
+
60
+ def test_save_new
61
+ hash = {'foo' => 'hello', 'bar' => 'world'}
62
+
63
+ obj = Dummy.new(hash)
64
+ obj.save # save to db
65
+ hash['_id'] = obj._id # add _id to hash
66
+ result = TinyMongo.db['dummies'].find_one({'_id' => obj._id})
67
+ assert_equal TinyMongo::Helper.stringify_keys_in_hash(hash), result # compare
68
+ end
69
+
70
+ def test_create
71
+ hash = {'foo' => 'hello', 'bar' => 'world'}
72
+
73
+ obj = Dummy.create(hash) # save to db
74
+ hash['_id'] = obj._id # add _id to hash
75
+
76
+ result = TinyMongo.db['dummies'].find_one({'_id' => obj._id})
77
+ assert_equal TinyMongo::Helper.stringify_keys_in_hash(hash), result, result # compare
78
+ end
79
+
80
+ def test_save_update
81
+ obj = Dummy.create('foo' => 'hello')
82
+ obj.foo = 'bye'
83
+ obj.save
84
+
85
+ result = TinyMongo.db['dummies'].find_one({'_id' => obj._id})
86
+ assert_equal 'bye', result['foo']
87
+ end
88
+
89
+ def test_update_attribute
90
+ obj = Dummy.create('foo' => 'hello')
91
+ obj.update_attribute('foo', 'bye')
92
+
93
+ result = TinyMongo.db['dummies'].find_one({'_id' => obj._id})
94
+ assert_equal 'bye', result['foo']
95
+ end
96
+
97
+ def test_update_attributes
98
+ obj = Dummy.create('foo' => 'hello', 'bar' => 'world')
99
+ obj.update_attributes({'foo' => 'world', 'bar' => 'hello'})
100
+
101
+ result = TinyMongo.db['dummies'].find_one({'_id' => obj._id})
102
+ assert_equal 'world', result['foo']
103
+ assert_equal 'hello', result['bar']
104
+ end
105
+
106
+ def test_find_one_using_id
107
+ o_id = TinyMongo.db['dummies'].save({'foo' => 'hello'})
108
+
109
+ found = Dummy.find_one(o_id)
110
+
111
+ assert_equal 'hello', found.foo
112
+ assert_equal o_id, found._id
113
+ end
114
+
115
+ def test_find_one_using_id_string
116
+ o_id = TinyMongo.db['dummies'].save({'foo' => 'hello'})
117
+
118
+ found = Dummy.find_one(o_id.to_s)
119
+
120
+ assert_equal 'hello', found.foo
121
+ assert_equal o_id, found._id
122
+ end
123
+
124
+ def test_find_one_using_hash
125
+ o_id = TinyMongo.db['dummies'].save({'foo' => 'hello', 'bar' => 'world'})
126
+
127
+ found = Dummy.find_one({'foo' => 'hello'})
128
+
129
+ assert_equal 'hello', found.foo
130
+ assert_equal 'world', found.bar
131
+ assert_equal o_id, found._id
132
+ end
133
+
134
+ def test_count
135
+ Dummy.create('foo' => 'hello')
136
+ Dummy.create('foo' => 'hello')
137
+ Dummy.create('foo' => 'hello')
138
+
139
+ assert_equal 3, Dummy.count
140
+ end
141
+
142
+ def test_delete
143
+ obj = Dummy.create('foo' => 'hello')
144
+ assert_equal 1, Dummy.count
145
+ obj.delete
146
+ assert_equal 0, Dummy.count
147
+ end
148
+
149
+ def test_delete_using_id
150
+ obj = Dummy.create('foo' => 'hello')
151
+ assert_equal 1, Dummy.count
152
+ Dummy.delete(obj._id)
153
+ assert_equal 0, Dummy.count
154
+ end
155
+
156
+ def test_delete_all
157
+ Dummy.create('foo' => 'hello')
158
+ Dummy.create('foo' => 'hello')
159
+ Dummy.create('foo' => 'hello')
160
+ assert_equal 3, Dummy.count
161
+ Dummy.delete_all
162
+ assert_equal 0, Dummy.count
163
+ end
164
+
165
+ def test_inc
166
+ d = Dummy.create('foo' => 1)
167
+ assert_equal 1, TinyMongo.db['dummies'].find_one()['foo']
168
+ d.inc({'foo' => 1})
169
+ assert_equal 2, TinyMongo.db['dummies'].find_one()['foo']
170
+ end
171
+
172
+ def test_set
173
+ d = Dummy.create('foo' => 'hello')
174
+ assert_equal 'hello', TinyMongo.db['dummies'].find_one()['foo']
175
+ d.set({'foo' => 'bye'})
176
+ assert_equal 'bye', TinyMongo.db['dummies'].find_one()['foo']
177
+ end
178
+
179
+ def test_unset
180
+ d = Dummy.create('foo' => 'hello')
181
+ assert_equal 'hello', TinyMongo.db['dummies'].find_one()['foo']
182
+ d.unset({'foo' => 1})
183
+ assert_equal nil, TinyMongo.db['dummies'].find_one()['foo']
184
+ end
185
+
186
+ def test_push
187
+ d = Dummy.create('foo' => [])
188
+ assert_equal [], TinyMongo.db['dummies'].find_one()['foo']
189
+ d.push({'foo' => 'hello'})
190
+ assert_equal ['hello'], TinyMongo.db['dummies'].find_one()['foo']
191
+ end
192
+
193
+ def test_push_all
194
+ d = Dummy.create('foo' => [])
195
+ assert_equal [], TinyMongo.db['dummies'].find_one()['foo']
196
+ d.push_all({'foo' => ['hello','world']})
197
+ assert_equal ['hello', 'world'], TinyMongo.db['dummies'].find_one()['foo']
198
+ end
199
+
200
+ def test_add_to_set
201
+ d = Dummy.create('foo' => [1,2,3,4])
202
+ assert_equal [1,2,3,4], TinyMongo.db['dummies'].find_one()['foo']
203
+ d.add_to_set({'foo' => 1})
204
+ assert_equal [1,2,3,4], TinyMongo.db['dummies'].find_one()['foo']
205
+ d.add_to_set({'foo' => 5})
206
+ assert_equal [1,2,3,4,5], TinyMongo.db['dummies'].find_one()['foo']
207
+ end
208
+
209
+ def test_pop
210
+ d = Dummy.create('foo' => [1,2,3,4])
211
+ assert_equal [1,2,3,4], TinyMongo.db['dummies'].find_one()['foo']
212
+ d.pop({'foo' => 1})
213
+ assert_equal [1,2,3], TinyMongo.db['dummies'].find_one()['foo']
214
+ d.pop({'foo' => -1})
215
+ assert_equal [2,3], TinyMongo.db['dummies'].find_one()['foo']
216
+ end
217
+
218
+ def test_pull
219
+ d = Dummy.create('foo' => [1,1,2,2,2,3])
220
+ assert_equal [1,1,2,2,2,3], TinyMongo.db['dummies'].find_one()['foo']
221
+ d.pull({'foo' => 2})
222
+ assert_equal [1,1,3], TinyMongo.db['dummies'].find_one()['foo']
223
+ end
224
+
225
+ def test_pull_all
226
+ d = Dummy.create('foo' => [1,1,2,2,2,3])
227
+ assert_equal [1,1,2,2,2,3], TinyMongo.db['dummies'].find_one()['foo']
228
+ d.pull_all({'foo' => [1,2]})
229
+ assert_equal [3], TinyMongo.db['dummies'].find_one()['foo']
230
+ end
231
+
232
+ end
data/tinymongo.gemspec ADDED
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{tinymongo}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Peter Jihoon Kim"]
12
+ s.date = %q{2010-07-21}
13
+ s.description = %q{Simple MongoDB wrapper}
14
+ s.email = %q{raingrove@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ "LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "init.rb",
25
+ "lib/generators/USAGE",
26
+ "lib/generators/templates/tinymongo.yml.erb",
27
+ "lib/generators/tinymongo_generator.rb",
28
+ "lib/tinymongo.rb",
29
+ "lib/tinymongo/helper.rb",
30
+ "lib/tinymongo/model.rb",
31
+ "test/test_helper.rb",
32
+ "test/test_tinymongo.rb",
33
+ "tinymongo.gemspec"
34
+ ]
35
+ s.homepage = %q{http://github.com/petejkim/tinymongo}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.7}
39
+ s.summary = %q{Simple MongoDB wrapper}
40
+ s.test_files = [
41
+ "test/test_helper.rb",
42
+ "test/test_tinymongo.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
50
+ s.add_runtime_dependency(%q<mongo>, [">= 0"])
51
+ s.add_runtime_dependency(%q<bson>, [">= 0"])
52
+ else
53
+ s.add_dependency(%q<mongo>, [">= 0"])
54
+ s.add_dependency(%q<bson>, [">= 0"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<mongo>, [">= 0"])
58
+ s.add_dependency(%q<bson>, [">= 0"])
59
+ end
60
+ end
61
+
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tinymongo
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Peter Jihoon Kim
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-21 00:00:00 +09:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: mongo
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: bson
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :runtime
45
+ version_requirements: *id002
46
+ description: Simple MongoDB wrapper
47
+ email: raingrove@gmail.com
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files:
53
+ - LICENSE
54
+ - README.rdoc
55
+ files:
56
+ - LICENSE
57
+ - README.rdoc
58
+ - Rakefile
59
+ - VERSION
60
+ - init.rb
61
+ - lib/generators/USAGE
62
+ - lib/generators/templates/tinymongo.yml.erb
63
+ - lib/generators/tinymongo_generator.rb
64
+ - lib/tinymongo.rb
65
+ - lib/tinymongo/helper.rb
66
+ - lib/tinymongo/model.rb
67
+ - test/test_helper.rb
68
+ - test/test_tinymongo.rb
69
+ - tinymongo.gemspec
70
+ has_rdoc: true
71
+ homepage: http://github.com/petejkim/tinymongo
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options:
76
+ - --charset=UTF-8
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project:
98
+ rubygems_version: 1.3.7
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Simple MongoDB wrapper
102
+ test_files:
103
+ - test/test_helper.rb
104
+ - test/test_tinymongo.rb