mongo_db 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -3,10 +3,9 @@ require 'rake_ext'
3
3
  project(
4
4
  name: "mongo_db",
5
5
  # official_name: "mongo_db",
6
- version: '0.0.1',
6
+ version: '0.1.0',
7
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",
8
+ summary: "Model & Ruby driver enchancements for MongoDB",
10
9
 
11
10
  author: "Alexey Petrushin",
12
11
  homepage: "http://github.com/alexeypetrushin/mongo_db"
@@ -0,0 +1,68 @@
1
+ Mongo::Collection.class_eval do
2
+ alias_method :save_with_ext, :save
3
+ def save doc, opts = {}
4
+ save_with_ext doc, reverse_merge_defaults(opts, :safe)
5
+ end
6
+
7
+ alias_method :insert_whth_ext, :insert
8
+ def insert doc_or_docs, opts = {}
9
+ insert_whth_ext doc_or_docs, reverse_merge_defaults(opts, :safe)
10
+ end
11
+
12
+ alias_method :update_whth_ext, :update
13
+ def update selector, document, opts = {}
14
+ # because :multi works only with $ operators, we need to check it
15
+ opts = if document.keys.any?{|k| k =~ /^\$/}
16
+ reverse_merge_defaults(opts, :safe, :multi)
17
+ else
18
+ reverse_merge_defaults(opts, :safe)
19
+ end
20
+
21
+ update_whth_ext selector, document, opts
22
+ end
23
+
24
+ alias_method :remove_with_ext, :remove
25
+ def remove selector = {}, opts = {}
26
+ remove_with_ext selector, reverse_merge_defaults(opts, :safe, :multi)
27
+ end
28
+
29
+ def first *args
30
+ o = find_one *args
31
+ ::Mongo::Ext::HashHelper.unmarshal o
32
+ end
33
+
34
+ def all *args, &block
35
+ if block
36
+ each *args, &block
37
+ else
38
+ list = []
39
+ each(*args){|o| list << o}
40
+ list
41
+ end
42
+ end
43
+
44
+ def each selector = {}, opts = {}, &block
45
+ cursor = nil
46
+ begin
47
+ cursor = find selector, reverse_merge_defaults(opts, :batch_size)
48
+ cursor.each do |o|
49
+ o = ::Mongo::Ext::HashHelper.unmarshal o
50
+ block.call o
51
+ end
52
+ nil
53
+ ensure
54
+ cursor.close if cursor
55
+ end
56
+ end
57
+
58
+ alias_method :destroy, :remove
59
+
60
+ protected
61
+ def reverse_merge_defaults opts, *keys
62
+ h = opts.clone
63
+ keys.each do |k|
64
+ h[k] = Mongo.defaults[k] if Mongo.defaults.include?(k) and !h.include?(k)
65
+ end
66
+ h
67
+ end
68
+ end
@@ -1,5 +1,6 @@
1
- class Mongo::DB
2
- def method_missing collection_name
3
- self[collection_name]
4
- end
1
+ Mongo::DB.class_eval do
2
+ protected
3
+ def method_missing collection_name
4
+ self.collection collection_name
5
+ end
5
6
  end
@@ -0,0 +1,34 @@
1
+ require 'set'
2
+ require 'date'
3
+
4
+ class Mongo::Ext::HashHelper
5
+ # SIMPLE_TYPES = [
6
+ # String, Symbol,
7
+ # Numeric,
8
+ # Regexp,
9
+ # Array,
10
+ # TrueClass, FalseClass,
11
+ # Date, DateTime,
12
+ # BSON::ObjectId
13
+ # ].to_set
14
+
15
+ class << self
16
+ # symbolizing hashes
17
+ def unmarshal o
18
+ return o unless Mongo.defaults[:symbolize]
19
+
20
+ if o.is_a? Hash
21
+ h = {}
22
+ o.each do |k, v|
23
+ k = k.to_sym if k.is_a?(String)
24
+ h[k] = unmarshal v
25
+ end
26
+ h
27
+ elsif o.is_a? Array
28
+ o.collect{|v| unmarshal v}
29
+ else
30
+ o
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,11 +1,11 @@
1
- MONGO_TEST_DATABASE = 'default_test'
1
+ MONGO_TEST_DATABASE_NAME = 'default_test'
2
2
 
3
3
  rspec do
4
4
  def mongo
5
5
  $mongo || raise('Mongo not defined (use :with_mongo helper)!')
6
6
  end
7
7
 
8
- def clear_mongo name = MONGO_TEST_DATABASE
8
+ def clear_mongo name = MONGO_TEST_DATABASE_NAME
9
9
  mongo.db.collection_names.each do |name|
10
10
  next if name =~ /^system\./
11
11
  mongo.db.collection(name).drop
@@ -14,12 +14,12 @@ rspec do
14
14
 
15
15
  class << self
16
16
  def with_mongo
17
- require 'ostruct'
18
-
19
17
  before :all do
18
+ require 'ostruct'
19
+
20
20
  $mongo = OpenStruct.new.tap do |m|
21
21
  m.connection = Mongo::Connection.new
22
- m.db = m.connection.db MONGO_TEST_DATABASE
22
+ m.db = m.connection.db MONGO_TEST_DATABASE_NAME
23
23
  end
24
24
  end
25
25
  after(:all){$mongo = nil}
@@ -0,0 +1,28 @@
1
+ # Hash.class_eval do
2
+ # unless defined_method? :subset
3
+ # def subset *keys, &block
4
+ # keys = keys.first if keys.first.is_a? Array
5
+ # h = {}
6
+ # if keys
7
+ # self.each do |k, v|
8
+ # h[k] = v if keys.include? k
9
+ # end
10
+ # else
11
+ # self.each do |k, v|
12
+ # h[k] = v if block.call k
13
+ # end
14
+ # end
15
+ # h
16
+ # end
17
+ # end
18
+ #
19
+ # unless defined_method? :reverse_merge
20
+ # def reverse_merge(other_hash)
21
+ # other_hash.merge(self)
22
+ # end
23
+ #
24
+ # def reverse_merge!(other_hash)
25
+ # merge!(other_hash){|k,o,n| o }
26
+ # end
27
+ # end
28
+ # end
@@ -1,5 +1,23 @@
1
+ require 'mongo_db/gems'
2
+
1
3
  require 'mongo'
2
4
 
5
+ # namespace for extensions
6
+ class Mongo::Ext; end
7
+
8
+ # mongo extensions
9
+ Mongo.class_eval do
10
+ def self.defaults; @defaults ||= {} end
11
+ defaults.merge! \
12
+ symbolize: true,
13
+ batch_size: 50,
14
+ multi: true,
15
+ safe: true
16
+ end
17
+
3
18
  %w(
19
+ support
4
20
  database
5
- ).each{|f| require "mongo_model/support/#{f}"}
21
+ collection
22
+ hash_helper
23
+ ).each{|f| require "mongo_db/driver/#{f}"}
@@ -0,0 +1,4 @@
1
+ require 'mongo_db/driver'
2
+
3
+ %w(
4
+ ).each{|f| require "mongo_db/model/#{f}"}
data/readme.md CHANGED
@@ -1,9 +1,58 @@
1
- # Ruby ODM for MongoDB
1
+ Model & Ruby driver enchancements for MongoDB
2
2
 
3
- Not finished, in development.
3
+ # MongoDB driver enchancements
4
+
5
+ - Makes alien API of mongo-ruby-driver friendly & handy.
6
+ - No extra abstraction or complexities introduced, all things are exactly the same as in MongoDB.
7
+ - Simple migrations support.
8
+
9
+ Sample:
10
+
11
+ ``` ruby
12
+ require 'mongo_db/driver'
13
+
14
+ # collection shortcuts
15
+ db.some_collection
16
+
17
+ # save & update
18
+ zeratul = {name: 'Zeratul'}
19
+ db.heroes.save zeratul
20
+
21
+ # first & all
22
+ db.heroes.first name: 'Zeratul' # => {name: 'Zeratul'}
23
+
24
+ db.heroes.all name: 'Zeratul' # => [{name: 'Zeratul'}]
25
+ db.heroes.all name: 'Zeratul' do |hero|
26
+ hero # => {name: 'Zeratul'}
27
+ end
28
+
29
+ # each
30
+ db.heroes.each name: 'Zeratul' do |hero|
31
+ hero # => {name: 'Zeratul'}
32
+ end
33
+ ```
34
+
35
+ # Model (work in progress)
36
+
37
+ Model designed after the excellent "Domain-Driven Design" book by Eric Evans.
4
38
 
5
39
  - Very small.
6
- - Schema-less, dynamic.
40
+ - Minimum extra abstraction, trying to keep things as close to the MongoDB semantic as possible.
41
+ - Schema-less, dynamic (with ability to specify types for mass-assignment).
7
42
  - Models can be saved to any collection.
8
43
  - 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.
44
+ - Doesn't try to mimic ActiveRecord, it's differrent and designed to get most of MongoDB.
45
+
46
+ # Installation & Usage
47
+
48
+ ``` bash
49
+ gem install mongo_db
50
+ ```
51
+
52
+ ``` ruby
53
+ require 'mongo_db/driver'
54
+ ```
55
+
56
+ # License
57
+
58
+ Copyright (c) Alexey Petrushin, http://petrush.in, released under the MIT license.
@@ -0,0 +1,70 @@
1
+ require 'driver/spec_helper'
2
+
3
+ describe "Collection" do
4
+ with_mongo
5
+
6
+ it 'by default save must update all matched by criteria (not first as defautl in mongo)' do
7
+ db.units.save name: 'Probe', race: 'Protoss', status: 'alive'
8
+ db.units.save name: 'Zealot', race: 'Protoss', status: 'alive'
9
+
10
+ # update
11
+ db.units.update({race: 'Protoss'}, :$set => {status: 'dead'})
12
+ db.units.all.collect{|u| u[:status]}.should == %w(dead dead)
13
+
14
+ # destroy
15
+ db.units.destroy race: 'Protoss'
16
+ db.units.count.should == 0
17
+ end
18
+
19
+ describe "symbolize" do
20
+ it 'should always return symbolized hashes' do
21
+ zeratul = {name: 'Zeratul'}
22
+ db.heroes.save(zeratul).should be_mongo_id
23
+ r = db.heroes.first(name: 'Zeratul')
24
+ r[:_id].should be_mongo_id
25
+ r['_id'].should be_nil
26
+ r[:name].should == 'Zeratul'
27
+ r['name'].should be_nil
28
+ end
29
+
30
+ it "should be able to disable symbolization" do
31
+ old = Mongo.defaults[:symbolize]
32
+ begin
33
+ Mongo.defaults[:symbolize] = false
34
+
35
+ zeratul = {name: 'Zeratul'}
36
+ db.heroes.save(zeratul).should be_mongo_id
37
+ r = db.heroes.first(name: 'Zeratul')
38
+ r[:_id].should be_nil
39
+ r['_id'].should be_mongo_id
40
+ r[:name].should be_nil
41
+ r['name'].should == 'Zeratul'
42
+ ensure
43
+ Mongo.defaults[:symbolize] = old
44
+ end
45
+ end
46
+ end
47
+
48
+ it "first" do
49
+ db.heroes.first.should be_nil
50
+ zeratul = {name: 'Zeratul'}
51
+ db.heroes.save(zeratul).should be_mongo_id
52
+ db.heroes.first(name: 'Zeratul')[:name].should == 'Zeratul'
53
+ end
54
+
55
+ it 'all' do
56
+ db.heroes.all.should == []
57
+
58
+ zeratul = {name: 'Zeratul'}
59
+ db.heroes.save(zeratul).should be_mongo_id
60
+
61
+ list = db.heroes.all(name: 'Zeratul')
62
+ list.size.should == 1
63
+ list.first[:name].should == 'Zeratul'
64
+
65
+ # with block
66
+ list = []; db.heroes.all{|o| list << o}
67
+ list.size.should == 1
68
+ list.first[:name].should == 'Zeratul'
69
+ end
70
+ end
@@ -1,7 +1,7 @@
1
- require 'spec_helper'
1
+ require 'driver/spec_helper'
2
2
 
3
3
  describe "Hash CRUD" do
4
- with_mongo_model
4
+ with_mongo
5
5
 
6
6
  describe 'simple' do
7
7
  before do
@@ -15,7 +15,8 @@ describe "Hash CRUD" do
15
15
  db.heroes.first.should == nil
16
16
 
17
17
  # create
18
- db.heroes.save(@zeratul)
18
+ db.heroes.save(@zeratul).should be_mongo_id
19
+ @zeratul[:_id].should be_mongo_id
19
20
 
20
21
  # read
21
22
  db.heroes.all.should == [@zeratul]
@@ -24,12 +25,12 @@ describe "Hash CRUD" do
24
25
 
25
26
  # update
26
27
  @zeratul[:info] = 'Killer of Cerebrates'
27
- db.heroes.save({name: 'Zeratul'}, @zeratul)
28
+ db.heroes.save @zeratul
28
29
  db.heroes.count.should == 1
29
- db.heroes.first(name: 'Zeratul').info.should == 'Killer of Cerebrates'
30
-
30
+ db.heroes.first(name: 'Zeratul')[:info].should == 'Killer of Cerebrates'
31
+
31
32
  # destroy
32
- db.heroes.destroy name: 'Zeratul'
33
+ db.heroes.destroy @zeratul
33
34
  db.heroes.count.should == 0
34
35
  end
35
36
  end
@@ -47,7 +48,7 @@ describe "Hash CRUD" do
47
48
 
48
49
  it 'crud' do
49
50
  # create
50
- db.players.save(@player)
51
+ db.players.save(@player).should be_mongo_id
51
52
 
52
53
  # read
53
54
  db.players.count.should == 1
@@ -55,14 +56,14 @@ describe "Hash CRUD" do
55
56
 
56
57
  # update
57
58
  @player[:missions].first[:stats][:units] = 9
58
- @player.missions << {name: 'Desperate Alliance', stats: {buildings: 11, units: 40}},
59
- db.players.save({name: 'Alex'}, @player)
59
+ @player[:missions].push name: 'Desperate Alliance', stats: {buildings: 11, units: 40}
60
+ db.players.save(@player).should_not be_nil
60
61
  db.players.count.should == 1
61
62
  db.players.first.should == @player
62
63
  db.players.first.object_id.should_not == @player.object_id
63
64
 
64
65
  # destroy
65
- db.players.destroy name: 'Alex'
66
+ db.players.destroy @player
66
67
  db.players.count.should == 0
67
68
  end
68
69
  end
@@ -0,0 +1,9 @@
1
+ require 'driver/spec_helper'
2
+
3
+ describe "Database" do
4
+ with_mongo
5
+
6
+ it "should provide handy access to collections" do
7
+ db.some_collection.name.should == 'some_collection'
8
+ end
9
+ end
@@ -0,0 +1,29 @@
1
+ require 'driver/spec_helper'
2
+
3
+ describe "Example" do
4
+ with_mongo
5
+
6
+ it "database & collection" do
7
+ require 'mongo_db/driver'
8
+
9
+ # collection shortcuts
10
+ db.some_collection
11
+
12
+ # save & update
13
+ zeratul = {name: 'Zeratul'}
14
+ db.heroes.save zeratul
15
+
16
+ # first & all
17
+ db.heroes.first name: 'Zeratul' # => {name: 'Zeratul'}
18
+
19
+ db.heroes.all name: 'Zeratul' # => [{name: 'Zeratul'}]
20
+ db.heroes.all name: 'Zeratul' do |hero|
21
+ hero # => {name: 'Zeratul'}
22
+ end
23
+
24
+ # each
25
+ db.heroes.each name: 'Zeratul' do |hero|
26
+ hero # => {name: 'Zeratul'}
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,27 @@
1
+ # require 'driver/spec_helper'
2
+ #
3
+ # describe "Querying" do
4
+ # with_mongo
5
+ #
6
+ # it "database & collection" do
7
+ # # collection shortcuts
8
+ # db.some_collection
9
+ #
10
+ # # save & update
11
+ # zeratul = {name: 'Zeratul'}
12
+ # db.heroes.save zeratul
13
+ #
14
+ # # first & all
15
+ # db.heroes.first name: 'Zeratul' # => {name: 'Zeratul'}
16
+ #
17
+ # db.heroes.all name: 'Zeratul' # => [{name: 'Zeratul'}]
18
+ # db.heroes.all name: 'Zeratul' do |hero|
19
+ # hero # => {name: 'Zeratul'}
20
+ # end
21
+ #
22
+ # # each
23
+ # db.heroes.each name: 'Zeratul' do |hero|
24
+ # hero # => {name: 'Zeratul'}
25
+ # end
26
+ # end
27
+ # end
@@ -0,0 +1,21 @@
1
+ require 'mongo_db/driver'
2
+ require 'ruby_ext'
3
+
4
+ require 'rspec_ext'
5
+ require 'mongo_db/driver/spec'
6
+
7
+ #
8
+ # Handy spec helpers
9
+ #
10
+ rspec do
11
+ def db
12
+ mongo.db
13
+ end
14
+ end
15
+
16
+ Object.class_eval do
17
+ def mongo_id?; false end
18
+ end
19
+ BSON::ObjectId.class_eval do
20
+ def mongo_id?; true end
21
+ end
File without changes
File without changes
File without changes
File without changes
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.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-08-12 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
16
- requirement: &1006610 !ruby/object:Gem::Requirement
16
+ requirement: &319910 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *1006610
24
+ version_requirements: *319910
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mongo
27
- requirement: &1015350 !ruby/object:Gem::Requirement
27
+ requirement: &601440 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '1.3'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *1015350
35
+ version_requirements: *601440
36
36
  description:
37
37
  email:
38
38
  executables: []
@@ -41,26 +41,29 @@ extra_rdoc_files: []
41
41
  files:
42
42
  - Rakefile
43
43
  - readme.md
44
- - lib/mongo_db/driver/connection.rb
44
+ - lib/mongo_db/driver/collection.rb
45
45
  - lib/mongo_db/driver/database.rb
46
+ - lib/mongo_db/driver/hash_helper.rb
46
47
  - lib/mongo_db/driver/spec.rb
48
+ - lib/mongo_db/driver/support.rb
47
49
  - lib/mongo_db/driver.rb
48
50
  - lib/mongo_db/gems.rb
49
51
  - 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
52
+ - spec/driver/collection_spec.rb
53
+ - spec/driver/crud_spec.rb
54
+ - spec/driver/database_spec.rb
55
+ - spec/driver/example_spec.rb
56
+ - spec/driver/query_spec.rb
57
+ - spec/driver/spec_helper.rb
58
+ - spec/migration/migration_spec.rb
59
+ - spec/model/model/crud.rb
60
+ - spec/model/model/query.rb
61
+ - spec/model/object/callbacks.rb
62
+ - spec/model/object/crud.rb
63
+ - spec/model/object/crud_shared.rb
64
+ - spec/model/object/validation.rb
65
+ - spec/model/spec_helper.rb
66
+ - spec/test.rb
64
67
  homepage: http://github.com/alexeypetrushin/mongo_db
65
68
  licenses: []
66
69
  post_install_message:
@@ -84,5 +87,5 @@ rubyforge_project:
84
87
  rubygems_version: 1.8.6
85
88
  signing_key:
86
89
  specification_version: 3
87
- summary: MongDB ODM. In development, don't use it, come back at 11/8/25
90
+ summary: Model & Ruby driver enchancements for MongoDB
88
91
  test_files: []
data/lib/mongo_db.rb DELETED
@@ -1,3 +0,0 @@
1
- require 'mongo_db/gems'
2
- require 'mongo_db/driver'
3
- require 'mongo_db/model'
@@ -1,10 +0,0 @@
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
@@ -1,4 +0,0 @@
1
- require 'mongo_ext'
2
-
3
- require 'rspec_ext'
4
- require 'mongo_ext/spec'
File without changes
data/spec/query_spec.rb DELETED
File without changes