mongo_db 0.1.9 → 0.1.10
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_db/driver/{core/collection.rb → collection.rb} +13 -5
- data/lib/mongo_db/driver/{core/database.rb → database.rb} +0 -0
- data/lib/mongo_db/driver/dynamic_finders.rb +41 -0
- data/lib/mongo_db/driver.rb +33 -2
- data/lib/mongo_db/migration/definition.rb +19 -0
- data/lib/mongo_db/migration/migration.rb +68 -0
- data/lib/mongo_db/migration/tasks.rb +19 -0
- data/lib/mongo_db/migration.rb +8 -0
- data/lib/mongo_db/model/assignment.rb +54 -0
- data/lib/mongo_db/model/callbacks.rb +36 -0
- data/lib/mongo_db/model/crud.rb +28 -0
- data/lib/mongo_db/model/db.rb +53 -0
- data/lib/mongo_db/model/misc.rb +14 -0
- data/lib/mongo_db/model/model.rb +10 -0
- data/lib/mongo_db/model/query.rb +36 -0
- data/lib/mongo_db/model/scope.rb +99 -0
- data/lib/mongo_db/model/spec.rb +12 -0
- data/lib/mongo_db/model/support/types.rb +110 -0
- data/lib/mongo_db/model/validation.rb +5 -0
- data/lib/mongo_db/model.rb +30 -0
- data/lib/mongo_db/object/object_serializer.rb +20 -21
- data/readme.md +132 -19
- data/spec/driver/{core/collection_spec.rb → collection_spec.rb} +13 -0
- data/spec/driver/{core/crud_spec.rb → crud_spec.rb} +0 -0
- data/spec/driver/{core/database_spec.rb → database_spec.rb} +0 -0
- data/spec/driver/dynamic_finders_spec.rb +50 -0
- data/spec/driver/{core/hash_helper_spec.rb → hash_helper_spec.rb} +0 -0
- data/spec/{model/example.rb → integration/am_conversion_spec.rb} +0 -0
- data/spec/integration/am_validation_spec.rb +40 -0
- data/spec/migration/migration_spec.rb +60 -0
- data/spec/model/assignment_spec.rb +79 -0
- data/spec/model/callbacks_spec.rb +47 -0
- data/spec/model/{model_crud.rb → crud_spec.rb} +46 -36
- data/spec/model/db_spec.rb +63 -0
- data/spec/model/misc_spec.rb +32 -0
- data/spec/model/query_spec.rb +47 -0
- data/spec/model/scope_spec.rb +149 -0
- data/spec/model/spec_helper.rb +4 -0
- data/spec/model/validation_spec.rb +37 -0
- data/spec/object/callbacks_spec.rb +6 -4
- data/spec/object/crud_shared.rb +1 -1
- data/spec/object/crud_spec.rb +15 -10
- data/spec/object/spec_helper.rb +3 -2
- data/spec/object/validation_spec.rb +4 -2
- metadata +39 -18
- data/lib/mongo_db/driver/core.rb +0 -29
- data/lib/mongo_db/driver/more/collection_finders.rb +0 -43
- data/lib/mongo_db/driver/more.rb +0 -10
- data/spec/driver/more/querying_spec.rb +0 -59
- data/spec/model/callbacks.rb +0 -100
- data/spec/test.rb +0 -10
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'model/spec_helper'
|
2
|
+
|
3
|
+
describe 'Model callbacks' do
|
4
|
+
with_mongo
|
5
|
+
|
6
|
+
after{remove_constants :User, :Writer}
|
7
|
+
|
8
|
+
it "should update attributes" do
|
9
|
+
class User
|
10
|
+
inherit Mongo::Model
|
11
|
+
|
12
|
+
attr_accessor :name, :active, :age, :banned
|
13
|
+
end
|
14
|
+
|
15
|
+
u = User.new
|
16
|
+
u.set name: 'Alex', active: '1', age: '31', banned: '0'
|
17
|
+
[u.name, u.active, u.age, u.banned].should == ['Alex', '1', '31', '0']
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should update only specified attributes" do
|
21
|
+
class User
|
22
|
+
inherit Mongo::Model
|
23
|
+
|
24
|
+
attr_accessor :name, :active, :age, :banned
|
25
|
+
|
26
|
+
assignment do
|
27
|
+
name String, true
|
28
|
+
active Boolean, true
|
29
|
+
age Integer, true
|
30
|
+
banned Boolean
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
u = User.new
|
35
|
+
u.set name: 'Alex', active: '1', age: '31', password: 'fake'
|
36
|
+
[u.name, u.active, u.age, u.banned].should == ['Alex', true, 31, nil]
|
37
|
+
|
38
|
+
# should allow to forcefully cast and update any attribute
|
39
|
+
u.set! banned: '0'
|
40
|
+
u.banned.should == false
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should inherit assignment rules" do
|
44
|
+
class User
|
45
|
+
inherit Mongo::Model
|
46
|
+
|
47
|
+
attr_accessor :age
|
48
|
+
|
49
|
+
assignment do
|
50
|
+
age Integer, true
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class Writer < User
|
55
|
+
attr_accessor :posts
|
56
|
+
|
57
|
+
assignment do
|
58
|
+
posts Integer, true
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
u = Writer.new
|
63
|
+
u.set age: '20', posts: '12'
|
64
|
+
[u.age, u.posts].should == [20, 12]
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'casting smoke test' do
|
68
|
+
[
|
69
|
+
Boolean, '1', true,
|
70
|
+
Date, '2011-08-23', Date.parse('2011-08-23'),
|
71
|
+
Float, '1.2', 1.2,
|
72
|
+
Integer, '10', 10,
|
73
|
+
String, 'Hi', 'Hi',
|
74
|
+
Time, '2011-08-23', Date.parse('2011-08-23').to_time
|
75
|
+
].each_slice 3 do |type, raw, expected|
|
76
|
+
type.cast(raw).should == expected
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'model/spec_helper'
|
2
|
+
|
3
|
+
describe 'Model callbacks' do
|
4
|
+
with_mongo
|
5
|
+
|
6
|
+
after(:all){remove_constants :TheModel, :Player}
|
7
|
+
|
8
|
+
it "callback integration" do
|
9
|
+
class TheModel
|
10
|
+
inherit Mongo::Model
|
11
|
+
end
|
12
|
+
|
13
|
+
model = TheModel.new
|
14
|
+
|
15
|
+
model.should_receive(:run_before_callbacks).with(:save, {method: :save})
|
16
|
+
model._run_callbacks :before, :save
|
17
|
+
end
|
18
|
+
|
19
|
+
it "integration smoke test" do
|
20
|
+
class Player
|
21
|
+
inherit Mongo::Model
|
22
|
+
|
23
|
+
before_validate :before_validate_check
|
24
|
+
after_save :after_save_check
|
25
|
+
|
26
|
+
attr_accessor :missions
|
27
|
+
|
28
|
+
class Mission
|
29
|
+
inherit Mongo::Model
|
30
|
+
|
31
|
+
before_validate :before_validate_check
|
32
|
+
after_save :after_save_check
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
mission = Player::Mission.new
|
37
|
+
player = Player.new
|
38
|
+
player.missions = [mission]
|
39
|
+
|
40
|
+
player.should_receive(:before_validate_check).once.ordered.and_return(nil)
|
41
|
+
mission.should_receive(:before_validate_check).once.ordered.and_return(nil)
|
42
|
+
player.should_receive(:after_save_check).once.ordered.and_return(nil)
|
43
|
+
mission.should_receive(:after_save_check).once.ordered.and_return(nil)
|
44
|
+
|
45
|
+
db.units.save(player).should be_true
|
46
|
+
end
|
47
|
+
end
|
@@ -1,101 +1,111 @@
|
|
1
|
-
require 'spec_helper'
|
1
|
+
require 'model/spec_helper'
|
2
2
|
require 'object/crud_shared'
|
3
3
|
|
4
4
|
describe "Model CRUD" do
|
5
|
-
|
5
|
+
with_mongo_model
|
6
6
|
|
7
7
|
describe 'simple' do
|
8
|
-
before do
|
9
|
-
class
|
8
|
+
before :all do
|
9
|
+
class Unit
|
10
10
|
inherit Mongo::Model
|
11
|
-
collection
|
11
|
+
collection :units
|
12
12
|
|
13
|
-
def initialize name = nil, info = nil; @name, @info = name, info end
|
14
13
|
attr_accessor :name, :info
|
15
14
|
def == o; [self.class, name, info] == [o.class, o.respond_to(:name), o.respond_to(:info)] end
|
16
15
|
end
|
16
|
+
end
|
17
|
+
after(:all){remove_constants :Unit}
|
17
18
|
|
18
|
-
|
19
|
+
before do
|
20
|
+
@zeratul = Unit.new.set name: 'Zeratul', info: 'Dark Templar'
|
19
21
|
end
|
20
|
-
after{remove_constants :Person}
|
21
22
|
|
22
23
|
it_should_behave_like "object CRUD"
|
23
24
|
|
24
25
|
it 'model crud' do
|
25
26
|
# read
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
Unit.count.should == 0
|
28
|
+
Unit.all.should == []
|
29
|
+
Unit.first.should == nil
|
29
30
|
|
30
31
|
# create
|
31
32
|
@zeratul.save.should be_true
|
32
33
|
@zeratul._id.should_not be_nil
|
33
34
|
|
34
35
|
# read
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
36
|
+
Unit.count.should == 1
|
37
|
+
Unit.all.should == [@zeratul]
|
38
|
+
Unit.first.should == @zeratul
|
39
|
+
Unit.first.object_id.should_not == @zeratul.object_id
|
39
40
|
|
40
41
|
# update
|
41
42
|
@zeratul.info = 'Killer of Cerebrates'
|
42
43
|
@zeratul.save.should be_true
|
43
|
-
|
44
|
-
|
44
|
+
Unit.count.should == 1
|
45
|
+
Unit.first(name: 'Zeratul').info.should == 'Killer of Cerebrates'
|
45
46
|
|
46
47
|
# destroy
|
47
48
|
@zeratul.destroy.should be_true
|
48
|
-
|
49
|
+
Unit.count.should == 0
|
49
50
|
end
|
50
51
|
|
51
52
|
it 'should be able to save to another collection' do
|
52
53
|
# create
|
53
|
-
@zeratul.save(collection: db.
|
54
|
+
@zeratul.save(collection: db.heroes).should be_true
|
54
55
|
@zeratul._id.should_not be_nil
|
55
56
|
|
56
57
|
# read
|
57
|
-
|
58
|
-
db.
|
59
|
-
db.
|
60
|
-
db.
|
58
|
+
Unit.count.should == 0
|
59
|
+
db.heroes.count.should == 1
|
60
|
+
db.heroes.first.should == @zeratul
|
61
|
+
db.heroes.first.object_id.should_not == @zeratul.object_id
|
61
62
|
|
62
63
|
# update
|
63
64
|
@zeratul.info = 'Killer of Cerebrates'
|
64
|
-
@zeratul.save(collection: db.
|
65
|
-
|
66
|
-
db.
|
67
|
-
db.
|
65
|
+
@zeratul.save(collection: db.heroes).should be_true
|
66
|
+
Unit.count.should == 0
|
67
|
+
db.heroes.count.should == 1
|
68
|
+
db.heroes.first(name: 'Zeratul').info.should == 'Killer of Cerebrates'
|
68
69
|
|
69
70
|
# destroy
|
70
|
-
@zeratul.destroy(collection: db.
|
71
|
-
db.
|
71
|
+
@zeratul.destroy(collection: db.heroes).should be_true
|
72
|
+
db.heroes.count.should == 0
|
72
73
|
end
|
74
|
+
|
75
|
+
it 'create'
|
76
|
+
|
77
|
+
it 'destroy_all'
|
73
78
|
end
|
74
79
|
|
75
80
|
describe 'embedded' do
|
76
|
-
before do
|
81
|
+
before :all do
|
77
82
|
class Player
|
78
83
|
inherit Mongo::Model
|
84
|
+
collection :players
|
85
|
+
|
79
86
|
attr_accessor :missions
|
80
87
|
def == o; [self.class, self.missions] == [o.class, o.respond_to(:missions)] end
|
81
88
|
|
82
89
|
class Mission
|
83
90
|
inherit Mongo::Model
|
84
|
-
|
91
|
+
|
85
92
|
attr_accessor :name, :stats
|
86
93
|
def == o; [self.class, self.name, self.stats] == [o.class, o.respond_to(:name), o.respond_to(:stats)] end
|
87
94
|
end
|
88
95
|
end
|
96
|
+
end
|
97
|
+
after(:all){remove_constants :Player}
|
89
98
|
|
99
|
+
before do
|
100
|
+
@mission_class = Player::Mission
|
90
101
|
@player = Player.new
|
91
102
|
@player.missions = [
|
92
|
-
Player::Mission.new('Wasteland', {buildings: 5, units: 10}),
|
93
|
-
Player::Mission.new('Backwater Station', {buildings: 8, units: 25}),
|
103
|
+
Player::Mission.new.set(name: 'Wasteland', stats: {buildings: 5, units: 10}),
|
104
|
+
Player::Mission.new.set(name: 'Backwater Station', stats: {buildings: 8, units: 25}),
|
94
105
|
]
|
95
106
|
end
|
96
|
-
after{remove_constants :Player}
|
97
107
|
|
98
|
-
it_should_behave_like '
|
108
|
+
it_should_behave_like 'embedded object CRUD'
|
99
109
|
|
100
110
|
it 'crud' do
|
101
111
|
# create
|
@@ -109,7 +119,7 @@ describe "Model CRUD" do
|
|
109
119
|
|
110
120
|
# update
|
111
121
|
@player.missions.first.stats[:units] = 9
|
112
|
-
@player.missions << Player::Mission.new('Desperate Alliance', {buildings: 11, units: 40})
|
122
|
+
@player.missions << Player::Mission.new.set(name: 'Desperate Alliance', stats: {buildings: 11, units: 40})
|
113
123
|
@player.save.should be_true
|
114
124
|
Player.count.should == 1
|
115
125
|
Player.first.should == @player
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'model/spec_helper'
|
2
|
+
|
3
|
+
describe 'Collection & Database' do
|
4
|
+
with_mongo
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
class TheModel
|
8
|
+
inherit Mongo::Model
|
9
|
+
end
|
10
|
+
end
|
11
|
+
after(:all){remove_constants :TheModel}
|
12
|
+
|
13
|
+
after do
|
14
|
+
TheModel.db = nil
|
15
|
+
TheModel.collection = nil
|
16
|
+
Mongo::Model.connection, Mongo::Model.db = nil, nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "global setting" do
|
20
|
+
Mongo::Model.connection = db.connection
|
21
|
+
Mongo::Model.db = db
|
22
|
+
|
23
|
+
Mongo::Model.connection.should == db.connection
|
24
|
+
Mongo::Model.db.should == db
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should allow set database per model" do
|
28
|
+
Mongo::Model.connection = db.connection
|
29
|
+
Mongo::Model.db = db
|
30
|
+
|
31
|
+
TheModel.db.should == db
|
32
|
+
|
33
|
+
TheModel.db :test
|
34
|
+
TheModel.db.name.should == 'test'
|
35
|
+
|
36
|
+
TheModel.db = nil
|
37
|
+
TheModel.db db
|
38
|
+
TheModel.db.should == db
|
39
|
+
|
40
|
+
TheModel.db = nil
|
41
|
+
TheModel.db{db}
|
42
|
+
TheModel.db.should == db
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should allow set collection per model" do
|
46
|
+
Mongo::Model.db = db
|
47
|
+
|
48
|
+
# TheModel.default_collection_name.should == :the_model
|
49
|
+
# TheModel.collection.name.should == 'the_model'
|
50
|
+
|
51
|
+
TheModel.collection :units
|
52
|
+
TheModel.collection.name.should == 'units'
|
53
|
+
|
54
|
+
TheModel.collection = nil
|
55
|
+
units = db.units
|
56
|
+
TheModel.collection units
|
57
|
+
TheModel.collection.should == units
|
58
|
+
|
59
|
+
TheModel.collection = nil
|
60
|
+
TheModel.collection{units}
|
61
|
+
TheModel.collection.should == units
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'model/spec_helper'
|
2
|
+
|
3
|
+
describe 'Model Miscellaneous' do
|
4
|
+
with_mongo_model
|
5
|
+
|
6
|
+
after{remove_constants :Unit}
|
7
|
+
|
8
|
+
it "timestamps" do
|
9
|
+
class Unit3
|
10
|
+
inherit Mongo::Model
|
11
|
+
collection :units
|
12
|
+
|
13
|
+
attr_accessor :name
|
14
|
+
|
15
|
+
timestamps!
|
16
|
+
end
|
17
|
+
|
18
|
+
unit = Unit3.new.set name: 'Zeratul'
|
19
|
+
unit.save!
|
20
|
+
|
21
|
+
unit = Unit3.first
|
22
|
+
unit.created_at.should_not be_nil
|
23
|
+
unit.updated_at.should_not be_nil
|
24
|
+
created_at,updated_at = unit.created_at, unit.updated_at
|
25
|
+
|
26
|
+
unit.save!
|
27
|
+
unit.created_at.should == created_at
|
28
|
+
unit.updated_at.should > updated_at
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'model/spec_helper'
|
2
|
+
require 'object/crud_shared'
|
3
|
+
|
4
|
+
describe "Model Query" do
|
5
|
+
with_mongo_model
|
6
|
+
|
7
|
+
before :all do
|
8
|
+
class Unit
|
9
|
+
inherit Mongo::Model
|
10
|
+
collection :units
|
11
|
+
|
12
|
+
attr_accessor :name
|
13
|
+
end
|
14
|
+
end
|
15
|
+
after(:all){remove_constants :Unit}
|
16
|
+
|
17
|
+
before{@zeratul = Unit.new.set name: 'Zeratul'}
|
18
|
+
|
19
|
+
it 'exist?' do
|
20
|
+
Unit.should_not exist(name: 'Zeratul')
|
21
|
+
@zeratul.save!
|
22
|
+
Unit.should exist(name: 'Zeratul')
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'first, first!' do
|
26
|
+
Unit.first.should be_nil
|
27
|
+
-> {Unit.first!}.should raise_error(Mongo::NotFound)
|
28
|
+
@zeratul.save
|
29
|
+
Unit.first.should_not be_nil
|
30
|
+
Unit.first!.should_not be_nil
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'all, each' do
|
34
|
+
list = []; Unit.each{|o| list << o}
|
35
|
+
list.size.should == 0
|
36
|
+
|
37
|
+
@zeratul.save
|
38
|
+
list = []; Unit.each{|o| list << o}
|
39
|
+
list.size.should == 1
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'dynamic finders integration' do
|
43
|
+
Unit.first_by_name('Zeratul').should be_nil
|
44
|
+
Unit.new.set(name: 'Zeratul').save!
|
45
|
+
Unit.first_by_name('Zeratul').name.should == 'Zeratul'
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
require 'model/spec_helper'
|
2
|
+
|
3
|
+
describe "Scope" do
|
4
|
+
with_mongo_model
|
5
|
+
|
6
|
+
before do
|
7
|
+
class Unit
|
8
|
+
inherit Mongo::Model
|
9
|
+
collection :units
|
10
|
+
|
11
|
+
attr_accessor :name, :status, :race
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
after{remove_constants :Unit, :Protoss}
|
16
|
+
|
17
|
+
describe 'current scope' do
|
18
|
+
it "should affect finders" do
|
19
|
+
Unit.new.set(name: 'Zeratul', status: 'alive').save!
|
20
|
+
Unit.new.set(name: 'Jim', status: 'alive').save!
|
21
|
+
Unit.new.set(name: 'Tassadar', status: 'dead').save!
|
22
|
+
|
23
|
+
Unit.count.should == 3
|
24
|
+
Unit.all.size.should == 3
|
25
|
+
Unit.first(name: 'Tassadar').should_not be_nil
|
26
|
+
Unit.first!(name: 'Tassadar').should_not be_nil
|
27
|
+
|
28
|
+
Unit.stub!(:current_scope).and_return(status: 'alive')
|
29
|
+
|
30
|
+
Unit.count.should == 2
|
31
|
+
Unit.all.size.should == 2
|
32
|
+
Unit.first(name: 'Tassadar').should be_nil
|
33
|
+
-> {Unit.first!(name: 'Tassadar')}.should raise_error(Mongo::NotFound)
|
34
|
+
|
35
|
+
# should be merged with finders
|
36
|
+
Unit.count(status: 'dead').should == 1
|
37
|
+
Unit.all(status: 'dead').size.should == 1
|
38
|
+
Unit.first(name: 'Tassadar', status: 'dead').should_not be_nil
|
39
|
+
Unit.first!(name: 'Tassadar', status: 'dead').should_not be_nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'default scope' do
|
44
|
+
it "should not affect objects without default_scope" do
|
45
|
+
Unit.current_scope.should == {}
|
46
|
+
end
|
47
|
+
|
48
|
+
it "definition" do
|
49
|
+
Unit.default_scope status: 'alive'
|
50
|
+
Unit.current_scope.should == {status: 'alive'}
|
51
|
+
|
52
|
+
Unit.default_scope do
|
53
|
+
{status: 'alive'}
|
54
|
+
end
|
55
|
+
Unit.current_scope.should == {status: 'alive'}
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should be inherited" do
|
59
|
+
Unit.default_scope status: 'alive'
|
60
|
+
|
61
|
+
class Protoss < Unit; end
|
62
|
+
Protoss.current_scope.should == {status: 'alive'}
|
63
|
+
|
64
|
+
Protoss.default_scope status: 'dead'
|
65
|
+
Unit.current_scope.should == {status: 'alive'}
|
66
|
+
Protoss.current_scope.should == {status: 'dead'}
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe 'scope' do
|
71
|
+
it "definition" do
|
72
|
+
Unit.scope :alive, status: 'alive'
|
73
|
+
Unit.alive.current_scope.should == {status: 'alive'}
|
74
|
+
|
75
|
+
Unit.scope :alive do
|
76
|
+
{status: 'alive'}
|
77
|
+
end
|
78
|
+
Unit.alive.current_scope.should == {status: 'alive'}
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'scope should affect current scope' do
|
82
|
+
Unit.scope :alive, status: 'alive'
|
83
|
+
|
84
|
+
Unit.current_scope.should == {}
|
85
|
+
|
86
|
+
Unit.alive.current_scope.should == {status: 'alive'}
|
87
|
+
Unit.alive.should == Unit
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should be merged with default scope' do
|
91
|
+
Unit.default_scope race: 'Protoss'
|
92
|
+
Unit.scope :alive, status: 'alive'
|
93
|
+
Unit.alive.current_scope.should == {race: 'Protoss', status: 'alive'}
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'should allow to chain scopes' do
|
97
|
+
Unit.scope :alive, status: 'alive'
|
98
|
+
Unit.scope :protosses, race: 'Protoss'
|
99
|
+
Unit.alive.protosses.current_scope.should == {race: 'Protoss', status: 'alive'}
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe 'with_scope' do
|
104
|
+
it "shouldn't allow to nest exclusive scope" do
|
105
|
+
-> {
|
106
|
+
Unit.with_exclusive_scope do
|
107
|
+
Unit.with_exclusive_scope{}
|
108
|
+
end
|
109
|
+
}.should raise_error(/exclusive scope already applied/)
|
110
|
+
|
111
|
+
-> {
|
112
|
+
Unit.with_exclusive_scope do
|
113
|
+
Unit.with_scope{}
|
114
|
+
end
|
115
|
+
}.should raise_error(/exclusive scope already applied/)
|
116
|
+
end
|
117
|
+
|
118
|
+
it "with_exclusive_scope should clear other scopes" do
|
119
|
+
Unit.default_scope status: 'alive'
|
120
|
+
|
121
|
+
Unit.with_scope race: 'Protoss' do
|
122
|
+
Unit.current_scope.should == {status: 'alive', race: 'Protoss'}
|
123
|
+
|
124
|
+
Unit.with_exclusive_scope do
|
125
|
+
Unit.current_scope.should == {}
|
126
|
+
end
|
127
|
+
|
128
|
+
Unit.with_exclusive_scope race: 'Terran' do
|
129
|
+
Unit.current_scope.should == {race: 'Terran'}
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
it "usage" do
|
135
|
+
Unit.with_scope status: 'alive' do
|
136
|
+
Unit.current_scope.should == {status: 'alive'}
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should merge scope" do
|
141
|
+
Unit.default_scope status: 'alive'
|
142
|
+
Unit.with_scope race: 'Protoss' do
|
143
|
+
Unit.with_scope name: 'Zeratul' do
|
144
|
+
Unit.current_scope.should == {name: 'Zeratul', race: 'Protoss', status: 'alive'}
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'model/spec_helper'
|
2
|
+
|
3
|
+
describe "Validations" do
|
4
|
+
with_mongo_model
|
5
|
+
|
6
|
+
before do
|
7
|
+
class Unit
|
8
|
+
inherit Mongo::Model
|
9
|
+
collection :units
|
10
|
+
|
11
|
+
attr_accessor :errors
|
12
|
+
|
13
|
+
attr_accessor :name
|
14
|
+
end
|
15
|
+
end
|
16
|
+
after{remove_constants :Unit}
|
17
|
+
|
18
|
+
it "should not save model with errors" do
|
19
|
+
unit = Unit.new.set name: 'Zeratul'
|
20
|
+
unit.save.should be_true
|
21
|
+
|
22
|
+
unit.errors = []
|
23
|
+
unit.save.should be_true
|
24
|
+
|
25
|
+
unit.errors = ['hairy error']
|
26
|
+
unit.save.should be_false
|
27
|
+
|
28
|
+
unit.errors = {name: 'hairy error'}
|
29
|
+
unit.save.should be_false
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should check :errors only and ignore valid? method" do
|
33
|
+
unit = Unit.new.set name: 'Zeratul'
|
34
|
+
unit.should_not_receive(:valid?)
|
35
|
+
unit.save.should be_true
|
36
|
+
end
|
37
|
+
end
|
@@ -3,7 +3,7 @@ require 'object/spec_helper'
|
|
3
3
|
describe 'Object callbacks' do
|
4
4
|
with_mongo
|
5
5
|
|
6
|
-
before do
|
6
|
+
before :all do
|
7
7
|
class Player
|
8
8
|
include RSpec::CallbackHelper
|
9
9
|
attr_accessor :missions
|
@@ -12,12 +12,14 @@ describe 'Object callbacks' do
|
|
12
12
|
include RSpec::CallbackHelper
|
13
13
|
end
|
14
14
|
end
|
15
|
+
end
|
16
|
+
after(:all){remove_constants :Player}
|
15
17
|
|
18
|
+
before do
|
16
19
|
@mission = Player::Mission.new
|
17
20
|
@player = Player.new
|
18
21
|
@player.missions = [@mission]
|
19
22
|
end
|
20
|
-
after{remove_constants :Player}
|
21
23
|
|
22
24
|
it 'create' do
|
23
25
|
%w(before_validate before_save before_create after_create after_save after_validate).each do |name|
|
@@ -61,8 +63,8 @@ describe 'Object callbacks' do
|
|
61
63
|
end
|
62
64
|
|
63
65
|
it 'should be able interrupt CRUD' do
|
64
|
-
@mission.stub! :_run_callbacks do |
|
65
|
-
false if
|
66
|
+
@mission.stub! :_run_callbacks do |type, method_name|
|
67
|
+
false if type == :before and method_name == :save
|
66
68
|
end
|
67
69
|
db.players.save(@player).should be_false
|
68
70
|
db.players.count.should == 0
|
data/spec/object/crud_shared.rb
CHANGED
@@ -40,7 +40,7 @@ shared_examples_for 'embedded object CRUD' do
|
|
40
40
|
|
41
41
|
# update
|
42
42
|
@player.missions.first.stats[:units] = 9
|
43
|
-
@player.missions <<
|
43
|
+
@player.missions << @mission_class.new('Desperate Alliance', {buildings: 11, units: 40})
|
44
44
|
db.players.save @player
|
45
45
|
db.players.count.should == 1
|
46
46
|
db.players.first.should == @player
|