mongodb 0.0.8 → 0.0.9
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/mongo/driver.rb +1 -0
- data/lib/mongo/driver/collection.rb +18 -13
- data/lib/mongo/object/object.rb +3 -6
- data/lib/mongo/object/spec/crud_shared.rb +2 -2
- data/readme.md +2 -2
- data/spec/driver/collection_spec.rb +33 -32
- data/spec/driver/crud_spec.rb +10 -10
- data/spec/driver/dynamic_finders_spec.rb +1 -1
- data/spec/driver/hash_helper_spec.rb +16 -15
- data/spec/driver/spec_helper.rb +0 -1
- data/spec/object/crud_spec.rb +3 -3
- metadata +2 -2
data/lib/mongo/driver.rb
CHANGED
@@ -12,6 +12,14 @@ module Mongo::CollectionExt
|
|
12
12
|
def insert_with_ext args, options = {}
|
13
13
|
result = insert_without_ext args, reverse_merge_defaults(options, :safe)
|
14
14
|
|
15
|
+
# for some strange reason MongoDB Ruby driver
|
16
|
+
# uses Strings for all keys but _id.
|
17
|
+
# It's inconvinient, fixing it.
|
18
|
+
if Mongo.defaults[:convert_id_to_string]
|
19
|
+
list = args.is_a?(Array) ? args : [args]
|
20
|
+
list.each{|h| h['_id'] = h.delete :_id}
|
21
|
+
end
|
22
|
+
|
15
23
|
# fix for mongodriver, it will return single result if we supply [doc] as args
|
16
24
|
(args.is_a?(Array) and !result.is_a?(Array)) ? [result] : result
|
17
25
|
end
|
@@ -48,9 +56,7 @@ module Mongo::CollectionExt
|
|
48
56
|
#
|
49
57
|
def first selector = {}, options = {}
|
50
58
|
selector = convert_underscore_to_dollar_in_selector selector if selector.is_a? Hash
|
51
|
-
|
52
|
-
h = find_one selector, options
|
53
|
-
symbolize_doc h
|
59
|
+
find_one selector, options
|
54
60
|
end
|
55
61
|
|
56
62
|
def first! selector = {}, options = {}
|
@@ -74,7 +80,6 @@ module Mongo::CollectionExt
|
|
74
80
|
begin
|
75
81
|
cursor = find selector, reverse_merge_defaults(options, :batch_size)
|
76
82
|
cursor.each do |doc|
|
77
|
-
doc = symbolize_doc doc
|
78
83
|
block.call doc
|
79
84
|
end
|
80
85
|
nil
|
@@ -108,15 +113,15 @@ module Mongo::CollectionExt
|
|
108
113
|
h
|
109
114
|
end
|
110
115
|
|
111
|
-
# symbolizing hashes
|
112
|
-
def symbolize_doc doc
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
end
|
116
|
+
# # symbolizing hashes
|
117
|
+
# def symbolize_doc doc
|
118
|
+
# return doc unless Mongo.defaults[:symbolize]
|
119
|
+
#
|
120
|
+
# Mongo::CollectionExt.convert_doc doc do |k, v, result|
|
121
|
+
# k = k.to_sym if k.is_a? String
|
122
|
+
# result[k] = v
|
123
|
+
# end
|
124
|
+
# end
|
120
125
|
|
121
126
|
# replaces :_lt to :$lt in query
|
122
127
|
def convert_underscore_to_dollar_in_selector selector
|
data/lib/mongo/object/object.rb
CHANGED
@@ -29,7 +29,7 @@ module Mongo::Object
|
|
29
29
|
with_object_callbacks :create, options do |options|
|
30
30
|
doc = ::Mongo::Object.to_mongo self
|
31
31
|
collection.create(doc, options)
|
32
|
-
self._id = doc[
|
32
|
+
self._id = doc['_id']
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
@@ -88,7 +88,6 @@ module Mongo::Object
|
|
88
88
|
# converts object to document (also works with nested & arrays)
|
89
89
|
def to_mongo obj
|
90
90
|
return obj.to_mongo if obj.respond_to? :to_mongo
|
91
|
-
symbolize = ::Mongo.defaults[:symbolize]
|
92
91
|
|
93
92
|
if obj.is_a? Hash
|
94
93
|
doc = {}
|
@@ -104,15 +103,13 @@ module Mongo::Object
|
|
104
103
|
# copying instance variables
|
105
104
|
each_object_instance_variable obj do |iv_name, v|
|
106
105
|
k = iv_name.to_s[1..-1]
|
107
|
-
k = k.to_sym if symbolize
|
108
106
|
doc[k] = to_mongo v
|
109
107
|
end
|
110
108
|
|
111
109
|
# adding _id & _class
|
112
|
-
id_key, class_key = symbolize ? [:_id, :_class] : ['_id', '_class']
|
113
110
|
id = instance_variable_get('@_id')
|
114
|
-
doc[
|
115
|
-
doc[
|
111
|
+
doc['_id'] = id if id
|
112
|
+
doc['_class'] = obj.class.name
|
116
113
|
|
117
114
|
doc
|
118
115
|
else # simple type
|
@@ -39,10 +39,10 @@ shared_examples_for 'embedded object CRUD' do
|
|
39
39
|
db.players.first.object_id.should_not == @players.object_id
|
40
40
|
|
41
41
|
# update
|
42
|
-
@player.missions.first.stats[
|
42
|
+
@player.missions.first.stats['units'] = 9
|
43
43
|
mission = @mission_class.new.tap do |m|
|
44
44
|
m.name = 'Desperate Alliance'
|
45
|
-
m.stats = {buildings
|
45
|
+
m.stats = {'buildings' => 11, 'units' => 40}
|
46
46
|
end
|
47
47
|
@player.missions << mission
|
48
48
|
db.players.save @player
|
data/readme.md
CHANGED
@@ -20,7 +20,7 @@ These enhancements alter the driver's API and made it more simple and intuitive.
|
|
20
20
|
require 'mongo/driver'
|
21
21
|
|
22
22
|
# Changing some defaults.
|
23
|
-
Mongo.defaults.merge!
|
23
|
+
Mongo.defaults.merge! multi: true, safe: true
|
24
24
|
|
25
25
|
# Connection & db.
|
26
26
|
connection = Mongo::Connection.new
|
@@ -124,7 +124,7 @@ Note: the :initialize method should allow to create object without arguments.
|
|
124
124
|
``` ruby
|
125
125
|
# Connecting to MongoDB.
|
126
126
|
require 'mongo/object'
|
127
|
-
Mongo.defaults.merge!
|
127
|
+
Mongo.defaults.merge! multi: true, safe: true
|
128
128
|
connection = Mongo::Connection.new
|
129
129
|
db = connection.db 'default_test'
|
130
130
|
db.units.drop
|
@@ -9,47 +9,48 @@ describe "Collection" do
|
|
9
9
|
|
10
10
|
# update
|
11
11
|
db.units.update({race: 'Protoss'}, :$set => {status: 'dead'})
|
12
|
-
db.units.all.collect{|u| u[
|
12
|
+
db.units.all.collect{|u| u['status']}.should == %w(dead dead)
|
13
13
|
|
14
14
|
# destroy
|
15
15
|
db.units.destroy race: 'Protoss'
|
16
16
|
db.units.count.should == 0
|
17
17
|
end
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
end
|
19
|
+
# Discarded
|
20
|
+
# describe "symbolize" do
|
21
|
+
# it 'should always return symbolized hashes' do
|
22
|
+
# zeratul = {name: 'Zeratul'}
|
23
|
+
# db.units.save(zeratul).should be_mongo_id
|
24
|
+
# r = db.units.first(name: 'Zeratul')
|
25
|
+
# r[:_id].should be_mongo_id
|
26
|
+
# r['_id'].should be_nil
|
27
|
+
# r[:name].should == 'Zeratul'
|
28
|
+
# r['name'].should be_nil
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# it "should be able to disable symbolization" do
|
32
|
+
# old = Mongo.defaults[:symbolize]
|
33
|
+
# begin
|
34
|
+
# Mongo.defaults[:symbolize] = false
|
35
|
+
#
|
36
|
+
# zeratul = {name: 'Zeratul'}
|
37
|
+
# db.units.save(zeratul).should be_mongo_id
|
38
|
+
# r = db.units.first(name: 'Zeratul')
|
39
|
+
# r[:_id].should be_nil
|
40
|
+
# r['_id'].should be_mongo_id
|
41
|
+
# r[:name].should be_nil
|
42
|
+
# r['name'].should == 'Zeratul'
|
43
|
+
# ensure
|
44
|
+
# Mongo.defaults[:symbolize] = old
|
45
|
+
# end
|
46
|
+
# end
|
47
|
+
# end
|
47
48
|
|
48
49
|
it "first" do
|
49
50
|
db.units.first.should be_nil
|
50
51
|
zeratul = {name: 'Zeratul'}
|
51
52
|
db.units.save(zeratul).should be_mongo_id
|
52
|
-
db.units.first(name: 'Zeratul')[
|
53
|
+
db.units.first(name: 'Zeratul')['name'].should == 'Zeratul'
|
53
54
|
end
|
54
55
|
|
55
56
|
it 'all' do
|
@@ -60,12 +61,12 @@ describe "Collection" do
|
|
60
61
|
|
61
62
|
list = db.units.all(name: 'Zeratul')
|
62
63
|
list.size.should == 1
|
63
|
-
list.first[
|
64
|
+
list.first['name'].should == 'Zeratul'
|
64
65
|
|
65
66
|
# with block
|
66
67
|
list = []; db.units.all{|o| list << o}
|
67
68
|
list.size.should == 1
|
68
|
-
list.first[
|
69
|
+
list.first['name'].should == 'Zeratul'
|
69
70
|
end
|
70
71
|
|
71
72
|
it 'count' do
|
data/spec/driver/crud_spec.rb
CHANGED
@@ -5,7 +5,7 @@ describe "Hash CRUD" do
|
|
5
5
|
|
6
6
|
describe 'simple' do
|
7
7
|
before do
|
8
|
-
@zeratul = {name
|
8
|
+
@zeratul = {'name' => 'Zeratul', 'info' => 'Dark Templar'}
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'crud' do
|
@@ -16,7 +16,7 @@ describe "Hash CRUD" do
|
|
16
16
|
|
17
17
|
# create
|
18
18
|
db.units.save(@zeratul).should be_mongo_id
|
19
|
-
@zeratul[
|
19
|
+
@zeratul['_id'].should be_mongo_id
|
20
20
|
|
21
21
|
# read
|
22
22
|
db.units.all.should == [@zeratul]
|
@@ -24,10 +24,10 @@ describe "Hash CRUD" do
|
|
24
24
|
db.units.first.should == @zeratul
|
25
25
|
|
26
26
|
# update
|
27
|
-
@zeratul[
|
27
|
+
@zeratul['info'] = 'Killer of Cerebrates'
|
28
28
|
db.units.save @zeratul
|
29
29
|
db.units.count.should == 1
|
30
|
-
db.units.first(name: 'Zeratul')[
|
30
|
+
db.units.first(name: 'Zeratul')['info'].should == 'Killer of Cerebrates'
|
31
31
|
|
32
32
|
# destroy
|
33
33
|
db.units.destroy @zeratul
|
@@ -38,10 +38,10 @@ describe "Hash CRUD" do
|
|
38
38
|
describe 'embedded' do
|
39
39
|
before do
|
40
40
|
@player = {
|
41
|
-
name
|
42
|
-
missions
|
43
|
-
{name
|
44
|
-
{name
|
41
|
+
'name' => 'Alex',
|
42
|
+
'missions' => [
|
43
|
+
{'name' => 'Wasteland', 'stats' => {'buildings' => 5, 'units' => 10}},
|
44
|
+
{'name' => 'Backwater Station', 'stats' => {'buildings' => 8, 'units' => 25}}
|
45
45
|
]
|
46
46
|
}
|
47
47
|
end
|
@@ -55,8 +55,8 @@ describe "Hash CRUD" do
|
|
55
55
|
db.players.first.should == @player
|
56
56
|
|
57
57
|
# update
|
58
|
-
@player[
|
59
|
-
@player[
|
58
|
+
@player['missions'].first['stats']['units'] = 9
|
59
|
+
@player['missions'].push 'name' => 'Desperate Alliance', 'stats' => {'buildings' => 11, 'units' => 40}
|
60
60
|
db.players.save(@player).should_not be_nil
|
61
61
|
db.players.count.should == 1
|
62
62
|
db.players.first.should == @player
|
@@ -45,6 +45,6 @@ describe "Dynamic Finders" do
|
|
45
45
|
it 'integration with collection' do
|
46
46
|
db.units.first_by_name('Jim').should be_nil
|
47
47
|
db.units.save name: 'Jim'
|
48
|
-
db.units.first_by_name('Jim')[
|
48
|
+
db.units.first_by_name('Jim')['name'].should == 'Jim'
|
49
49
|
end
|
50
50
|
end
|
@@ -6,19 +6,20 @@ describe "Collection" do
|
|
6
6
|
@helper.send :extend, Mongo::CollectionExt
|
7
7
|
end
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
9
|
+
# Discarded
|
10
|
+
# it "symbolize" do
|
11
|
+
# @helper.send(:symbolize_doc, {
|
12
|
+
# 'a' => 1,
|
13
|
+
# 'b' => {
|
14
|
+
# 'c' => 2,
|
15
|
+
# 'd' => [{'e' => 3}]
|
16
|
+
# }
|
17
|
+
# }).should == {
|
18
|
+
# a: 1,
|
19
|
+
# b: {
|
20
|
+
# c: 2,
|
21
|
+
# d: [{e: 3}]
|
22
|
+
# }
|
23
|
+
# }
|
24
|
+
# end
|
24
25
|
end
|
data/spec/driver/spec_helper.rb
CHANGED
data/spec/object/crud_spec.rb
CHANGED
@@ -24,7 +24,7 @@ describe "Object CRUD" do
|
|
24
24
|
|
25
25
|
it "should allow to read object as hash" do
|
26
26
|
db.units.save! @zeratul
|
27
|
-
db.units.first({}, object: false).
|
27
|
+
db.units.first({}, object: false).is_a?(Hash).should be_true
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
@@ -51,8 +51,8 @@ describe "Object CRUD" do
|
|
51
51
|
@mission_class = Player2::Mission
|
52
52
|
@player = Player2.new
|
53
53
|
@player.missions = [
|
54
|
-
Player2::Mission.new('Wasteland', {buildings
|
55
|
-
Player2::Mission.new('Backwater Station', {buildings
|
54
|
+
Player2::Mission.new('Wasteland', {'buildings' => 5, 'units' => 10}),
|
55
|
+
Player2::Mission.new('Backwater Station', {'buildings' => 8, 'units' => 25}),
|
56
56
|
]
|
57
57
|
end
|
58
58
|
|
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.9
|
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-09-
|
12
|
+
date: 2011-09-04 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email:
|