mongo_db 0.1.9 → 0.1.10
Sign up to get free protection for your applications and to get access to all the features.
- 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
data/spec/object/crud_spec.rb
CHANGED
@@ -5,23 +5,25 @@ describe "Object CRUD" do
|
|
5
5
|
with_mongo
|
6
6
|
|
7
7
|
describe 'simple' do
|
8
|
-
before do
|
9
|
-
class
|
8
|
+
before :all do
|
9
|
+
class Unit2
|
10
10
|
def initialize name = nil, info = nil; @name, @info = name, info end
|
11
11
|
attr_accessor :name, :info
|
12
12
|
def == o; [self.class, name, info] == [o.class, o.respond_to(:name), o.respond_to(:info)] end
|
13
13
|
end
|
14
|
+
end
|
15
|
+
after(:all){remove_constants :Unit2}
|
14
16
|
|
15
|
-
|
17
|
+
before do
|
18
|
+
@zeratul = Unit2.new 'Zeratul', 'Dark Templar'
|
16
19
|
end
|
17
|
-
after{remove_constants :Person}
|
18
20
|
|
19
21
|
it_should_behave_like "object CRUD"
|
20
22
|
end
|
21
23
|
|
22
24
|
describe 'embedded' do
|
23
|
-
before do
|
24
|
-
class
|
25
|
+
before :all do
|
26
|
+
class Player2
|
25
27
|
attr_accessor :missions
|
26
28
|
def == o; [self.class, self.missions] == [o.class, o.respond_to(:missions)] end
|
27
29
|
|
@@ -31,14 +33,17 @@ describe "Object CRUD" do
|
|
31
33
|
def == o; [self.class, self.name, self.stats] == [o.class, o.respond_to(:name), o.respond_to(:stats)] end
|
32
34
|
end
|
33
35
|
end
|
36
|
+
end
|
37
|
+
after(:all){remove_constants :Player2}
|
34
38
|
|
35
|
-
|
39
|
+
before do
|
40
|
+
@mission_class = Player2::Mission
|
41
|
+
@player = Player2.new
|
36
42
|
@player.missions = [
|
37
|
-
|
38
|
-
|
43
|
+
Player2::Mission.new('Wasteland', {buildings: 5, units: 10}),
|
44
|
+
Player2::Mission.new('Backwater Station', {buildings: 8, units: 25}),
|
39
45
|
]
|
40
46
|
end
|
41
|
-
after{remove_constants :Player}
|
42
47
|
|
43
48
|
it_should_behave_like 'embedded object CRUD'
|
44
49
|
end
|
data/spec/object/spec_helper.rb
CHANGED
@@ -7,7 +7,8 @@ Mongo::ObjectSerializer::SIMPLE_TYPES << RSpec::Mocks::Proxy
|
|
7
7
|
|
8
8
|
# To simplify callback expectations
|
9
9
|
module RSpec::CallbackHelper
|
10
|
-
def _run_callbacks
|
11
|
-
|
10
|
+
def _run_callbacks type, method_name
|
11
|
+
callback_method_name = :"#{type}_#{method_name}"
|
12
|
+
send callback_method_name if respond_to? callback_method_name
|
12
13
|
end
|
13
14
|
end
|
@@ -3,19 +3,21 @@ require 'object/spec_helper'
|
|
3
3
|
describe 'Object validation' do
|
4
4
|
with_mongo
|
5
5
|
|
6
|
-
before do
|
6
|
+
before :all do
|
7
7
|
class Player
|
8
8
|
attr_accessor :missions
|
9
9
|
|
10
10
|
class Mission
|
11
11
|
end
|
12
12
|
end
|
13
|
+
end
|
14
|
+
after(:all){remove_constants :Player}
|
13
15
|
|
16
|
+
before do
|
14
17
|
@mission = Player::Mission.new
|
15
18
|
@player = Player.new
|
16
19
|
@player.missions = [@mission]
|
17
20
|
end
|
18
|
-
after{remove_constants :Player}
|
19
21
|
|
20
22
|
it 'should not save/update/destroy invalid objects' do
|
21
23
|
# create
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.10
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-08-
|
12
|
+
date: 2011-08-23 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongo
|
16
|
-
requirement: &
|
16
|
+
requirement: &753720 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '1.3'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *753720
|
25
25
|
description:
|
26
26
|
email:
|
27
27
|
executables: []
|
@@ -30,34 +30,55 @@ extra_rdoc_files: []
|
|
30
30
|
files:
|
31
31
|
- Rakefile
|
32
32
|
- readme.md
|
33
|
-
- lib/mongo_db/driver/
|
34
|
-
- lib/mongo_db/driver/
|
35
|
-
- lib/mongo_db/driver/
|
36
|
-
- lib/mongo_db/driver/more/collection_finders.rb
|
37
|
-
- lib/mongo_db/driver/more.rb
|
33
|
+
- lib/mongo_db/driver/collection.rb
|
34
|
+
- lib/mongo_db/driver/database.rb
|
35
|
+
- lib/mongo_db/driver/dynamic_finders.rb
|
38
36
|
- lib/mongo_db/driver/spec.rb
|
39
37
|
- lib/mongo_db/driver.rb
|
40
38
|
- lib/mongo_db/gems.rb
|
39
|
+
- lib/mongo_db/migration/definition.rb
|
40
|
+
- lib/mongo_db/migration/migration.rb
|
41
|
+
- lib/mongo_db/migration/tasks.rb
|
42
|
+
- lib/mongo_db/migration.rb
|
43
|
+
- lib/mongo_db/model/assignment.rb
|
44
|
+
- lib/mongo_db/model/callbacks.rb
|
45
|
+
- lib/mongo_db/model/crud.rb
|
46
|
+
- lib/mongo_db/model/db.rb
|
47
|
+
- lib/mongo_db/model/misc.rb
|
48
|
+
- lib/mongo_db/model/model.rb
|
49
|
+
- lib/mongo_db/model/query.rb
|
50
|
+
- lib/mongo_db/model/scope.rb
|
51
|
+
- lib/mongo_db/model/spec.rb
|
52
|
+
- lib/mongo_db/model/support/types.rb
|
53
|
+
- lib/mongo_db/model/validation.rb
|
54
|
+
- lib/mongo_db/model.rb
|
41
55
|
- lib/mongo_db/object/object_helper.rb
|
42
56
|
- lib/mongo_db/object/object_serializer.rb
|
43
57
|
- lib/mongo_db/object.rb
|
44
|
-
- spec/driver/
|
45
|
-
- spec/driver/
|
46
|
-
- spec/driver/
|
47
|
-
- spec/driver/
|
58
|
+
- spec/driver/collection_spec.rb
|
59
|
+
- spec/driver/crud_spec.rb
|
60
|
+
- spec/driver/database_spec.rb
|
61
|
+
- spec/driver/dynamic_finders_spec.rb
|
48
62
|
- spec/driver/fixes_spec.rb
|
49
|
-
- spec/driver/
|
63
|
+
- spec/driver/hash_helper_spec.rb
|
50
64
|
- spec/driver/spec_helper.rb
|
65
|
+
- spec/integration/am_conversion_spec.rb
|
66
|
+
- spec/integration/am_validation_spec.rb
|
51
67
|
- spec/migration/migration_spec.rb
|
52
|
-
- spec/model/
|
53
|
-
- spec/model/
|
54
|
-
- spec/model/
|
68
|
+
- spec/model/assignment_spec.rb
|
69
|
+
- spec/model/callbacks_spec.rb
|
70
|
+
- spec/model/crud_spec.rb
|
71
|
+
- spec/model/db_spec.rb
|
72
|
+
- spec/model/misc_spec.rb
|
73
|
+
- spec/model/query_spec.rb
|
74
|
+
- spec/model/scope_spec.rb
|
75
|
+
- spec/model/spec_helper.rb
|
76
|
+
- spec/model/validation_spec.rb
|
55
77
|
- spec/object/callbacks_spec.rb
|
56
78
|
- spec/object/crud_shared.rb
|
57
79
|
- spec/object/crud_spec.rb
|
58
80
|
- spec/object/spec_helper.rb
|
59
81
|
- spec/object/validation_spec.rb
|
60
|
-
- spec/test.rb
|
61
82
|
homepage: http://github.com/alexeypetrushin/mongo_db
|
62
83
|
licenses: []
|
63
84
|
post_install_message:
|
data/lib/mongo_db/driver/core.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'mongo_db/gems'
|
2
|
-
|
3
|
-
require 'mongo'
|
4
|
-
|
5
|
-
%w(
|
6
|
-
database
|
7
|
-
collection
|
8
|
-
).each{|f| require "mongo_db/driver/core/#{f}"}
|
9
|
-
|
10
|
-
# defaults
|
11
|
-
Mongo.class_eval do
|
12
|
-
class << self
|
13
|
-
def defaults; @defaults ||= {} end
|
14
|
-
attr_writer :defaults
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
# database
|
19
|
-
Mongo::DB.send :include, Mongo::DBExt
|
20
|
-
|
21
|
-
# collection
|
22
|
-
Mongo::Collection.class_eval do
|
23
|
-
include Mongo::CollectionExt
|
24
|
-
|
25
|
-
%w(insert update remove save).each do |method|
|
26
|
-
alias_method "#{method}_without_ext", method
|
27
|
-
alias_method method, "#{method}_with_ext"
|
28
|
-
end
|
29
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
module Mongo::CollectionFinders
|
2
|
-
#
|
3
|
-
# first_by_id, special case
|
4
|
-
#
|
5
|
-
def first_by_id id
|
6
|
-
first _id: id
|
7
|
-
end
|
8
|
-
alias_method :by_id, :first_by_id
|
9
|
-
|
10
|
-
def first_by_id! id
|
11
|
-
first_by_id(id) || raise(Mongo::NotFound, "document with id #{id} not found!")
|
12
|
-
end
|
13
|
-
alias_method :by_id!, :first_by_id!
|
14
|
-
|
15
|
-
protected
|
16
|
-
#
|
17
|
-
# first_by_field, all_by_field
|
18
|
-
#
|
19
|
-
def method_missing clause, *a, &b
|
20
|
-
if clause =~ /^([a-z]_by_[a-z_])|(by_[a-z_])/
|
21
|
-
clause = clause.to_s
|
22
|
-
|
23
|
-
bang = clause =~ /!$/
|
24
|
-
clause = clause[0..-2] if bang
|
25
|
-
|
26
|
-
finder, field = if clause =~ /^by_/
|
27
|
-
['first', clause.sub(/by_/, '')]
|
28
|
-
else
|
29
|
-
clause.split(/_by_/, 2)
|
30
|
-
end
|
31
|
-
finder = 'first' if finder == 'find'
|
32
|
-
|
33
|
-
raise "You can't use bang version with :#{finder}!" if bang and finder != 'first'
|
34
|
-
|
35
|
-
raise "invalid arguments for finder (#{a})!" unless a.size == 1
|
36
|
-
field_value = a.first
|
37
|
-
|
38
|
-
send(finder, field => field_value) || (bang && raise(Mongo::NotFound, "document with #{field}: #{field_value} not found!"))
|
39
|
-
else
|
40
|
-
super
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
data/lib/mongo_db/driver/more.rb
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
require 'driver/spec_helper'
|
2
|
-
|
3
|
-
describe "Querying" do
|
4
|
-
with_mongo
|
5
|
-
|
6
|
-
before do
|
7
|
-
@jim = {name: 'Jim', age: 34}
|
8
|
-
@units = db.units
|
9
|
-
end
|
10
|
-
|
11
|
-
describe "dynamic finders" do
|
12
|
-
it "find, first, by" do
|
13
|
-
@units.first_by_name('Jim').should be_nil
|
14
|
-
-> {@units.first_by_name!('Jim')}.should raise_error(Mongo::NotFound)
|
15
|
-
@units.by_name('Jim').should be_nil
|
16
|
-
-> {@units.by_name!('Jim')}.should raise_error(Mongo::NotFound)
|
17
|
-
@units.first_by_name('Jim').should be_nil
|
18
|
-
-> {@units.first_by_name!('Jim')}.should raise_error(Mongo::NotFound)
|
19
|
-
|
20
|
-
@units.save @jim
|
21
|
-
|
22
|
-
@units.first_by_name('Jim').should == @jim
|
23
|
-
@units.first_by_name!('Jim').should == @jim
|
24
|
-
@units.by_name('Jim').should == @jim
|
25
|
-
@units.by_name!('Jim').should == @jim
|
26
|
-
@units.first_by_name('Jim').should == @jim
|
27
|
-
@units.first_by_name!('Jim').should == @jim
|
28
|
-
end
|
29
|
-
|
30
|
-
it "all" do
|
31
|
-
@units.all_by_name('Jim').should == []
|
32
|
-
@units.save @jim
|
33
|
-
@units.all_by_name('Jim').should == [@jim]
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should allow to use bang version only with :first" do
|
37
|
-
-> {@units.all_by_name!('Jim')}.should raise_error(/can't use bang/)
|
38
|
-
end
|
39
|
-
|
40
|
-
it "by_id (special case)" do
|
41
|
-
@units.method(:by_id).should == @units.method(:first_by_id)
|
42
|
-
@units.method(:by_id!).should == @units.method(:first_by_id!)
|
43
|
-
|
44
|
-
@units.by_id('4de81858cf26bde569000009').should be_nil
|
45
|
-
-> {@units.by_id!('4de81858cf26bde569000009')}.should raise_error(Mongo::NotFound)
|
46
|
-
|
47
|
-
@units.save @jim
|
48
|
-
|
49
|
-
@units.by_id(@jim[:_id]).should == @jim
|
50
|
-
@units.by_id!(@jim[:_id]).should == @jim
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
it "underscore to dollar" do
|
55
|
-
@units.save @jim
|
56
|
-
@units.save name: 'Zeratul', age: 600
|
57
|
-
@units.all(age: {_lt: 100}).should == [@jim]
|
58
|
-
end
|
59
|
-
end
|
data/spec/model/callbacks.rb
DELETED
@@ -1,100 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe 'Object callbacks' do
|
4
|
-
with_mongo
|
5
|
-
|
6
|
-
before do
|
7
|
-
class Player
|
8
|
-
attr_accessor :missions
|
9
|
-
|
10
|
-
class Mission
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
@mission = Player::Mission.new
|
15
|
-
@player = Player.new
|
16
|
-
@player.missions = [@mission]
|
17
|
-
end
|
18
|
-
after{remove_constants :Player}
|
19
|
-
|
20
|
-
it 'create' do
|
21
|
-
[
|
22
|
-
:before_validation,
|
23
|
-
:after_validation,
|
24
|
-
:before_save,
|
25
|
-
:before_create,
|
26
|
-
:after_create,
|
27
|
-
:after_save
|
28
|
-
].each do |name|
|
29
|
-
@player.should_receive(name).ordered
|
30
|
-
@misson.should_receive(name).ordered
|
31
|
-
end
|
32
|
-
|
33
|
-
db.players.save @player
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'update' do
|
37
|
-
db.players.save @player
|
38
|
-
|
39
|
-
[
|
40
|
-
:before_validation,
|
41
|
-
:validate,
|
42
|
-
:after_validation,
|
43
|
-
:before_save,
|
44
|
-
:before_update,
|
45
|
-
:after_update,
|
46
|
-
:after_save
|
47
|
-
].each do |name|
|
48
|
-
@player.should_receive(name).ordered
|
49
|
-
@misson.should_receive(name).ordered
|
50
|
-
end
|
51
|
-
db.players.save @player
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'destroy' do
|
55
|
-
db.players.save @player
|
56
|
-
|
57
|
-
[
|
58
|
-
:before_validation,
|
59
|
-
:validate,
|
60
|
-
:after_validation,
|
61
|
-
:before_destroy,
|
62
|
-
:after_destroy,
|
63
|
-
].each do |name|
|
64
|
-
@player.should_receive(name).ordered
|
65
|
-
@misson.should_receive(name).ordered
|
66
|
-
end
|
67
|
-
db.players.destroy @player
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'should be able interrupt CRUD' do
|
71
|
-
@misson.stub!(:before_save).and_return(false)
|
72
|
-
db.players.save(@player).should be_false
|
73
|
-
db.players.count.should == 0
|
74
|
-
end
|
75
|
-
|
76
|
-
it 'should be able skip callbacks' do
|
77
|
-
[
|
78
|
-
:before_validation,
|
79
|
-
:after_validation,
|
80
|
-
:before_save,
|
81
|
-
:before_create,
|
82
|
-
:after_create,
|
83
|
-
:after_save,
|
84
|
-
:before_update,
|
85
|
-
:after_update,
|
86
|
-
:before_destroy,
|
87
|
-
:after_destroy
|
88
|
-
].each do |name|
|
89
|
-
@player.should_not_receive(name)
|
90
|
-
@misson.should_receive(name)
|
91
|
-
end
|
92
|
-
|
93
|
-
db.players.save @player, callbacks: false
|
94
|
-
db.players.count.should == 1
|
95
|
-
db.players.save @player, callbacks: false
|
96
|
-
db.players.count.should == 1
|
97
|
-
db.players.destroy @player, callbacks: false
|
98
|
-
db.players.count.should == 0
|
99
|
-
end
|
100
|
-
end
|