mongodb 0.0.13 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +3 -4
- data/lib/mongo/driver.rb +3 -3
- data/lib/mongo/driver/collection.rb +10 -22
- data/lib/mongo/driver/dynamic_finders.rb +1 -3
- data/lib/mongo/object.rb +2 -16
- data/lib/mongo/object/{object_helper.rb → collection_helper.rb} +12 -15
- data/lib/mongo/object/load.rb +22 -0
- data/lib/mongo/object/object.rb +60 -254
- data/lib/mongo/object/spec.rb +13 -3
- data/lib/mongo/object/spec/shared_object_crud.rb +124 -0
- data/lib/mongo/object/support.rb +15 -0
- data/lib/mongodb/gems.rb +1 -1
- data/spec/driver/collection_spec.rb +13 -43
- data/spec/driver/connection_spec.rb +1 -1
- data/spec/driver/database_spec.rb +1 -1
- data/spec/driver/dynamic_finders_spec.rb +4 -4
- data/spec/driver/fixes_spec.rb +2 -2
- data/spec/driver/hash_crud_spec.rb +69 -0
- data/spec/driver/spec_helper.rb +1 -3
- data/spec/migration/migration_spec.rb +4 -4
- data/spec/object/miscellaneous_spec.rb +23 -0
- data/spec/object/object_crud_spec.rb +57 -0
- data/spec/object/spec_helper.rb +1 -14
- metadata +11 -12
- data/lib/mongo/object/spec/crud_shared.rb +0 -63
- data/spec/driver/crud_spec.rb +0 -70
- data/spec/driver/hash_helper_spec.rb +0 -25
- data/spec/object/callbacks_spec.rb +0 -115
- data/spec/object/crud_spec.rb +0 -61
- data/spec/object/validation_spec.rb +0 -79
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'object/spec_helper'
|
2
|
+
require 'mongo/object/spec/shared_object_crud'
|
3
|
+
|
4
|
+
describe "Object CRUD" do
|
5
|
+
with_mongo
|
6
|
+
|
7
|
+
describe 'single object' do
|
8
|
+
it_should_behave_like "single object CRUD"
|
9
|
+
|
10
|
+
before do
|
11
|
+
class Unit
|
12
|
+
include Mongo::Object
|
13
|
+
|
14
|
+
attr_accessor :name, :info
|
15
|
+
def == o; [self.class, name, info] == [o.class, o.name, o.info] end
|
16
|
+
end
|
17
|
+
|
18
|
+
@unit = Unit.new.tap do |o|
|
19
|
+
o.name, o.info = 'Zeratul', 'Dark Templar'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
after{remove_constants :Unit}
|
23
|
+
|
24
|
+
it "should allow to read object as hash, without unmarshalling" do
|
25
|
+
db.units.save! @unit
|
26
|
+
db.units.first({}, object: false).is_a?(Hash).should be_true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'embedded object' do
|
31
|
+
it_should_behave_like 'embedded object CRUD'
|
32
|
+
|
33
|
+
before do
|
34
|
+
class Unit
|
35
|
+
include Mongo::Object
|
36
|
+
|
37
|
+
attr_accessor :items
|
38
|
+
def == o; [self.class, self.items] == [o.class, o.items] end
|
39
|
+
|
40
|
+
class Item
|
41
|
+
include Mongo::Object
|
42
|
+
|
43
|
+
attr_accessor :name
|
44
|
+
def == o; [self.class, self.name] == [o.class, o.name] end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
@item_class = Unit::Item
|
49
|
+
@unit = Unit.new
|
50
|
+
@unit.items = [
|
51
|
+
Unit::Item.new.tap{|o| o.name = 'Psionic blade'},
|
52
|
+
Unit::Item.new.tap{|o| o.name = 'Plasma shield'},
|
53
|
+
]
|
54
|
+
end
|
55
|
+
after{remove_constants :Unit}
|
56
|
+
end
|
57
|
+
end
|
data/spec/object/spec_helper.rb
CHANGED
@@ -3,17 +3,4 @@ require 'mongo/object'
|
|
3
3
|
require 'rspec_ext'
|
4
4
|
require 'mongo/object/spec'
|
5
5
|
|
6
|
-
require 'driver/spec_helper'
|
7
|
-
|
8
|
-
# To simplify callback expectations
|
9
|
-
module RSpec::CallbackHelper
|
10
|
-
def run_before_callbacks method_name, options = {}
|
11
|
-
callback_method_name = :"before_#{method_name}"
|
12
|
-
respond_to?(callback_method_name) ? send(callback_method_name) : true
|
13
|
-
end
|
14
|
-
|
15
|
-
def run_after_callbacks method_name, options = {}
|
16
|
-
callback_method_name = :"after_#{method_name}"
|
17
|
-
respond_to?(callback_method_name) ? send(callback_method_name) : true
|
18
|
-
end
|
19
|
-
end
|
6
|
+
require 'driver/spec_helper'
|
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: 2.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,19 +9,19 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-11-18 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongo
|
16
|
-
requirement: &
|
16
|
+
requirement: &2849860 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '1.
|
21
|
+
version: '1.4'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2849860
|
25
25
|
description:
|
26
26
|
email:
|
27
27
|
executables: []
|
@@ -41,26 +41,25 @@ files:
|
|
41
41
|
- lib/mongo/migration/migration.rb
|
42
42
|
- lib/mongo/migration/tasks.rb
|
43
43
|
- lib/mongo/migration.rb
|
44
|
+
- lib/mongo/object/collection_helper.rb
|
45
|
+
- lib/mongo/object/load.rb
|
44
46
|
- lib/mongo/object/object.rb
|
45
|
-
- lib/mongo/object/
|
46
|
-
- lib/mongo/object/spec/crud_shared.rb
|
47
|
+
- lib/mongo/object/spec/shared_object_crud.rb
|
47
48
|
- lib/mongo/object/spec.rb
|
48
49
|
- lib/mongo/object/support.rb
|
49
50
|
- lib/mongo/object.rb
|
50
51
|
- lib/mongodb/gems.rb
|
51
52
|
- spec/driver/collection_spec.rb
|
52
53
|
- spec/driver/connection_spec.rb
|
53
|
-
- spec/driver/crud_spec.rb
|
54
54
|
- spec/driver/database_spec.rb
|
55
55
|
- spec/driver/dynamic_finders_spec.rb
|
56
56
|
- spec/driver/fixes_spec.rb
|
57
|
-
- spec/driver/
|
57
|
+
- spec/driver/hash_crud_spec.rb
|
58
58
|
- spec/driver/spec_helper.rb
|
59
59
|
- spec/migration/migration_spec.rb
|
60
|
-
- spec/object/
|
61
|
-
- spec/object/
|
60
|
+
- spec/object/miscellaneous_spec.rb
|
61
|
+
- spec/object/object_crud_spec.rb
|
62
62
|
- spec/object/spec_helper.rb
|
63
|
-
- spec/object/validation_spec.rb
|
64
63
|
homepage: http://alexeypetrushin.github.com/mongodb
|
65
64
|
licenses: []
|
66
65
|
post_install_message:
|
@@ -1,63 +0,0 @@
|
|
1
|
-
shared_examples_for 'object CRUD' do
|
2
|
-
it 'crud' do
|
3
|
-
# read
|
4
|
-
db.units.count.should == 0
|
5
|
-
db.units.all.should == []
|
6
|
-
db.units.first.should == nil
|
7
|
-
|
8
|
-
# create
|
9
|
-
db.units.save(@zeratul).should be_true
|
10
|
-
@zeratul._id.should_not be_nil
|
11
|
-
|
12
|
-
# read
|
13
|
-
db.units.count.should == 1
|
14
|
-
db.units.all.should == [@zeratul]
|
15
|
-
db.units.first.should == @zeratul
|
16
|
-
db.units.first.object_id.should_not == @zeratul.object_id
|
17
|
-
|
18
|
-
# update
|
19
|
-
@zeratul.info = 'Killer of Cerebrates'
|
20
|
-
db.units.save(@zeratul).should be_true
|
21
|
-
db.units.count.should == 1
|
22
|
-
db.units.first(name: 'Zeratul').info.should == 'Killer of Cerebrates'
|
23
|
-
|
24
|
-
# destroy
|
25
|
-
db.units.destroy(@zeratul).should be_true
|
26
|
-
db.units.count.should == 0
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
shared_examples_for 'embedded object CRUD' do
|
31
|
-
it 'crud' do
|
32
|
-
# create
|
33
|
-
db.players.save(@player)
|
34
|
-
@player._id.should_not be_nil
|
35
|
-
|
36
|
-
# read
|
37
|
-
db.players.count.should == 1
|
38
|
-
db.players.first.should == @player
|
39
|
-
db.players.first.object_id.should_not == @players.object_id
|
40
|
-
|
41
|
-
# update
|
42
|
-
@player.missions.first.stats['units'] = 9
|
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
|
48
|
-
db.players.save @player
|
49
|
-
db.players.count.should == 1
|
50
|
-
db.players.first.should == @player
|
51
|
-
db.players.first.object_id.should_not == @player.object_id
|
52
|
-
|
53
|
-
# destroy
|
54
|
-
db.players.destroy @player
|
55
|
-
db.players.count.should == 0
|
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
|
63
|
-
end
|
data/spec/driver/crud_spec.rb
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
require 'driver/spec_helper'
|
2
|
-
|
3
|
-
describe "Hash CRUD" do
|
4
|
-
with_mongo
|
5
|
-
|
6
|
-
describe 'simple' do
|
7
|
-
before do
|
8
|
-
@zeratul = {'name' => 'Zeratul', 'info' => 'Dark Templar'}
|
9
|
-
end
|
10
|
-
|
11
|
-
it 'crud' do
|
12
|
-
# read
|
13
|
-
db.units.count.should == 0
|
14
|
-
db.units.all.should == []
|
15
|
-
db.units.first.should == nil
|
16
|
-
|
17
|
-
# create
|
18
|
-
db.units.save(@zeratul).should be_mongo_id
|
19
|
-
@zeratul['_id'].should be_mongo_id
|
20
|
-
|
21
|
-
# read
|
22
|
-
db.units.all.should == [@zeratul]
|
23
|
-
db.units.count.should == 1
|
24
|
-
db.units.first.should == @zeratul
|
25
|
-
|
26
|
-
# update
|
27
|
-
@zeratul['info'] = 'Killer of Cerebrates'
|
28
|
-
db.units.save @zeratul
|
29
|
-
db.units.count.should == 1
|
30
|
-
db.units.first(name: 'Zeratul')['info'].should == 'Killer of Cerebrates'
|
31
|
-
|
32
|
-
# destroy
|
33
|
-
db.units.destroy @zeratul
|
34
|
-
db.units.count.should == 0
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
describe 'embedded' do
|
39
|
-
before do
|
40
|
-
@player = {
|
41
|
-
'name' => 'Alex',
|
42
|
-
'missions' => [
|
43
|
-
{'name' => 'Wasteland', 'stats' => {'buildings' => 5, 'units' => 10}},
|
44
|
-
{'name' => 'Backwater Station', 'stats' => {'buildings' => 8, 'units' => 25}}
|
45
|
-
]
|
46
|
-
}
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'crud' do
|
50
|
-
# create
|
51
|
-
db.players.save(@player).should be_mongo_id
|
52
|
-
|
53
|
-
# read
|
54
|
-
db.players.count.should == 1
|
55
|
-
db.players.first.should == @player
|
56
|
-
|
57
|
-
# update
|
58
|
-
@player['missions'].first['stats']['units'] = 9
|
59
|
-
@player['missions'].push 'name' => 'Desperate Alliance', 'stats' => {'buildings' => 11, 'units' => 40}
|
60
|
-
db.players.save(@player).should_not be_nil
|
61
|
-
db.players.count.should == 1
|
62
|
-
db.players.first.should == @player
|
63
|
-
db.players.first.object_id.should_not == @player.object_id
|
64
|
-
|
65
|
-
# destroy
|
66
|
-
db.players.destroy @player
|
67
|
-
db.players.count.should == 0
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'driver/spec_helper'
|
2
|
-
|
3
|
-
describe "Collection" do
|
4
|
-
before do
|
5
|
-
@helper = Object.new
|
6
|
-
@helper.send :extend, Mongo::CollectionExt
|
7
|
-
end
|
8
|
-
|
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
|
25
|
-
end
|
@@ -1,115 +0,0 @@
|
|
1
|
-
require 'object/spec_helper'
|
2
|
-
|
3
|
-
describe 'Object callbacks' do
|
4
|
-
with_mongo
|
5
|
-
|
6
|
-
[Object, Array, Hash].each do |embedded_object_superclass|
|
7
|
-
embedded_object_class = nil
|
8
|
-
before :all do
|
9
|
-
class MainObject
|
10
|
-
include Mongo::Object, RSpec::CallbackHelper
|
11
|
-
|
12
|
-
attr_accessor :children
|
13
|
-
end
|
14
|
-
|
15
|
-
embedded_object_class = Class.new embedded_object_superclass do
|
16
|
-
include Mongo::Object, RSpec::CallbackHelper
|
17
|
-
end
|
18
|
-
end
|
19
|
-
after(:all){remove_constants :MainObject}
|
20
|
-
|
21
|
-
before do
|
22
|
-
@child = embedded_object_class.new
|
23
|
-
@object = MainObject.new
|
24
|
-
@object.children = [@child]
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'create' do
|
28
|
-
%w(before_validate after_validate before_save before_create after_create after_save).each do |name|
|
29
|
-
@object.should_receive(name).once.ordered.and_return(true)
|
30
|
-
@child.should_receive(name).once.ordered.and_return(true)
|
31
|
-
end
|
32
|
-
|
33
|
-
db.objects.save(@object).should be_true
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'update' do
|
37
|
-
db.objects.save(@object).should be_true
|
38
|
-
|
39
|
-
%w(before_validate after_validate before_save before_update after_update after_save).each do |name|
|
40
|
-
@object.should_receive(name).once.ordered.and_return(true)
|
41
|
-
@child.should_receive(name).once.ordered.and_return(true)
|
42
|
-
end
|
43
|
-
db.objects.save(@object).should be_true
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'destroy' do
|
47
|
-
db.objects.save(@object).should be_true
|
48
|
-
|
49
|
-
%w(before_validate after_validate before_destroy after_destroy).each do |name|
|
50
|
-
@object.should_receive(name).once.ordered.and_return(true)
|
51
|
-
@child.should_receive(name).once.ordered.and_return(true)
|
52
|
-
end
|
53
|
-
db.objects.destroy(@object).should be_true
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'should be able skip callbacks' do
|
57
|
-
@object.should_not_receive(:run_callbacks)
|
58
|
-
@child.should_not_receive(:run_callbacks)
|
59
|
-
|
60
|
-
db.objects.save(@object, callbacks: false).should be_true
|
61
|
-
db.objects.count.should == 1
|
62
|
-
db.objects.save(@object, callbacks: false).should be_true
|
63
|
-
db.objects.count.should == 1
|
64
|
-
db.objects.destroy(@object, callbacks: false).should be_true
|
65
|
-
db.objects.count.should == 0
|
66
|
-
end
|
67
|
-
|
68
|
-
it 'should be able interrupt CRUD' do
|
69
|
-
@child.stub! :run_before_callbacks do |method_name|
|
70
|
-
false if method_name == :create
|
71
|
-
end
|
72
|
-
db.objects.save(@object).should be_false
|
73
|
-
db.objects.count.should == 0
|
74
|
-
end
|
75
|
-
|
76
|
-
describe "embedded" do
|
77
|
-
it 'should fire :destroy on detached objects' do
|
78
|
-
db.objects.save(@object).should be_true
|
79
|
-
@object.children.clear
|
80
|
-
@child.should_receive(:before_destroy).once.and_return(true)
|
81
|
-
db.objects.destroy(@object).should be_true
|
82
|
-
end
|
83
|
-
|
84
|
-
it 'should fire :destroy on deleted objects in update' do
|
85
|
-
db.objects.save(@object).should be_true
|
86
|
-
@object.children.clear
|
87
|
-
@child.should_receive(:before_destroy).once.and_return(true)
|
88
|
-
db.objects.save(@object).should be_true
|
89
|
-
end
|
90
|
-
|
91
|
-
it 'should fire :create on new objects in update' do
|
92
|
-
db.objects.save(@object).should be_true
|
93
|
-
child2 = embedded_object_class.new
|
94
|
-
@object.children << child2
|
95
|
-
child2.should_receive(:before_create).once.and_return(true)
|
96
|
-
child2.should_not_receive(:before_update)
|
97
|
-
db.objects.save(@object).should be_true
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
it "should fire :after_build callback after building the object" do
|
102
|
-
class SpecialMainObject < MainObject
|
103
|
-
def run_after_callbacks method, options
|
104
|
-
self.class.build_callback if method == :build
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
player = SpecialMainObject.new
|
109
|
-
db.objects.save! player
|
110
|
-
|
111
|
-
SpecialMainObject.should_receive :build_callback
|
112
|
-
db.objects.first
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
data/spec/object/crud_spec.rb
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
require 'object/spec_helper'
|
2
|
-
require 'mongo/object/spec/crud_shared'
|
3
|
-
|
4
|
-
describe "Object CRUD" do
|
5
|
-
with_mongo
|
6
|
-
|
7
|
-
describe 'simple' do
|
8
|
-
before :all do
|
9
|
-
class Unit2
|
10
|
-
include Mongo::Object
|
11
|
-
|
12
|
-
def initialize name = nil, info = nil; @name, @info = name, info end
|
13
|
-
attr_accessor :name, :info
|
14
|
-
def == o; [self.class, name, info] == [o.class, o.respond_to(:name), o.respond_to(:info)] end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
after(:all){remove_constants :Unit2}
|
18
|
-
|
19
|
-
before do
|
20
|
-
@zeratul = Unit2.new 'Zeratul', 'Dark Templar'
|
21
|
-
end
|
22
|
-
|
23
|
-
it_should_behave_like "object CRUD"
|
24
|
-
|
25
|
-
it "should allow to read object as hash" do
|
26
|
-
db.units.save! @zeratul
|
27
|
-
db.units.first({}, object: false).is_a?(Hash).should be_true
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
describe 'embedded' do
|
32
|
-
before :all do
|
33
|
-
class Player2
|
34
|
-
include Mongo::Object
|
35
|
-
|
36
|
-
attr_accessor :missions
|
37
|
-
def == o; [self.class, self.missions] == [o.class, o.respond_to(:missions)] end
|
38
|
-
|
39
|
-
class Mission
|
40
|
-
include Mongo::Object
|
41
|
-
|
42
|
-
def initialize name = nil, stats = nil; @name, @stats = name, stats end
|
43
|
-
attr_accessor :name, :stats
|
44
|
-
def == o; [self.class, self.name, self.stats] == [o.class, o.respond_to(:name), o.respond_to(:stats)] end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
after(:all){remove_constants :Player2}
|
49
|
-
|
50
|
-
before do
|
51
|
-
@mission_class = Player2::Mission
|
52
|
-
@player = Player2.new
|
53
|
-
@player.missions = [
|
54
|
-
Player2::Mission.new('Wasteland', {'buildings' => 5, 'units' => 10}),
|
55
|
-
Player2::Mission.new('Backwater Station', {'buildings' => 8, 'units' => 25}),
|
56
|
-
]
|
57
|
-
end
|
58
|
-
|
59
|
-
it_should_behave_like 'embedded object CRUD'
|
60
|
-
end
|
61
|
-
end
|