tinymongo 0.1.2 → 0.1.3
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/VERSION +1 -1
- data/lib/tinymongo/errors.rb +22 -0
- data/lib/tinymongo/helper.rb +1 -5
- data/lib/tinymongo/model.rb +32 -140
- data/lib/tinymongo/modifiers.rb +46 -0
- data/lib/tinymongo.rb +3 -1
- data/tinymongo.gemspec +4 -2
- metadata +5 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module TinyMongo
|
2
|
+
class Error < StandardError
|
3
|
+
end
|
4
|
+
|
5
|
+
class NotConfiguredError < Error
|
6
|
+
def initialize
|
7
|
+
super('Please do TinyMongo.configure() before attempting to connect!')
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class NotConnectedError < Error
|
12
|
+
def initialize
|
13
|
+
super('Not connected to MongoDB. Please connect using TinyMongo.connect()!')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class ModifierOperationError < Error
|
18
|
+
def initialize
|
19
|
+
super('Modifier operations are not allowed on objects that are not yet saved!')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/tinymongo/helper.rb
CHANGED
@@ -28,11 +28,7 @@ module TinyMongo
|
|
28
28
|
end
|
29
29
|
|
30
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
|
31
|
+
new_array = array.map { |value| hashify_models_in(value) }
|
36
32
|
end
|
37
33
|
|
38
34
|
def hashify_models_in_hash(hash)
|
data/lib/tinymongo/model.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'tinymongo/modifiers'
|
2
|
+
|
1
3
|
module TinyMongo
|
2
4
|
class Model
|
3
5
|
class << self
|
@@ -23,7 +25,6 @@ module TinyMongo
|
|
23
25
|
end
|
24
26
|
|
25
27
|
def db
|
26
|
-
raise 'Not connected to MongoDB. Please connect using TinyMongo.connect().' unless TinyMongo.connected?
|
27
28
|
TinyMongo.db
|
28
29
|
end
|
29
30
|
|
@@ -42,16 +43,23 @@ module TinyMongo
|
|
42
43
|
args.each do |arg|
|
43
44
|
new_args << Helper.hashify_models_in(arg)
|
44
45
|
end
|
45
|
-
collection.find(*new_args)
|
46
|
+
cursor = collection.find(*new_args)
|
47
|
+
|
48
|
+
if(cursor && cursor.count > 0)
|
49
|
+
hashes = cursor.to_a
|
50
|
+
objs = hashes.map { |hash| self.new(hash) }
|
51
|
+
end
|
52
|
+
|
53
|
+
cursor ? objs : nil
|
46
54
|
end
|
47
55
|
|
48
56
|
def find_one(*args)
|
49
57
|
if([BSON::ObjectID, String].include? args[0].class)
|
50
|
-
|
58
|
+
hash = collection.find_one({'_id' => Helper.bson_object_id(args[0])})
|
51
59
|
else
|
52
|
-
|
60
|
+
hash = collection.find_one(*args)
|
53
61
|
end
|
54
|
-
|
62
|
+
hash ? self.new(hash) : nil
|
55
63
|
end
|
56
64
|
|
57
65
|
def create(hash)
|
@@ -80,6 +88,10 @@ module TinyMongo
|
|
80
88
|
end
|
81
89
|
end
|
82
90
|
|
91
|
+
def initialize(hash={})
|
92
|
+
@_tinymongo_hash = Helper.stringify_keys_in_hash(hash)
|
93
|
+
end
|
94
|
+
|
83
95
|
def _id
|
84
96
|
@_tinymongo_hash['_id']
|
85
97
|
end
|
@@ -87,10 +99,6 @@ module TinyMongo
|
|
87
99
|
def _id=(val)
|
88
100
|
@_tinymongo_hash['_id'] = Helper.bson_object_id(val)
|
89
101
|
end
|
90
|
-
|
91
|
-
def initialize(hash={})
|
92
|
-
@_tinymongo_hash = Helper.stringify_keys_in_hash(hash)
|
93
|
-
end
|
94
102
|
|
95
103
|
def db
|
96
104
|
self.db
|
@@ -103,16 +111,24 @@ module TinyMongo
|
|
103
111
|
def to_hash
|
104
112
|
@_tinymongo_hash.clone
|
105
113
|
end
|
106
|
-
|
114
|
+
|
115
|
+
def reload
|
116
|
+
if(self._id)
|
117
|
+
obj = collection.find_one({ '_id' => self._id })
|
118
|
+
@_tinymongo_hash = Helper.stringify_keys_in_hash(obj) if(obj)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
107
122
|
def save
|
108
|
-
if(
|
123
|
+
if(self._id.nil?) # new
|
109
124
|
oid = collection.save(@_tinymongo_hash)
|
110
125
|
if(oid)
|
111
126
|
@_tinymongo_hash.delete(:_id)
|
112
|
-
|
127
|
+
self._id = oid
|
113
128
|
end
|
114
129
|
else # update
|
115
|
-
collection.update({ '_id' =>
|
130
|
+
collection.update({ '_id' => self._id }, @_tinymongo_hash, :upsert => true)
|
131
|
+
reload
|
116
132
|
end
|
117
133
|
return self
|
118
134
|
end
|
@@ -128,8 +144,8 @@ module TinyMongo
|
|
128
144
|
end
|
129
145
|
|
130
146
|
def delete
|
131
|
-
if(
|
132
|
-
collection.remove({ '_id' =>
|
147
|
+
if(self._id)
|
148
|
+
collection.remove({ '_id' => self._id })
|
133
149
|
end
|
134
150
|
end
|
135
151
|
|
@@ -140,131 +156,7 @@ module TinyMongo
|
|
140
156
|
def destroy(id)
|
141
157
|
delete(id)
|
142
158
|
end
|
143
|
-
|
144
|
-
def inc(hash)
|
145
|
-
hash.each_pair do |key, value|
|
146
|
-
key = key.to_s
|
147
|
-
|
148
|
-
if(@_tinymongo_hash[key] && (@_tinymongo_hash[key].kind_of? Numeric))
|
149
|
-
send(key + '=', (send(key) + value))
|
150
|
-
else
|
151
|
-
send(key + '=', value)
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
if(@_tinymongo_hash['_id'])
|
156
|
-
collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$inc' => Helper.hashify_models_in_hash(hash) })
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
def set(hash)
|
161
|
-
hash.each_pair { |key, value| send(key.to_s + '=', value) }
|
162
|
-
|
163
|
-
if(@_tinymongo_hash['_id'])
|
164
|
-
collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$set' => Helper.hashify_models_in_hash(hash) })
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
def unset(hash)
|
169
|
-
hash.each_key do |key|
|
170
|
-
@_tinymongo_hash.delete(key.to_s)
|
171
|
-
end
|
172
|
-
|
173
|
-
if(@_tinymongo_hash['_id'])
|
174
|
-
collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$unset' => Helper.hashify_models_in_hash(hash) })
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
def push(hash)
|
179
|
-
hash.each_pair do |key, value|
|
180
|
-
key = key.to_s
|
181
|
-
|
182
|
-
if(@_tinymongo_hash[key] && (@_tinymongo_hash[key].instance_of? Array))
|
183
|
-
send(key) << value
|
184
|
-
else
|
185
|
-
send(key + '=', value)
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
if(@_tinymongo_hash['_id'])
|
190
|
-
collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$push' => Helper.hashify_models_in_hash(hash) })
|
191
|
-
end
|
192
|
-
end
|
193
|
-
|
194
|
-
def push_all(hash)
|
195
|
-
hash.each_pair do |key, value|
|
196
|
-
key = key.to_s
|
197
|
-
|
198
|
-
if(@_tinymongo_hash[key] && (@_tinymongo_hash[key].instance_of? Array))
|
199
|
-
value.each { |v| send(key) << value }
|
200
|
-
else
|
201
|
-
send(key + '=', value)
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
if(@_tinymongo_hash['_id'])
|
206
|
-
collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$pushAll' => Helper.hashify_models_in_hash(hash) })
|
207
|
-
end
|
208
|
-
end
|
209
|
-
|
210
|
-
def add_to_set(hash)
|
211
|
-
hash.each_pair do |key, value|
|
212
|
-
key = key.to_s
|
213
|
-
|
214
|
-
if(!(@_tinymongo_hash[key].include? value) && (@_tinymongo_hash[key].instance_of? Array))
|
215
|
-
send(key) << value
|
216
|
-
end
|
217
|
-
end
|
218
159
|
|
219
|
-
|
220
|
-
collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$addToSet' => Helper.hashify_models_in_hash(hash) })
|
221
|
-
end
|
222
|
-
end
|
223
|
-
|
224
|
-
def pop(hash)
|
225
|
-
hash.each_pair do |key, value|
|
226
|
-
key = key.to_s
|
227
|
-
|
228
|
-
if(@_tinymongo_hash[key] && (@_tinymongo_hash[key].instance_of? Array))
|
229
|
-
if(value == 1)
|
230
|
-
send(key).pop
|
231
|
-
elsif(value == -1)
|
232
|
-
send(key).shift
|
233
|
-
end
|
234
|
-
end
|
235
|
-
end
|
236
|
-
|
237
|
-
if(@_tinymongo_hash['_id'])
|
238
|
-
collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$pop' => Helper.hashify_models_in_hash(hash) })
|
239
|
-
end
|
240
|
-
end
|
241
|
-
|
242
|
-
def pull(hash)
|
243
|
-
hash.each_pair do |key, value|
|
244
|
-
key = key.to_s
|
245
|
-
if(@_tinymongo_hash[key] && (@_tinymongo_hash[key].instance_of? Array))
|
246
|
-
send(key).delete_if { |v| v == value }
|
247
|
-
end
|
248
|
-
end
|
249
|
-
|
250
|
-
if(@_tinymongo_hash['_id'])
|
251
|
-
collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$pull' => Helper.hashify_models_in_hash(hash) })
|
252
|
-
end
|
253
|
-
end
|
254
|
-
|
255
|
-
def pull_all(hash)
|
256
|
-
hash.each_pair do |key, value|
|
257
|
-
key = key.to_s
|
258
|
-
if(@_tinymongo_hash[key] && (@_tinymongo_hash[key].instance_of? Array))
|
259
|
-
value.each do |v|
|
260
|
-
send(key).delete_if { |w| w == v }
|
261
|
-
end
|
262
|
-
end
|
263
|
-
end
|
264
|
-
|
265
|
-
if(@_tinymongo_hash['_id'])
|
266
|
-
collection.update({ '_id' => @_tinymongo_hash['_id'] }, { '$pullAll' => Helper.hashify_models_in_hash(hash) })
|
267
|
-
end
|
268
|
-
end
|
160
|
+
include Modifiers
|
269
161
|
end
|
270
162
|
end
|
@@ -0,0 +1,46 @@
|
|
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
|
data/lib/tinymongo.rb
CHANGED
@@ -15,11 +15,12 @@ module TinyMongo
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def db
|
18
|
+
raise NotConnectedError unless TinyMongo.connected?
|
18
19
|
@db
|
19
20
|
end
|
20
21
|
|
21
22
|
def connect
|
22
|
-
raise
|
23
|
+
raise NotConfiguredError unless @configured
|
23
24
|
|
24
25
|
if defined?(PhusionPassenger) && @connection
|
25
26
|
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
@@ -46,5 +47,6 @@ module TinyMongo
|
|
46
47
|
end
|
47
48
|
end
|
48
49
|
|
50
|
+
require 'tinymongo/errors'
|
49
51
|
require 'tinymongo/helper'
|
50
52
|
require 'tinymongo/model'
|
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.3"
|
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-22}
|
13
13
|
s.description = %q{Simple MongoDB wrapper}
|
14
14
|
s.email = %q{raingrove@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -28,8 +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/errors.rb",
|
31
32
|
"lib/tinymongo/helper.rb",
|
32
33
|
"lib/tinymongo/model.rb",
|
34
|
+
"lib/tinymongo/modifiers.rb",
|
33
35
|
"test/test_helper.rb",
|
34
36
|
"test/test_tinymongo.rb",
|
35
37
|
"tinymongo.gemspec"
|
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
|
+
- 3
|
9
|
+
version: 0.1.3
|
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-22 00:00:00 +09:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -64,8 +64,10 @@ files:
|
|
64
64
|
- lib/generators/templates/tinymongo.yml.erb
|
65
65
|
- lib/generators/tinymongo_generator.rb
|
66
66
|
- lib/tinymongo.rb
|
67
|
+
- lib/tinymongo/errors.rb
|
67
68
|
- lib/tinymongo/helper.rb
|
68
69
|
- lib/tinymongo/model.rb
|
70
|
+
- lib/tinymongo/modifiers.rb
|
69
71
|
- test/test_helper.rb
|
70
72
|
- test/test_tinymongo.rb
|
71
73
|
- tinymongo.gemspec
|