mongodb 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongo/driver/collection.rb +24 -24
- data/lib/mongo/object/object.rb +50 -45
- data/lib/mongo/object/object_helper.rb +22 -22
- data/lib/mongo/object/spec.rb +2 -0
- data/lib/mongo/object/spec/crud_shared.rb +11 -1
- data/spec/object/spec_helper.rb +2 -2
- metadata +2 -2
@@ -5,34 +5,34 @@ module Mongo::CollectionExt
|
|
5
5
|
#
|
6
6
|
# CRUD
|
7
7
|
#
|
8
|
-
def save_with_ext doc,
|
9
|
-
save_without_ext doc, reverse_merge_defaults(
|
8
|
+
def save_with_ext doc, options = {}
|
9
|
+
save_without_ext doc, reverse_merge_defaults(options, :safe)
|
10
10
|
end
|
11
11
|
|
12
|
-
def insert_with_ext args,
|
13
|
-
result = insert_without_ext args, reverse_merge_defaults(
|
12
|
+
def insert_with_ext args, options = {}
|
13
|
+
result = insert_without_ext args, reverse_merge_defaults(options, :safe)
|
14
14
|
|
15
15
|
# fix for mongodriver, it will return single result if we supply [doc] as args
|
16
16
|
(args.is_a?(Array) and !result.is_a?(Array)) ? [result] : result
|
17
17
|
end
|
18
18
|
|
19
|
-
def update_with_ext selector, doc,
|
19
|
+
def update_with_ext selector, doc, options = {}
|
20
20
|
selector = convert_underscore_to_dollar_in_selector selector
|
21
21
|
doc = convert_underscore_to_dollar_in_update doc
|
22
22
|
|
23
23
|
# because :multi works only with $ operators, we need to check if it's applicable
|
24
|
-
|
25
|
-
reverse_merge_defaults(
|
24
|
+
options = if doc.keys.any?{|k| k =~ /^\$/}
|
25
|
+
reverse_merge_defaults(options, :safe, :multi)
|
26
26
|
else
|
27
|
-
reverse_merge_defaults(
|
27
|
+
reverse_merge_defaults(options, :safe)
|
28
28
|
end
|
29
29
|
|
30
|
-
update_without_ext selector, doc,
|
30
|
+
update_without_ext selector, doc, options
|
31
31
|
end
|
32
32
|
|
33
|
-
def remove_with_ext selector = {},
|
33
|
+
def remove_with_ext selector = {}, options = {}
|
34
34
|
selector = convert_underscore_to_dollar_in_selector selector
|
35
|
-
remove_without_ext selector, reverse_merge_defaults(
|
35
|
+
remove_without_ext selector, reverse_merge_defaults(options, :safe, :multi)
|
36
36
|
end
|
37
37
|
|
38
38
|
def destroy *args
|
@@ -46,33 +46,33 @@ module Mongo::CollectionExt
|
|
46
46
|
#
|
47
47
|
# Querying
|
48
48
|
#
|
49
|
-
def first selector = {},
|
49
|
+
def first selector = {}, options = {}
|
50
50
|
selector = convert_underscore_to_dollar_in_selector selector if selector.is_a? Hash
|
51
51
|
|
52
|
-
h = find_one selector,
|
52
|
+
h = find_one selector, options
|
53
53
|
symbolize_doc h
|
54
54
|
end
|
55
55
|
|
56
|
-
def first! selector = {},
|
57
|
-
first(selector,
|
56
|
+
def first! selector = {}, options = {}
|
57
|
+
first(selector, options) || raise(Mongo::NotFound, "document with selector #{selector} not found!")
|
58
58
|
end
|
59
59
|
|
60
|
-
def all selector = {},
|
60
|
+
def all selector = {}, options = {}, &block
|
61
61
|
if block
|
62
|
-
each selector,
|
62
|
+
each selector, options, &block
|
63
63
|
else
|
64
64
|
list = []
|
65
|
-
each(selector,
|
65
|
+
each(selector, options){|doc| list << doc}
|
66
66
|
list
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
|
-
def each selector = {},
|
70
|
+
def each selector = {}, options = {}, &block
|
71
71
|
selector = convert_underscore_to_dollar_in_selector selector
|
72
72
|
|
73
73
|
cursor = nil
|
74
74
|
begin
|
75
|
-
cursor = find selector, reverse_merge_defaults(
|
75
|
+
cursor = find selector, reverse_merge_defaults(options, :batch_size)
|
76
76
|
cursor.each do |doc|
|
77
77
|
doc = symbolize_doc doc
|
78
78
|
block.call doc
|
@@ -84,8 +84,8 @@ module Mongo::CollectionExt
|
|
84
84
|
nil
|
85
85
|
end
|
86
86
|
|
87
|
-
def count_with_ext selector = {},
|
88
|
-
find(selector,
|
87
|
+
def count_with_ext selector = {}, options = {}
|
88
|
+
find(selector, options).count()
|
89
89
|
end
|
90
90
|
|
91
91
|
protected
|
@@ -100,8 +100,8 @@ module Mongo::CollectionExt
|
|
100
100
|
:_inc, :_set, :_unset, :_push, :_pushAll, :_addToSet, :_pop, :_pull, :_pullAll, :_rename, :_bit
|
101
101
|
].to_set
|
102
102
|
|
103
|
-
def reverse_merge_defaults
|
104
|
-
h =
|
103
|
+
def reverse_merge_defaults options, *keys
|
104
|
+
h = options.clone
|
105
105
|
keys.each do |k|
|
106
106
|
h[k] = Mongo.defaults[k] if Mongo.defaults.include?(k) and !h.include?(k)
|
107
107
|
end
|
data/lib/mongo/object/object.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
module Mongo::Object
|
2
|
-
attr_accessor :_id
|
2
|
+
attr_accessor :_id, :_parent
|
3
3
|
|
4
|
-
def valid?
|
5
|
-
|
4
|
+
def valid? options = {}
|
5
|
+
options = ::Mongo::Object.parse_options options
|
6
6
|
begin
|
7
|
-
return false if
|
7
|
+
return false if options[:callbacks] and !::Mongo::Object.run_before_callbacks(self, :validate)
|
8
8
|
|
9
|
-
|
9
|
+
child_options = options.merge internal: true
|
10
10
|
result = [
|
11
|
-
child_objects.all?{|group| group.all?{|obj| obj.valid?(
|
11
|
+
child_objects.all?{|group| group.all?{|obj| obj.valid?(child_options)}},
|
12
12
|
::Mongo::Object.run_validations(self),
|
13
13
|
errors.empty?
|
14
14
|
].all?
|
15
15
|
|
16
|
-
::Mongo::Object.run_after_callbacks(self, :validate) if
|
16
|
+
::Mongo::Object.run_after_callbacks(self, :validate) if options[:callbacks]
|
17
17
|
|
18
18
|
result
|
19
19
|
ensure
|
20
|
-
clear_child_objects unless
|
20
|
+
clear_child_objects unless options[:internal]
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -25,34 +25,34 @@ module Mongo::Object
|
|
25
25
|
@_errors ||= {}
|
26
26
|
end
|
27
27
|
|
28
|
-
def create_object collection,
|
29
|
-
with_object_callbacks :create,
|
28
|
+
def create_object collection, options
|
29
|
+
with_object_callbacks :create, options do |options|
|
30
30
|
doc = ::Mongo::Object.to_mongo self
|
31
|
-
collection.create(doc,
|
31
|
+
collection.create(doc, options)
|
32
32
|
self._id = doc[:_id] || doc['_id']
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
def update_object collection,
|
37
|
-
with_object_callbacks :update,
|
36
|
+
def update_object collection, options
|
37
|
+
with_object_callbacks :update, options do |options|
|
38
38
|
id = _id || "can't update object without _id (#{self})!"
|
39
39
|
doc = ::Mongo::Object.to_mongo self
|
40
|
-
collection.update({_id: id}, doc,
|
40
|
+
collection.update({_id: id}, doc, options)
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
def destroy_object collection,
|
45
|
-
with_object_callbacks :destroy,
|
44
|
+
def destroy_object collection, options
|
45
|
+
with_object_callbacks :destroy, options do |options|
|
46
46
|
id = _id || "can't destroy object without _id (#{self})!"
|
47
|
-
collection.destroy({_id: id},
|
47
|
+
collection.destroy({_id: id}, options)
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
-
def save_object collection,
|
51
|
+
def save_object collection, options
|
52
52
|
if _id
|
53
|
-
update_object collection,
|
53
|
+
update_object collection, options
|
54
54
|
else
|
55
|
-
create_object collection,
|
55
|
+
create_object collection, options
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
@@ -61,21 +61,21 @@ module Mongo::Object
|
|
61
61
|
SKIP_IV_REGEXP = /^@_/
|
62
62
|
|
63
63
|
class << self
|
64
|
-
def parse_options
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
return
|
64
|
+
def parse_options options
|
65
|
+
options = options.clone
|
66
|
+
options[:validate] = true unless options.include?(:validate)
|
67
|
+
options[:callbacks] = Mongo.defaults[:callbacks] unless options.include?(:callbacks)
|
68
|
+
return options
|
69
69
|
end
|
70
70
|
|
71
|
-
def to_mongo_options
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
71
|
+
def to_mongo_options options
|
72
|
+
options = options.clone
|
73
|
+
options.delete :validate
|
74
|
+
options.delete :callbacks
|
75
|
+
options
|
76
76
|
end
|
77
77
|
|
78
|
-
def
|
78
|
+
def each_object_instance_variable obj, &block
|
79
79
|
obj.instance_variables.each do |iv_name|
|
80
80
|
# skipping variables starting with _xx, usually they
|
81
81
|
# have specific meaning and used for example for cache
|
@@ -102,7 +102,7 @@ module Mongo::Object
|
|
102
102
|
doc = {}
|
103
103
|
|
104
104
|
# copying instance variables
|
105
|
-
|
105
|
+
each_object_instance_variable obj do |iv_name, v|
|
106
106
|
k = iv_name.to_s[1..-1]
|
107
107
|
k = k.to_sym if symbolize
|
108
108
|
doc[k] = to_mongo v
|
@@ -127,7 +127,7 @@ module Mongo::Object
|
|
127
127
|
obj.each{|v| each_object v, &block}
|
128
128
|
elsif obj.is_a? ::Mongo::Object
|
129
129
|
block.call obj if include_first
|
130
|
-
|
130
|
+
each_object_instance_variable obj do |iv_name, v|
|
131
131
|
each_object v, &block
|
132
132
|
end
|
133
133
|
end
|
@@ -136,7 +136,7 @@ module Mongo::Object
|
|
136
136
|
|
137
137
|
def build doc
|
138
138
|
return unless doc
|
139
|
-
obj = _build doc
|
139
|
+
obj = _build doc, nil
|
140
140
|
obj.send :update_original_children! if obj.is_a? ::Mongo::Object
|
141
141
|
obj
|
142
142
|
end
|
@@ -164,28 +164,33 @@ module Mongo::Object
|
|
164
164
|
end
|
165
165
|
|
166
166
|
protected
|
167
|
-
def _build doc
|
167
|
+
def _build doc, parent = nil
|
168
168
|
if doc.is_a? Hash
|
169
169
|
if class_name = doc[:_class] || doc['_class']
|
170
170
|
klass = constantize class_name
|
171
171
|
|
172
172
|
if klass.respond_to? :from_mongo
|
173
|
-
klass.from_mongo doc
|
173
|
+
obj = klass.from_mongo doc
|
174
174
|
else
|
175
175
|
obj = klass.new
|
176
|
+
parent ||= obj
|
176
177
|
doc.each do |k, v|
|
177
178
|
next if k.to_sym == :_class
|
178
179
|
|
179
|
-
v = _build v
|
180
|
+
v = _build v, parent
|
180
181
|
obj.instance_variable_set "@#{k}", v
|
181
182
|
end
|
182
183
|
obj
|
183
184
|
end
|
185
|
+
obj._parent = parent if parent
|
186
|
+
obj
|
184
187
|
else
|
185
|
-
|
188
|
+
hash = {}
|
189
|
+
doc.each{|k, v| hash[k] = _build v, parent}
|
190
|
+
hash
|
186
191
|
end
|
187
192
|
elsif doc.is_a? Array
|
188
|
-
doc.collect{|v| _build v}
|
193
|
+
doc.collect{|v| _build v, parent}
|
189
194
|
else
|
190
195
|
doc
|
191
196
|
end
|
@@ -240,21 +245,21 @@ module Mongo::Object
|
|
240
245
|
@_child_objects
|
241
246
|
end
|
242
247
|
|
243
|
-
def with_object_callbacks method,
|
244
|
-
|
248
|
+
def with_object_callbacks method, options, &block
|
249
|
+
options = ::Mongo::Object.parse_options options
|
245
250
|
|
246
251
|
# validation
|
247
|
-
return false if
|
252
|
+
return false if options[:validate] and !valid?(options.merge(internal: true))
|
248
253
|
|
249
254
|
# before callbacks
|
250
|
-
return false if
|
255
|
+
return false if options[:callbacks] and !run_all_callbacks(:before, method)
|
251
256
|
|
252
257
|
# saving
|
253
|
-
block.call ::Mongo::Object.to_mongo_options(
|
258
|
+
block.call ::Mongo::Object.to_mongo_options(options)
|
254
259
|
update_original_children!
|
255
260
|
|
256
261
|
# after callbacks
|
257
|
-
run_all_callbacks :after, method if
|
262
|
+
run_all_callbacks :after, method if options[:callbacks]
|
258
263
|
|
259
264
|
true
|
260
265
|
ensure
|
@@ -2,37 +2,37 @@ module Mongo::ObjectHelper
|
|
2
2
|
#
|
3
3
|
# CRUD
|
4
4
|
#
|
5
|
-
def create_with_object doc,
|
5
|
+
def create_with_object doc, options = {}
|
6
6
|
if doc.is_a? ::Mongo::Object
|
7
|
-
doc.create_object self,
|
7
|
+
doc.create_object self, options
|
8
8
|
else
|
9
|
-
create_without_object doc,
|
9
|
+
create_without_object doc, options
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
def update_with_object *args
|
14
14
|
if args.first.is_a? ::Mongo::Object
|
15
|
-
doc,
|
16
|
-
|
17
|
-
doc.update_object self,
|
15
|
+
doc, options = args
|
16
|
+
options ||= {}
|
17
|
+
doc.update_object self, options
|
18
18
|
else
|
19
19
|
update_without_object *args
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
def save_with_object doc,
|
23
|
+
def save_with_object doc, options = {}
|
24
24
|
if doc.is_a? ::Mongo::Object
|
25
|
-
doc.save_object self,
|
25
|
+
doc.save_object self, options
|
26
26
|
else
|
27
|
-
save_without_object doc,
|
27
|
+
save_without_object doc, options
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
31
|
def destroy_with_object *args
|
32
32
|
if args.first.is_a? ::Mongo::Object
|
33
|
-
doc,
|
34
|
-
|
35
|
-
doc.destroy_object self,
|
33
|
+
doc, options = args
|
34
|
+
options ||= {}
|
35
|
+
doc.destroy_object self, options
|
36
36
|
else
|
37
37
|
destroy_without_object *args
|
38
38
|
end
|
@@ -58,21 +58,21 @@ module Mongo::ObjectHelper
|
|
58
58
|
#
|
59
59
|
# Querying
|
60
60
|
#
|
61
|
-
def first selector = {},
|
62
|
-
|
63
|
-
if
|
64
|
-
super selector,
|
61
|
+
def first selector = {}, options = {}, &block
|
62
|
+
options = options.clone
|
63
|
+
if options.delete(:object) == false
|
64
|
+
super selector, options, &block
|
65
65
|
else
|
66
|
-
::Mongo::Object.build super(selector,
|
66
|
+
::Mongo::Object.build super(selector, options, &block)
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
|
-
def each selector = {},
|
71
|
-
|
72
|
-
if
|
73
|
-
super selector,
|
70
|
+
def each selector = {}, options = {}, &block
|
71
|
+
options = options.clone
|
72
|
+
if options.delete(:object) == false
|
73
|
+
super selector, options, &block
|
74
74
|
else
|
75
|
-
super selector,
|
75
|
+
super selector, options do |doc|
|
76
76
|
block.call ::Mongo::Object.build(doc)
|
77
77
|
end
|
78
78
|
end
|
data/lib/mongo/object/spec.rb
CHANGED
@@ -40,7 +40,11 @@ shared_examples_for 'embedded object CRUD' do
|
|
40
40
|
|
41
41
|
# update
|
42
42
|
@player.missions.first.stats[:units] = 9
|
43
|
-
|
43
|
+
mission = @mission_class.new.tap do |m|
|
44
|
+
m.name = 'Desperate Alliance'
|
45
|
+
m.stats = {buildings: 11, units: 40}
|
46
|
+
end
|
47
|
+
@player.missions << mission
|
44
48
|
db.players.save @player
|
45
49
|
db.players.count.should == 1
|
46
50
|
db.players.first.should == @player
|
@@ -50,4 +54,10 @@ shared_examples_for 'embedded object CRUD' do
|
|
50
54
|
db.players.destroy @player
|
51
55
|
db.players.count.should == 0
|
52
56
|
end
|
57
|
+
|
58
|
+
it "embedded object should have :_parent reference to the main object" do
|
59
|
+
db.players.save @player
|
60
|
+
player = db.players.first
|
61
|
+
player.missions.first._parent.should == player
|
62
|
+
end
|
53
63
|
end
|
data/spec/object/spec_helper.rb
CHANGED
@@ -7,12 +7,12 @@ require 'driver/spec_helper'
|
|
7
7
|
|
8
8
|
# To simplify callback expectations
|
9
9
|
module RSpec::CallbackHelper
|
10
|
-
def run_before_callbacks method_name,
|
10
|
+
def run_before_callbacks method_name, options = {}
|
11
11
|
callback_method_name = :"before_#{method_name}"
|
12
12
|
respond_to?(callback_method_name) ? send(callback_method_name) : true
|
13
13
|
end
|
14
14
|
|
15
|
-
def run_after_callbacks method_name,
|
15
|
+
def run_after_callbacks method_name, options = {}
|
16
16
|
callback_method_name = :"after_#{method_name}"
|
17
17
|
respond_to?(callback_method_name) ? send(callback_method_name) : true
|
18
18
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongodb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-09-02 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email:
|