mongodb_model 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,56 +1,39 @@
1
1
  module Mongo::Model::Db
2
2
  module ClassMethods
3
- inheritable_accessor :_db, nil
4
- def db= v
5
- self._db = if v.is_a? ::Proc
6
- v
7
- elsif v.is_a? ::Symbol
8
- -> do
9
- db_name = ::Mongo::Model.resolve_db_alias v
10
- ::Mongo::Model.connection.db db_name
11
- end
3
+ inheritable_accessor :db_name, nil
4
+ def db name = nil
5
+ if name
6
+ self.db_name = name
12
7
  else
13
- -> {v}
8
+ Mongo.db db_name || Mongo::Model.default_database_name
14
9
  end
15
10
  end
16
11
 
17
- def db *args, &block
18
- if block
19
- self.db = block
20
- elsif !args.empty?
21
- args.size.must == 1
22
- self.db = args.first
12
+ inheritable_accessor :collection_name, nil
13
+ def collection name = nil, &block
14
+ if name
15
+ self.collection_name = name
23
16
  else
24
- (_db && _db.call) || ::Mongo::Model.db
17
+ db.collection(collection_name || default_collection_name)
25
18
  end
26
19
  end
27
20
 
28
- inheritable_accessor :_collection, nil
29
- def collection= v
30
- self._collection = if v.is_a? ::Proc
31
- v
32
- elsif v.is_a? ::Symbol
33
- -> {db.collection v}
34
- else
35
- -> {v}
21
+ protected
22
+ def default_collection_name
23
+ first_ancestor_class = ancestors.find{|a| a.is_a? Class} ||
24
+ raise("can't evaluate default collection name for #{self}!")
25
+ first_ancestor_class.alias.pluralize.underscore.to_sym
36
26
  end
37
- end
27
+ end
28
+ end
38
29
 
39
- def collection *args, &block
40
- if block
41
- self.collection = block
42
- elsif !args.empty?
43
- args.size.must == 1
44
- self.collection = args.first
45
- else
46
- (_collection && _collection.call) || db.collection(default_collection_name)
47
- end
48
- end
30
+ Mongo::Model.class_eval do
31
+ class << self
32
+ attr_accessor :default_database_name
49
33
 
50
- def default_collection_name
51
- first_ancestor_class = ancestors.find{|a| a.is_a? Class} ||
52
- raise("can't evaluate default collection name for #{self}!")
53
- first_ancestor_class.alias.pluralize.underscore.to_sym
34
+ def default_database
35
+ Mongo.db default_database_name
54
36
  end
55
37
  end
38
+ self.default_database_name = :default
56
39
  end
@@ -19,9 +19,9 @@ module Mongo::Model::FileModel
19
19
  model.errors[attr_name] = file_model.errors unless file_model.errors.empty?
20
20
  end
21
21
 
22
- after_save{|model| model.send(attr_name).save}
22
+ after_save{|model| model.send(attr_name).save!}
23
23
 
24
- after_destroy{|model| model.send(attr_name).destroy}
24
+ after_destroy{|model| model.send(attr_name).destroy!}
25
25
  end
26
26
  end
27
27
  end
@@ -25,14 +25,15 @@ module Mongo::Model
25
25
  variables == o_variables
26
26
  end
27
27
 
28
- class << self
29
- attr_accessor :db, :connection
30
- attr_required :db, :connection
31
-
32
- # Override this method to provide custom alias to db name translation,
33
- # for example db_name = my_config[alias_name]
34
- def resolve_db_alias alias_name
35
- alias_name.to_s
36
- end
37
- end
28
+ # class << self
29
+ # attr_accessor :db, :connection
30
+ # attr_required :db, :connection
31
+ #
32
+ # # Override this method to provide custom alias to db name translation,
33
+ # # for example db_name = my_config[alias_name]
34
+ # def resolve_db_alias alias_name
35
+ # db_name = alias_name.to_s
36
+ # connection.db db_name
37
+ # end
38
+ # end
38
39
  end
@@ -1,7 +1,7 @@
1
1
  class Mongo::Model::Query < Object
2
2
  attr_reader :model_class, :selector, :options
3
3
 
4
- def initialize model_class, selector = {}, options = {} *args
4
+ def initialize model_class, selector = {}, options = {}
5
5
  @model_class, @selector, @options = model_class, selector, options
6
6
  end
7
7
 
@@ -11,6 +11,10 @@ module Mongo::Model::QueryMixin
11
11
  collection.count selector, options
12
12
  end
13
13
 
14
+ def size *args
15
+ count *args
16
+ end
17
+
14
18
  def first selector = {}, options = {}
15
19
  collection.first selector, options
16
20
  end
@@ -87,7 +87,10 @@ module Mongo::Model::Scope
87
87
  #
88
88
  def limit n; query({}, limit: n) end
89
89
  def skip n; query({}, skip: n) end
90
- def sort *list; query({}, sort: list) end
90
+ def sort *list
91
+ list = list.collect{|item| item.is_a?(Array) ? item : [item, 1]}
92
+ query({}, sort: list)
93
+ end
91
94
  def snapshot; query({}, snapshot: true) end
92
95
 
93
96
  def paginate page, per_page
@@ -5,9 +5,9 @@ rspec do
5
5
  class << self
6
6
  def with_mongo_model
7
7
  with_mongo
8
-
9
- before{Mongo::Model.db = mongo.db}
10
- after{Mongo::Model.db = nil}
8
+ #
9
+ # before{Mongo::Model.db = mongo.db}
10
+ # after{Mongo::Model.db = nil}
11
11
  end
12
12
  end
13
13
  end
data/readme.md CHANGED
@@ -4,14 +4,14 @@ Object Model for MongoDB (callbacks, validations, mass-assignment, finders, ...)
4
4
  - Minimum extra abstractions, trying to keep things as close to the MongoDB semantic as possible.
5
5
  - Schema-less, dynamic (with ability to specify types for mass-assignment).
6
6
  - Models can be saved to any collection, dynamically.
7
- - Full support for embedded objects (validations, callbacks, ...).
7
+ - Full support for composite / embedded objects (validations, callbacks, ...).
8
8
  - Scope, default_scope
9
- - Doesn't try to mimic ActiveRecord, MongoDB is differrent and this tool designed to get most of it.
9
+ - Doesn't try to mimic ActiveRecord, MongoDB is differrent and the Object Model designed to get most of it.
10
10
  - Works with multiple connections and databases.
11
11
  - Associations.
12
12
  - Very small, see [code stats][code_stats].
13
13
 
14
- Other ODM usually try to cover simple but non-standard API of MongoDB behind complex ORM-like abstractions. This tool **exposes simplicity and power of MongoDB and leverages it's differences**.
14
+ Other ODM usually try to cover simple but non-standard API of MongoDB behind complex ORM-like abstractions. This tool **exposes simplicity and power of MongoDB and leverages its differences**.
15
15
 
16
16
  ``` ruby
17
17
  # Connecting to MongoDB.
data/spec/db_spec.rb CHANGED
@@ -3,61 +3,46 @@ require 'spec_helper'
3
3
  describe 'Collection & Database' do
4
4
  with_mongo_model
5
5
 
6
- before :all do
6
+ before do
7
7
  class TheModel
8
8
  inherit Mongo::Model
9
9
  end
10
10
  end
11
- after(:all){remove_constants :TheModel}
11
+ after{remove_constants :TheModel}
12
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
13
+ # Discarded
14
+ # it "global setting" do
15
+ # Mongo::Model.connection = db.connection
16
+ # Mongo::Model.db = db
17
+ #
18
+ # Mongo::Model.connection.should == db.connection
19
+ # Mongo::Model.db.should == db
20
+ # end
26
21
 
27
22
  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'
23
+ TheModel.db :special_db_name
35
24
 
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
25
+ Mongo.should_receive(:db).with(:special_db_name).and_return(:special_db)
26
+ TheModel.db.should == :special_db
43
27
  end
44
28
 
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
29
+ # Discarded
30
+ # it "should allow set collection per model" do
31
+ # Mongo::Model.db = db
32
+ #
33
+ # # TheModel.default_collection_name.should == :the_model
34
+ # # TheModel.collection.name.should == 'the_model'
35
+ #
36
+ # TheModel.collection :units
37
+ # TheModel.collection.name.should == 'units'
38
+ #
39
+ # TheModel.collection = nil
40
+ # units = db.units
41
+ # TheModel.collection units
42
+ # TheModel.collection.should == units
43
+ #
44
+ # TheModel.collection = nil
45
+ # TheModel.collection{units}
46
+ # TheModel.collection.should == units
47
+ # end
63
48
  end
data/spec/query_spec.rb CHANGED
@@ -87,4 +87,12 @@ describe "Model Query" do
87
87
  u = SpecialUnit.query(name: 'Zeratul', status: 'active').build age: 500
88
88
  u.status.should == 'active'
89
89
  end
90
+
91
+ it "where" do
92
+ # Unit.where(name: 'Zeratul').should == Mongo::Model::Query.new(Unit, {name: 'Zeratul'}, {})
93
+
94
+ query = Unit.where(name: 'Zeratul')
95
+ query = query.where(race: 'Protoss')
96
+ query.should == Mongo::Model::Query.new(Unit, {name: 'Zeratul', race: 'Protoss'}, {})
97
+ end
90
98
  end
data/spec/scope_spec.rb CHANGED
@@ -150,9 +150,17 @@ describe "Scope" do
150
150
 
151
151
  describe 'handy scopes' do
152
152
  it "limit, skip, sort" do
153
- query = Unit.skip(30).limit(10).sort([name: 1]).snapshot
153
+ query = Unit.skip(30).limit(10).sort([:name, 1]).snapshot
154
154
  query.selector.should == {}
155
- query.options.should == {skip: 30, limit: 10, sort: [[name: 1]], snapshot: true}
155
+ query.options.should == {skip: 30, limit: 10, sort: [[:name, 1]], snapshot: true}
156
+ end
157
+
158
+ it "sort should understand simplified form" do
159
+ query = Unit.sort(:name)
160
+ query.options.should == {sort: [[:name, 1]]}
161
+
162
+ query = Unit.sort(:race, :name)
163
+ query.options.should == {sort: [[:race, 1], [:name, 1]]}
156
164
  end
157
165
 
158
166
  it 'paginate' do
@@ -34,10 +34,10 @@ describe "Validations" do
34
34
  unit.errors.clear
35
35
  unit.save.should be_true
36
36
 
37
- unit.errors[:name] = 'hairy error'
37
+ unit.errors.stub(:empty?).and_return(false)
38
38
  unit.save.should be_false
39
39
 
40
- unit.errors.clear
40
+ unit.errors.stub(:empty?).and_return(true)
41
41
  unit.save.should be_true
42
42
  end
43
43
 
@@ -93,6 +93,17 @@ describe "Validations" do
93
93
  end
94
94
  end
95
95
 
96
+ it "should clear errors before validation" do
97
+ class Unit < BaseUnit
98
+ validates_presence_of :name
99
+ end
100
+
101
+ unit = Unit.new
102
+ unit.should_not be_valid
103
+ unit.name = 'Zeratul'
104
+ unit.should be_valid
105
+ end
106
+
96
107
  describe "special" do
97
108
  it 'validates_uniqueness_of' do
98
109
  class Unit < BaseUnit
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongodb_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
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-09-06 00:00:00.000000000Z
12
+ date: 2011-09-15 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongodb
16
- requirement: &2839010 !ruby/object:Gem::Requirement
16
+ requirement: &2843630 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2839010
24
+ version_requirements: *2843630
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: file_model
27
- requirement: &2845520 !ruby/object:Gem::Requirement
27
+ requirement: &2843390 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2845520
35
+ version_requirements: *2843390
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: validatable2
38
- requirement: &2846710 !ruby/object:Gem::Requirement
38
+ requirement: &2843150 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2846710
46
+ version_requirements: *2843150
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: ruby_ext
49
- requirement: &2847870 !ruby/object:Gem::Requirement
49
+ requirement: &2842910 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *2847870
57
+ version_requirements: *2842910
58
58
  description:
59
59
  email:
60
60
  executables: []