mongo_db 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rake_ext'
2
+
3
+ project(
4
+ name: "mongo_db",
5
+ # official_name: "mongo_db",
6
+ version: '0.0.1',
7
+ gem: true,
8
+ summary: "MongDB ODM. In development, don't use it, come back at 11/8/25",
9
+ # Very small, schema-less, config-less Ruby ODM for MongoDB",
10
+
11
+ author: "Alexey Petrushin",
12
+ homepage: "http://github.com/alexeypetrushin/mongo_db"
13
+ )
File without changes
@@ -0,0 +1,5 @@
1
+ class Mongo::DB
2
+ def method_missing collection_name
3
+ self[collection_name]
4
+ end
5
+ end
@@ -0,0 +1,32 @@
1
+ MONGO_TEST_DATABASE = 'default_test'
2
+
3
+ rspec do
4
+ def mongo
5
+ $mongo || raise('Mongo not defined (use :with_mongo helper)!')
6
+ end
7
+
8
+ def clear_mongo name = MONGO_TEST_DATABASE
9
+ mongo.db.collection_names.each do |name|
10
+ next if name =~ /^system\./
11
+ mongo.db.collection(name).drop
12
+ end
13
+ end
14
+
15
+ class << self
16
+ def with_mongo
17
+ require 'ostruct'
18
+
19
+ before :all do
20
+ $mongo = OpenStruct.new.tap do |m|
21
+ m.connection = Mongo::Connection.new
22
+ m.db = m.connection.db MONGO_TEST_DATABASE
23
+ end
24
+ end
25
+ after(:all){$mongo = nil}
26
+
27
+ before do
28
+ clear_mongo
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ require 'mongo'
2
+
3
+ %w(
4
+ database
5
+ ).each{|f| require "mongo_model/support/#{f}"}
@@ -0,0 +1,2 @@
1
+ gem 'activemodel', '~> 3.0'
2
+ gem 'mongo', '~> 1.3'
File without changes
data/lib/mongo_db.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'mongo_db/gems'
2
+ require 'mongo_db/driver'
3
+ require 'mongo_db/model'
data/readme.md ADDED
@@ -0,0 +1,9 @@
1
+ # Ruby ODM for MongoDB
2
+
3
+ Not finished, in development.
4
+
5
+ - Very small.
6
+ - Schema-less, dynamic.
7
+ - Models can be saved to any collection.
8
+ - Full support for embedded objects (and MDD composite pattern).
9
+ - Doesn't try to mimic ActiveRecord, it's differrent and designed to get most of MongoDB.
File without changes
@@ -0,0 +1,10 @@
1
+ require 'mongo_ext/spec_helper'
2
+
3
+ describe "Mongo Extensions" do
4
+ with_mongo
5
+
6
+ it "should provide handy access to collections" do
7
+ mongo.db['collection1']
8
+ mongo.db.collection1
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ require 'mongo_ext'
2
+
3
+ require 'rspec_ext'
4
+ require 'mongo_ext/spec'
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Hash CRUD" do
4
+ with_mongo_model
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.heroes.count.should == 0
14
+ db.heroes.all.should == []
15
+ db.heroes.first.should == nil
16
+
17
+ # create
18
+ db.heroes.save(@zeratul)
19
+
20
+ # read
21
+ db.heroes.all.should == [@zeratul]
22
+ db.heroes.count.should == 1
23
+ db.heroes.first.should == @zeratul
24
+
25
+ # update
26
+ @zeratul[:info] = 'Killer of Cerebrates'
27
+ db.heroes.save({name: 'Zeratul'}, @zeratul)
28
+ db.heroes.count.should == 1
29
+ db.heroes.first(name: 'Zeratul').info.should == 'Killer of Cerebrates'
30
+
31
+ # destroy
32
+ db.heroes.destroy name: 'Zeratul'
33
+ db.heroes.count.should == 0
34
+ end
35
+ end
36
+
37
+ describe 'embedded' do
38
+ before do
39
+ @player = {
40
+ name: 'Alex',
41
+ missions: [
42
+ {name: 'Wasteland', stats: {buildings: 5, units: 10}},
43
+ {name: 'Backwater Station', stats: {buildings: 8, units: 25}}
44
+ ]
45
+ }
46
+ end
47
+
48
+ it 'crud' do
49
+ # create
50
+ db.players.save(@player)
51
+
52
+ # read
53
+ db.players.count.should == 1
54
+ db.players.first.should == @player
55
+
56
+ # update
57
+ @player[:missions].first[:stats][:units] = 9
58
+ @player.missions << {name: 'Desperate Alliance', stats: {buildings: 11, units: 40}},
59
+ db.players.save({name: 'Alex'}, @player)
60
+ db.players.count.should == 1
61
+ db.players.first.should == @player
62
+ db.players.first.object_id.should_not == @player.object_id
63
+
64
+ # destroy
65
+ db.players.destroy name: 'Alex'
66
+ db.players.count.should == 0
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,123 @@
1
+ require 'spec_helper'
2
+ require 'object/crud_shared'
3
+
4
+ describe "Model CRUD" do
5
+ with_mongo_model
6
+
7
+ describe 'simple' do
8
+ before do
9
+ class Person
10
+ inherit Mongo::Model
11
+ collection{db.heroes}
12
+
13
+ def initialize name, info; @name, @info = name, info end
14
+ attr_accessor :name, :info
15
+ def == o; [self.class, name, info] == [o.class, o.respond_to(:name), o.respond_to(:info)] end
16
+ end
17
+
18
+ @zeratul = User.new 'Zeratul', 'Dark Templar'
19
+ end
20
+ after{remove_constants :Person}
21
+
22
+ it_should_behave_like "object CRUD"
23
+
24
+ it 'model crud' do
25
+ # read
26
+ Person.count.should == 0
27
+ Person.all.should == []
28
+ Person.first.should == nil
29
+
30
+ # create
31
+ @zeratul.save.should be_true
32
+ @zeratul._id.should be_present
33
+
34
+ # read
35
+ Person.count.should == 1
36
+ Person.all.should == [@zeratul]
37
+ Person.first.should == @zeratul
38
+ Person.first.object_id.should_not == @zeratul.object_id
39
+
40
+ # update
41
+ @zeratul.info = 'Killer of Cerebrates'
42
+ @zeratul.save.should be_true
43
+ Person.count.should == 1
44
+ Person.first(name: 'Zeratul').info.should == 'Killer of Cerebrates'
45
+
46
+ # destroy
47
+ @zeratul.destroy.should be_true
48
+ Person.count.should == 0
49
+ end
50
+
51
+ it 'should be able to save to another collection' do
52
+ # create
53
+ @zeratul.save(collection: db.protosses).should be_true
54
+ @zeratul._id.should be_present
55
+
56
+ # read
57
+ Person.count.should == 0
58
+ db.protosses.count.should == 1
59
+ db.protosses.should == @zeratul
60
+ db.protosses.object_id.should_not == @zeratul.object_id
61
+
62
+ # update
63
+ @zeratul.info = 'Killer of Cerebrates'
64
+ @zeratul.save(collection: db.protosses).should be_true
65
+ Person.count.should == 0
66
+ db.protosses.count.should == 1
67
+ db.protosses.first(name: 'Zeratul').info.should == 'Killer of Cerebrates'
68
+
69
+ # destroy
70
+ @zeratul.destroy(collection: db.protosses).should be_true
71
+ db.protosses.count.should == 0
72
+ end
73
+ end
74
+
75
+ describe 'embedded' do
76
+ before do
77
+ class Player
78
+ inherit Mongo::Model
79
+ attr_accessor :missions
80
+ def == o; [self.class, self.missions] = [o.class, o.respond_to(:missions)] end
81
+
82
+ class Mission
83
+ inherit Mongo::Model
84
+ def initialize name, stats; @name, @stats = name, stats end
85
+ attr_accessor :name, :stats
86
+ def == o; [self.class, self.name, self.stats] = [o.class, o.respond_to(:name), o.respond_to(:stats)] end
87
+ end
88
+ end
89
+
90
+ @player = Player.new
91
+ @player.missions = [
92
+ Player::Mission.new('Wasteland', {buildings: 5, units: 10}),
93
+ Player::Mission.new('Backwater Station', {buildings: 8, units: 25}),
94
+ ]
95
+ end
96
+ after{remove_constants :Player}
97
+
98
+ it_should_behave_like 'shared object CRUD'
99
+
100
+ it 'crud' do
101
+ # create
102
+ @player.save.should be_true
103
+ @player._id.should be_present
104
+
105
+ # read
106
+ Player.count.should == 1
107
+ Player.first.should == @player
108
+ Player.first.object_id.should_not == @players.object_id
109
+
110
+ # update
111
+ @player.missions.first.stats[:units] = 9
112
+ @player.missions << Player::Mission.new('Desperate Alliance', {buildings: 11, units: 40}),
113
+ @player.save.should be_true
114
+ Player.count.should == 1
115
+ Player.first.should == @player
116
+ Player.first.object_id.should_not == @player.object_id
117
+
118
+ # destroy
119
+ @player.destroy.should be_true
120
+ Player.count.should == 0
121
+ end
122
+ end
123
+ end
File without changes
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Object callbacks' do
4
+ with_mongo_model
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
@@ -0,0 +1,53 @@
1
+ shared_examples_for 'object CRUD' do
2
+ it 'crud' do
3
+ # read
4
+ db.heroes.count.should == 0
5
+ db.heroes.all.should == []
6
+ db.heroes.first.should == nil
7
+
8
+ # create
9
+ db.heroes.save(@zeratul).should be_true
10
+ @zeratul.instance_variable_get(:@_id).should be_present
11
+
12
+ # read
13
+ db.heroes.count.should == 1
14
+ db.heroes.all.should == [@zeratul]
15
+ db.heroes.first.should == @zeratul
16
+ db.heroes.first.object_id.should_not == @zeratul.object_id
17
+
18
+ # update
19
+ @zeratul.info = 'Killer of Cerebrates'
20
+ db.heroes.save(@zeratul).should be_true
21
+ db.heroes.count.should == 1
22
+ db.heroes.first(name: 'Zeratul').info.should == 'Killer of Cerebrates'
23
+
24
+ # destroy
25
+ db.heroes.destroy(@zeratul).should be_true
26
+ db.heroes.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.instance_variable_get(:@_id).should be_present
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
+ @player.missions << Player::Mission.new('Desperate Alliance', {buildings: 11, units: 40}),
44
+ db.players.save @player
45
+ db.players.count.should == 1
46
+ db.players.first.should == @player
47
+ db.players.first.object_id.should_not == @player.object_id
48
+
49
+ # destroy
50
+ db.players.destroy @player
51
+ db.players.count.should == 0
52
+ end
53
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+ require 'object/crud_shared'
3
+
4
+ describe "Object CRUD" do
5
+ with_mongo_model
6
+
7
+ describe 'simple' do
8
+ before do
9
+ class Person
10
+ def initialize name, info; @name, @info = name, info end
11
+ attr_accessor :name, :info
12
+ def == o; [self.class, name, info] == [o.class, o.respond_to(:name), o.respond_to(:info)] end
13
+ end
14
+
15
+ @zeratul = User.new 'Zeratul', 'Dark Templar'
16
+ end
17
+ after{remove_constants :Person}
18
+
19
+ it_should_behave_like "object CRUD"
20
+ end
21
+
22
+ describe 'embedded' do
23
+ before do
24
+ class Player
25
+ attr_accessor :missions
26
+ def == o; [self.class, self.missions] = [o.class, o.respond_to(:missions)] end
27
+
28
+ class Mission
29
+ def initialize name, stats; @name, @stats = name, stats end
30
+ attr_accessor :name, :stats
31
+ def == o; [self.class, self.name, self.stats] = [o.class, o.respond_to(:name), o.respond_to(:stats)] end
32
+ end
33
+ end
34
+
35
+ @player = Player.new
36
+ @player.missions = [
37
+ Player::Mission.new('Wasteland', {buildings: 5, units: 10}),
38
+ Player::Mission.new('Backwater Station', {buildings: 8, units: 25}),
39
+ ]
40
+ end
41
+ after{remove_constants :Player}
42
+
43
+ it_should_behave_like 'embedded object CRUD'
44
+ end
45
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Object validation' do
4
+ with_mongo_model
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 'should not save/update/destroy invalid objects' do
21
+ # create
22
+ @player.stub!(:valid?).and_return(false)
23
+ db.players.save(@player).should be_false
24
+
25
+ @player.stub!(:valid?).and_return(true)
26
+ db.players.save(@player).should be_true
27
+
28
+ # update
29
+ @player.stub!(:valid?).and_return(false)
30
+ db.players.save(@player).should be_false
31
+
32
+ @player.stub!(:valid?).and_return(true)
33
+ db.players.save(@player).should be_true
34
+
35
+ # destroy
36
+ @player.stub!(:valid?).and_return(false)
37
+ db.players.destroy(@player).should be_false
38
+
39
+ @player.stub!(:valid?).and_return(true)
40
+ db.players.destroy(@player).should be_true
41
+ end
42
+
43
+ it 'should not save/update/destroy invalid embedded objects' do
44
+ # create
45
+ @mission.stub!(:valid?).and_return(false)
46
+ db.players.save(@player).should be_false
47
+
48
+ @mission.stub!(:valid?).and_return(true)
49
+ db.players.save(@player).should be_true
50
+
51
+ # update
52
+ @mission.stub!(:valid?).and_return(false)
53
+ db.players.save(@player).should be_false
54
+
55
+ @mission.stub!(:valid?).and_return(true)
56
+ db.players.save(@player).should be_true
57
+
58
+ # destroy
59
+ @mission.stub!(:valid?).and_return(false)
60
+ db.players.destroy(@player).should be_false
61
+
62
+ @mission.stub!(:valid?).and_return(true)
63
+ db.players.destroy(@player).should be_true
64
+ end
65
+
66
+ it "should be able skip validation" do
67
+ @player.stub!(:valid?).and_return(false)
68
+ db.players.save(@player, validate: false).should be_true
69
+
70
+ @player.stub!(:valid?).and_return(true)
71
+ @mission.stub!(:valid?).and_return(false)
72
+ db.players.save(@player, validate: false).should be_true
73
+ end
74
+ end
@@ -0,0 +1 @@
1
+ require 'mongo_model'
File without changes
data/spec/test_spec.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ db = Mongo::Connection.new("localhost").db("default_test")
4
+ p db.class
5
+ puts db.methods.sort
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo_db
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexey Petrushin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-12 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: &1006610 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *1006610
25
+ - !ruby/object:Gem::Dependency
26
+ name: mongo
27
+ requirement: &1015350 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '1.3'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *1015350
36
+ description:
37
+ email:
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files: []
41
+ files:
42
+ - Rakefile
43
+ - readme.md
44
+ - lib/mongo_db/driver/connection.rb
45
+ - lib/mongo_db/driver/database.rb
46
+ - lib/mongo_db/driver/spec.rb
47
+ - lib/mongo_db/driver.rb
48
+ - lib/mongo_db/gems.rb
49
+ - lib/mongo_db/model.rb
50
+ - lib/mongo_db.rb
51
+ - spec/mongo_ext/migration_spec.rb
52
+ - spec/mongo_ext/misc_spec.rb
53
+ - spec/mongo_ext/spec_helper.rb
54
+ - spec/mongo_model/hash/crud_spec.rb
55
+ - spec/mongo_model/model/crud_spec.rb
56
+ - spec/mongo_model/model/query_spec.rb
57
+ - spec/mongo_model/object/callbacks_spec.rb
58
+ - spec/mongo_model/object/crud_shared.rb
59
+ - spec/mongo_model/object/crud_spec.rb
60
+ - spec/mongo_model/object/validation_spec.rb
61
+ - spec/mongo_model/spec_helper.rb
62
+ - spec/query_spec.rb
63
+ - spec/test_spec.rb
64
+ homepage: http://github.com/alexeypetrushin/mongo_db
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.6
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: MongDB ODM. In development, don't use it, come back at 11/8/25
88
+ test_files: []