mongomatic 0.5.6 → 0.5.7
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/lib/mongomatic/base.rb +10 -12
- data/lib/mongomatic/modifiers.rb +40 -40
- data/test/helper.rb +0 -3
- data/test/test_mongomatic.rb +0 -7
- metadata +3 -3
data/lib/mongomatic/base.rb
CHANGED
@@ -69,14 +69,14 @@ module Mongomatic
|
|
69
69
|
do_callback(:after_drop)
|
70
70
|
end
|
71
71
|
|
72
|
-
[:before_drop, :after_drop].each do |method|
|
73
|
-
define_method(method) { true }
|
74
|
-
end
|
75
|
-
|
76
72
|
private
|
77
73
|
|
78
74
|
def do_callback(meth)
|
79
|
-
|
75
|
+
begin
|
76
|
+
send(meth)
|
77
|
+
rescue NoMethodError => e
|
78
|
+
false
|
79
|
+
end
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
@@ -238,12 +238,6 @@ module Mongomatic
|
|
238
238
|
@doc || {}
|
239
239
|
end
|
240
240
|
|
241
|
-
[ :before_validate, :after_validate, :before_insert, :before_insert_or_update,
|
242
|
-
:after_insert, :after_insert_or_update, :before_update, :after_update,
|
243
|
-
:before_remove, :after_remove].each do |method|
|
244
|
-
define_method(method) { true }
|
245
|
-
end
|
246
|
-
|
247
241
|
protected
|
248
242
|
|
249
243
|
def hash_for_field(field, break_if_dne=false)
|
@@ -260,7 +254,11 @@ module Mongomatic
|
|
260
254
|
end
|
261
255
|
|
262
256
|
def do_callback(meth)
|
263
|
-
|
257
|
+
begin
|
258
|
+
send(meth)
|
259
|
+
rescue NoMethodError => e
|
260
|
+
false
|
261
|
+
end
|
264
262
|
end
|
265
263
|
end
|
266
264
|
end
|
data/lib/mongomatic/modifiers.rb
CHANGED
@@ -7,7 +7,7 @@ module Mongomatic
|
|
7
7
|
# MongoDB equivalent: { $push : { field : value } }<br/>
|
8
8
|
# Appends value to field, if field is an existing array, otherwise sets field to the array [value]
|
9
9
|
# if field is not present. If field is present but is not an array, error is returned.
|
10
|
-
def push(field, val, safe=false)
|
10
|
+
def push(field, val, update_opts={}, safe=false)
|
11
11
|
mongo_field = field.to_s
|
12
12
|
field, hash = hash_for_field(mongo_field)
|
13
13
|
|
@@ -18,7 +18,7 @@ module Mongomatic
|
|
18
18
|
op = { "$push" => { mongo_field => val } }
|
19
19
|
res = true
|
20
20
|
|
21
|
-
safe == true ? res = update!(
|
21
|
+
safe == true ? res = update!(update_opts, op) : update(update_opts, op)
|
22
22
|
|
23
23
|
if res
|
24
24
|
hash[field] ||= []
|
@@ -27,8 +27,8 @@ module Mongomatic
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
def push!(field, val)
|
31
|
-
push(field, val, true)
|
30
|
+
def push!(field, val, update_opts={})
|
31
|
+
push(field, val, update_opts, true)
|
32
32
|
end
|
33
33
|
|
34
34
|
# MongoDB equivalent: { $pushAll : { field : value_array } }<br/>
|
@@ -36,7 +36,7 @@ module Mongomatic
|
|
36
36
|
# the array value_array if field is not present. If field is present but is not an array, an error
|
37
37
|
# condition is raised.
|
38
38
|
# user.push("interests", ["skydiving", "coding"])
|
39
|
-
def push_all(field, val, safe=false)
|
39
|
+
def push_all(field, val, update_opts={}, safe=false)
|
40
40
|
mongo_field = field.to_s
|
41
41
|
field, hash = hash_for_field(mongo_field)
|
42
42
|
|
@@ -48,7 +48,7 @@ module Mongomatic
|
|
48
48
|
op = { "$pushAll" => { mongo_field => val } }
|
49
49
|
res = true
|
50
50
|
|
51
|
-
safe == true ? res = update!(
|
51
|
+
safe == true ? res = update!(update_opts, op) : update(update_opts, op)
|
52
52
|
|
53
53
|
if res
|
54
54
|
hash[field] ||= []
|
@@ -57,15 +57,15 @@ module Mongomatic
|
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
-
def push_all!(field, val)
|
61
|
-
push_all(field, val, true)
|
60
|
+
def push_all!(field, val, update_opts={})
|
61
|
+
push_all(field, val, update_opts, true)
|
62
62
|
end
|
63
63
|
|
64
64
|
# MongoDB equivalent: { $pull : { field : _value } }<br/>
|
65
65
|
# Removes all occurrences of value from field, if field is an array. If field is present but is not
|
66
66
|
# an array, an error condition is raised.
|
67
67
|
# user.pull("interests", "watching paint dry")
|
68
|
-
def pull(field, val, safe=false)
|
68
|
+
def pull(field, val, update_opts={}, safe=false)
|
69
69
|
mongo_field = field.to_s
|
70
70
|
field, hash = hash_for_field(mongo_field)
|
71
71
|
|
@@ -76,7 +76,7 @@ module Mongomatic
|
|
76
76
|
op = { "$pull" => { mongo_field => val } }
|
77
77
|
res = true
|
78
78
|
|
79
|
-
safe == true ? res = update!(
|
79
|
+
safe == true ? res = update!(update_opts, op) : update(update_opts, op)
|
80
80
|
|
81
81
|
if res
|
82
82
|
hash[field] ||= []
|
@@ -85,15 +85,15 @@ module Mongomatic
|
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
88
|
-
def pull!(field, val)
|
89
|
-
pull(field, val, true)
|
88
|
+
def pull!(field, val, update_opts={})
|
89
|
+
pull(field, val, update_opts, true)
|
90
90
|
end
|
91
91
|
|
92
92
|
# MongoDB equivalent: { $pullAll : { field : value_array } }<br/>
|
93
93
|
# Removes all occurrences of each value in value_array from field, if field is an array. If field is
|
94
94
|
# present but is not an array, an error condition is raised.
|
95
95
|
# user.pull_all("interests", ["watching paint dry", "sitting on my ass"])
|
96
|
-
def pull_all(field, val, safe=false)
|
96
|
+
def pull_all(field, val, update_opts={}, safe=false)
|
97
97
|
mongo_field = field.to_s
|
98
98
|
field, hash = hash_for_field(mongo_field)
|
99
99
|
|
@@ -104,7 +104,7 @@ module Mongomatic
|
|
104
104
|
op = { "$pullAll" => { mongo_field => create_array(val) } }
|
105
105
|
res = true
|
106
106
|
|
107
|
-
safe == true ? res = update!(
|
107
|
+
safe == true ? res = update!(update_opts, op) : update(update_opts, op)
|
108
108
|
|
109
109
|
if res
|
110
110
|
hash[field] ||= []
|
@@ -114,14 +114,14 @@ module Mongomatic
|
|
114
114
|
end
|
115
115
|
end
|
116
116
|
|
117
|
-
def pull_all!(field, val)
|
118
|
-
pull_all(field, val, true)
|
117
|
+
def pull_all!(field, val, update_opts={})
|
118
|
+
pull_all(field, val, update_opts, true)
|
119
119
|
end
|
120
120
|
|
121
121
|
# MongoDB equivalent: { $inc : { field : value } }<br/>
|
122
122
|
# Increments field by the number value if field is present in the object, otherwise sets field to the number value.
|
123
123
|
# user.inc("cents_in_wallet", 1000)
|
124
|
-
def inc(field, val, safe=false)
|
124
|
+
def inc(field, val, update_opts={}, safe=false)
|
125
125
|
mongo_field = field.to_s
|
126
126
|
field, hash = hash_for_field(mongo_field)
|
127
127
|
|
@@ -132,7 +132,7 @@ module Mongomatic
|
|
132
132
|
op = { "$inc" => { mongo_field => val } }
|
133
133
|
res = true
|
134
134
|
|
135
|
-
safe == true ? res = update!(
|
135
|
+
safe == true ? res = update!(update_opts, op) : update(update_opts, op)
|
136
136
|
|
137
137
|
if res
|
138
138
|
hash[field] ||= 0
|
@@ -141,21 +141,21 @@ module Mongomatic
|
|
141
141
|
end
|
142
142
|
end
|
143
143
|
|
144
|
-
def inc!(field, val)
|
145
|
-
inc(field, val, true)
|
144
|
+
def inc!(field, val, update_opts={})
|
145
|
+
inc(field, val, update_opts, true)
|
146
146
|
end
|
147
147
|
|
148
148
|
# MongoDB equivalent: { $set : { field : value } }<br/>
|
149
149
|
# Sets field to value. All datatypes are supported with $set.
|
150
150
|
# user.set("name", "Ben")
|
151
|
-
def set(field, val, safe=false)
|
151
|
+
def set(field, val, update_opts={}, safe=false)
|
152
152
|
mongo_field = field.to_s
|
153
153
|
field, hash = hash_for_field(field.to_s)
|
154
154
|
|
155
155
|
op = { "$set" => { mongo_field => val } }
|
156
156
|
res = true
|
157
157
|
|
158
|
-
safe == true ? res = update!(
|
158
|
+
safe == true ? res = update!(update_opts, op) : update(update_opts, op)
|
159
159
|
|
160
160
|
if res
|
161
161
|
hash[field] = val
|
@@ -163,21 +163,21 @@ module Mongomatic
|
|
163
163
|
end
|
164
164
|
end
|
165
165
|
|
166
|
-
def set!(field, val)
|
167
|
-
set(field, val, true)
|
166
|
+
def set!(field, val, update_opts={})
|
167
|
+
set(field, val, update_opts, true)
|
168
168
|
end
|
169
169
|
|
170
170
|
# MongoDB equivalent: { $unset : { field : 1} }<br/>
|
171
171
|
# Deletes a given field. v1.3+
|
172
172
|
# user.unset("name")
|
173
|
-
def unset(field, safe=false)
|
173
|
+
def unset(field, update_opts={}, safe=false)
|
174
174
|
mongo_field = field.to_s
|
175
175
|
field, hash = hash_for_field(mongo_field)
|
176
176
|
|
177
177
|
op = { "$unset" => { mongo_field => 1 } }
|
178
178
|
res = true
|
179
179
|
|
180
|
-
safe == true ? res = update!(
|
180
|
+
safe == true ? res = update!(update_opts, op) : update(update_opts, op)
|
181
181
|
|
182
182
|
if res
|
183
183
|
hash.delete(field)
|
@@ -185,8 +185,8 @@ module Mongomatic
|
|
185
185
|
end
|
186
186
|
end
|
187
187
|
|
188
|
-
def unset!(field)
|
189
|
-
unset(field, true)
|
188
|
+
def unset!(field, update_opts={})
|
189
|
+
unset(field, update_opts, true)
|
190
190
|
end
|
191
191
|
|
192
192
|
# MongoDB equivalent: { $addToSet : { field : value } }<br/>
|
@@ -194,7 +194,7 @@ module Mongomatic
|
|
194
194
|
# Or to add many values:<br/>
|
195
195
|
# { $addToSet : { a : { $each : [ 3 , 5 , 6 ] } } }
|
196
196
|
# user.add_to_set("friend_ids", BSON::ObjectId('...'))
|
197
|
-
def add_to_set(field, val, safe=false)
|
197
|
+
def add_to_set(field, val, update_opts={}, safe=false)
|
198
198
|
mongo_field = field.to_s
|
199
199
|
field, hash = hash_for_field(mongo_field)
|
200
200
|
|
@@ -211,7 +211,7 @@ module Mongomatic
|
|
211
211
|
end
|
212
212
|
|
213
213
|
res = true
|
214
|
-
safe == true ? res = update!(
|
214
|
+
safe == true ? res = update!(update_opts, op) : update(update_opts, op)
|
215
215
|
|
216
216
|
if res
|
217
217
|
hash[field] ||= []
|
@@ -222,14 +222,14 @@ module Mongomatic
|
|
222
222
|
end
|
223
223
|
end
|
224
224
|
|
225
|
-
def add_to_set!(field, val)
|
226
|
-
add_to_set(field, val, true)
|
225
|
+
def add_to_set!(field, val, update_opts={})
|
226
|
+
add_to_set(field, val, update_opts, true)
|
227
227
|
end
|
228
228
|
|
229
229
|
# MongoDB equivalent: { $pop : { field : 1 } }<br/>
|
230
230
|
# Removes the last element in an array (ADDED in 1.1)
|
231
231
|
# user.pop_last("friend_ids")
|
232
|
-
def pop_last(field, safe=false)
|
232
|
+
def pop_last(field, update_opts={}, safe=false)
|
233
233
|
mongo_field = field.to_s
|
234
234
|
field, hash = hash_for_field(mongo_field)
|
235
235
|
|
@@ -240,7 +240,7 @@ module Mongomatic
|
|
240
240
|
op = { "$pop" => { mongo_field => 1 } }
|
241
241
|
|
242
242
|
res = true
|
243
|
-
safe == true ? res = update!(
|
243
|
+
safe == true ? res = update!(update_opts, op) : update(update_opts, op)
|
244
244
|
|
245
245
|
if res
|
246
246
|
hash[field] ||= []
|
@@ -249,14 +249,14 @@ module Mongomatic
|
|
249
249
|
end
|
250
250
|
end
|
251
251
|
|
252
|
-
def pop_last!(field)
|
253
|
-
pop_last(field, true)
|
252
|
+
def pop_last!(field, update_opts={})
|
253
|
+
pop_last(field, update_opts, true)
|
254
254
|
end
|
255
255
|
|
256
256
|
# MongoDB equivalent: { $pop : { field : -1 } }<br/>
|
257
257
|
# Removes the first element in an array (ADDED in 1.1)
|
258
258
|
# user.pop_first("friend_ids")
|
259
|
-
def pop_first(field, safe=false)
|
259
|
+
def pop_first(field, update_opts={}, safe=false)
|
260
260
|
mongo_field = field.to_s
|
261
261
|
field, hash = hash_for_field(mongo_field)
|
262
262
|
|
@@ -267,7 +267,7 @@ module Mongomatic
|
|
267
267
|
op = { "$pop" => { mongo_field => -1 } }
|
268
268
|
|
269
269
|
res = true
|
270
|
-
safe == true ? res = update!(
|
270
|
+
safe == true ? res = update!(update_opts, op) : update(update_opts, op)
|
271
271
|
|
272
272
|
if res
|
273
273
|
hash[field] ||= []
|
@@ -276,8 +276,8 @@ module Mongomatic
|
|
276
276
|
end
|
277
277
|
end
|
278
278
|
|
279
|
-
def pop_first!(field)
|
280
|
-
pop_first(field, true)
|
279
|
+
def pop_first!(field, update_opts={})
|
280
|
+
pop_first(field, update_opts, true)
|
281
281
|
end
|
282
282
|
|
283
283
|
end
|
data/test/helper.rb
CHANGED
data/test/test_mongomatic.rb
CHANGED
@@ -1,13 +1,6 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
class TestMongomatic < Test::Unit::TestCase
|
4
|
-
should "not fail if callbacks are undefined" do
|
5
|
-
Car.collection.drop
|
6
|
-
c1 = Car.new(:make => "Honda", :model => "Accord")
|
7
|
-
c1.insert!
|
8
|
-
assert_equal c1, Car.find_one(c1["_id"])
|
9
|
-
end
|
10
|
-
|
11
4
|
should "treat all keys as strings" do
|
12
5
|
Person.collection.drop
|
13
6
|
p1 = Person.new(:name => "Jordan")
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 5
|
8
|
-
-
|
9
|
-
version: 0.5.
|
8
|
+
- 7
|
9
|
+
version: 0.5.7
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ben Myles
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-10-01 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|