mongoid 0.9.5 → 0.9.6

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/README.textile CHANGED
@@ -31,7 +31,8 @@
31
31
  Initialize Mongoid:
32
32
 
33
33
  <pre>
34
- Mongoid.connect_to("myapp_database_name")
34
+ connection = Mongo::Connection.new
35
+ Mongoid.database = connection.db("my_db_name")
35
36
  </pre>
36
37
 
37
38
  Example of a simple domain model:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.5
1
+ 0.9.6
data/lib/mongoid.rb CHANGED
@@ -52,7 +52,10 @@ require "mongoid/document"
52
52
  module Mongoid
53
53
 
54
54
  # Raised when the database connection has not been set up.
55
- class NoConnectionError < RuntimeError; end
55
+ class InvalidDatabaseError < RuntimeError; end
56
+
57
+ # Raised when invalid options are passed into a constructor.
58
+ class InvalidOptionsError < RuntimeError; end
56
59
 
57
60
  # Raised when an association is defined on the class, but the
58
61
  # attribute in the hash is not an Array or Hash, or when
@@ -74,21 +77,15 @@ module Mongoid
74
77
  end
75
78
  end
76
79
 
77
- # Raised when invalid options are passed into a constructor.
78
- class InvalidOptionsError < RuntimeError; end
79
-
80
- # Connect to the database name supplied. This should be run
81
- # for initial setup, potentially in a rails initializer.
82
- def self.connect_to(name)
83
- @@connection ||= Mongo::Connection.new
84
- @@database ||= @@connection.db(name)
80
+ # Sets the Mongo::DB to be used.
81
+ def self.database=(db)
82
+ raise InvalidDatabaseError.new("Database should be a Mongo::DB, not #{db.class.name}") unless db.kind_of?(Mongo::DB)
83
+ @@database = db
85
84
  end
86
85
 
87
- # Get the MongoDB database. If initialization via Mongoid.connect_to()
88
- # has not happened, an exception will occur.
86
+ # Returns the Mongo::DB to use or raise an error if none was set.
89
87
  def self.database
90
- raise NoConnectionError unless @@database
91
- @@database
88
+ @@database || (raise InvalidDatabaseError.new("No database has been set"))
92
89
  end
93
90
 
94
91
  end
@@ -13,8 +13,10 @@ module Mongoid #:nodoc:
13
13
  # Example:
14
14
  #
15
15
  # <tt>DeleteAll.execute(Person, :conditions => { :field => "value" })</tt>
16
- def self.execute(klass, params)
17
- klass.find(:all, params).each { |doc| Delete.execute(doc) }
16
+ def self.execute(klass, params = nil)
17
+ params ? klass.find(:all, params).each do
18
+ |doc| Delete.execute(doc)
19
+ end : klass.collection.drop
18
20
  end
19
21
  end
20
22
  end
@@ -60,7 +60,7 @@ module Mongoid #:nodoc:
60
60
  #
61
61
  # Options:
62
62
  #
63
- # selections: A +Hash+ where the key is the field name and the value is an
63
+ # attributes: A +Hash+ where the key is the field name and the value is an
64
64
  # +Array+ of values that must all match.
65
65
  #
66
66
  # Example:
@@ -70,8 +70,8 @@ module Mongoid #:nodoc:
70
70
  # <tt>criteria.all(:field1 => ["value1", "value2"], :field2 => ["value1"])</tt>
71
71
  #
72
72
  # Returns: <tt>self</tt>
73
- def all(selections = {})
74
- selections.each { |key, value| @selector[key] = { "$all" => value } }; self
73
+ def all(attributes = {})
74
+ update_selector(attributes, "$all")
75
75
  end
76
76
 
77
77
  # Adds a criterion to the +Criteria+ that specifies values that must
@@ -125,7 +125,7 @@ module Mongoid #:nodoc:
125
125
  #
126
126
  # Options:
127
127
  #
128
- # excludes: A +Hash+ where the key is the field name and the value is a
128
+ # attributes: A +Hash+ where the key is the field name and the value is a
129
129
  # value that must not be equal to the corresponding field value in the database.
130
130
  #
131
131
  # Example:
@@ -135,8 +135,8 @@ module Mongoid #:nodoc:
135
135
  # <tt>criteria.excludes(:field1 => "value1", :field2 => "value1")</tt>
136
136
  #
137
137
  # Returns: <tt>self</tt>
138
- def excludes(exclusions = {})
139
- exclusions.each { |key, value| @selector[key] = { "$ne" => value } }; self
138
+ def excludes(attributes = {})
139
+ update_selector(attributes, "$ne")
140
140
  end
141
141
 
142
142
  # Adds a criterion to the +Criteria+ that specifies additional options
@@ -194,7 +194,7 @@ module Mongoid #:nodoc:
194
194
  #
195
195
  # Options:
196
196
  #
197
- # inclusions: A +Hash+ where the key is the field name and the value is an
197
+ # attributes: A +Hash+ where the key is the field name and the value is an
198
198
  # +Array+ of values that any can match.
199
199
  #
200
200
  # Example:
@@ -204,8 +204,8 @@ module Mongoid #:nodoc:
204
204
  # <tt>criteria.in(:field1 => ["value1", "value2"], :field2 => ["value1"])</tt>
205
205
  #
206
206
  # Returns: <tt>self</tt>
207
- def in(inclusions = {})
208
- inclusions.each { |key, value| @selector[key] = { "$in" => value } }; self
207
+ def in(attributes = {})
208
+ update_selector(attributes, "$in")
209
209
  end
210
210
 
211
211
  # Adds a criterion to the +Criteria+ that specifies an id that must be matched.
@@ -510,5 +510,10 @@ module Mongoid #:nodoc:
510
510
  end
511
511
  end
512
512
 
513
+ # Update the selector setting the operator on the value for each key in the
514
+ # supplied attributes +Hash+.
515
+ def update_selector(attributes, operator)
516
+ attributes.each { |key, value| @selector[key] = { operator => value } }; self
517
+ end
513
518
  end
514
519
  end
@@ -4,15 +4,11 @@ module Mongoid #:nodoc:
4
4
  module Hash #:nodoc:
5
5
  module Accessors #:nodoc:
6
6
  def insert(key, attrs)
7
- if key.singular?
8
- self[key] = attrs unless self[key]
9
- self[key] = self[key].merge(attrs) if self[key]
7
+ elements = self[key]
8
+ if elements
9
+ elements.update(attrs)
10
10
  else
11
- if elements = self[key]
12
- elements.update(attrs)
13
- else
14
- self[key] = [attrs]
15
- end
11
+ self[key] = key.singular? ? attrs : [attrs]
16
12
  end
17
13
  end
18
14
  end
data/mongoid.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.9.5"
8
+ s.version = "0.9.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Durran Jordan"]
12
- s.date = %q{2009-12-08}
12
+ s.date = %q{2009-12-09}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.textile"
@@ -112,7 +112,8 @@ Gem::Specification.new do |s|
112
112
  "spec/unit/mongoid/field_spec.rb",
113
113
  "spec/unit/mongoid/finders_spec.rb",
114
114
  "spec/unit/mongoid/timestamps_spec.rb",
115
- "spec/unit/mongoid/versioning_spec.rb"
115
+ "spec/unit/mongoid/versioning_spec.rb",
116
+ "spec/unit/mongoid_spec.rb"
116
117
  ]
117
118
  s.homepage = %q{http://github.com/durran/mongoid}
118
119
  s.rdoc_options = ["--charset=UTF-8"]
@@ -163,7 +164,8 @@ Gem::Specification.new do |s|
163
164
  "spec/unit/mongoid/field_spec.rb",
164
165
  "spec/unit/mongoid/finders_spec.rb",
165
166
  "spec/unit/mongoid/timestamps_spec.rb",
166
- "spec/unit/mongoid/versioning_spec.rb"
167
+ "spec/unit/mongoid/versioning_spec.rb",
168
+ "spec/unit/mongoid_spec.rb"
167
169
  ]
168
170
 
169
171
  if s.respond_to? :specification_version then
data/spec/spec_helper.rb CHANGED
@@ -8,7 +8,8 @@ require "mocha"
8
8
  require "mongoid"
9
9
  require "spec"
10
10
 
11
- Mongoid.connect_to("mongoid_test")
11
+ connection = Mongo::Connection.new
12
+ Mongoid.database = connection.db("mongoid_test")
12
13
 
13
14
  Spec::Runner.configure do |config|
14
15
  config.mock_with :mocha
@@ -8,13 +8,34 @@ describe Mongoid::Commands::DeleteAll do
8
8
  @doc = mock
9
9
  @docs = [@doc]
10
10
  @klass = mock
11
- @conditions = { :conditions => { :title => "Sir" } }
12
11
  end
13
12
 
14
- it "destroys each document that the criteria finds" do
15
- @klass.expects(:find).with(:all, @conditions).returns(@docs)
16
- Mongoid::Commands::Delete.expects(:execute).with(@doc)
17
- Mongoid::Commands::DeleteAll.execute(@klass, @conditions)
13
+ context "when conditions supplied" do
14
+
15
+ before do
16
+ @conditions = { :conditions => { :title => "Sir" } }
17
+ end
18
+
19
+ it "deletes each document that the criteria finds" do
20
+ @klass.expects(:find).with(:all, @conditions).returns(@docs)
21
+ Mongoid::Commands::Delete.expects(:execute).with(@doc)
22
+ Mongoid::Commands::DeleteAll.execute(@klass, @conditions)
23
+ end
24
+
25
+ end
26
+
27
+ context "when no conditions supplied" do
28
+
29
+ before do
30
+ @collection = mock
31
+ end
32
+
33
+ it "drops the collection" do
34
+ @klass.expects(:collection).returns(@collection)
35
+ @collection.expects(:drop)
36
+ Mongoid::Commands::DeleteAll.execute(@klass)
37
+ end
38
+
18
39
  end
19
40
 
20
41
  end
@@ -71,7 +71,7 @@ describe Mongoid::Extensions::Hash::Accessors do
71
71
  @new = { :_id => 2, :first_name => "Test2", :last_name => "User2" }
72
72
  end
73
73
 
74
- it "updates the existing attribute" do
74
+ it "adds the new attribute" do
75
75
  @hash.insert(:name, @new)
76
76
  @hash[:name].should == @new
77
77
  end
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid do
4
+
5
+ describe ".database=" do
6
+
7
+ context "when object provided is not a Mongo::DB" do
8
+
9
+ it "raises an error" do
10
+ lambda { Mongoid.database = "Test" }.should raise_error
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+
17
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5
4
+ version: 0.9.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-08 00:00:00 -05:00
12
+ date: 2009-12-09 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -189,6 +189,7 @@ files:
189
189
  - spec/unit/mongoid/finders_spec.rb
190
190
  - spec/unit/mongoid/timestamps_spec.rb
191
191
  - spec/unit/mongoid/versioning_spec.rb
192
+ - spec/unit/mongoid_spec.rb
192
193
  has_rdoc: true
193
194
  homepage: http://github.com/durran/mongoid
194
195
  licenses: []
@@ -262,3 +263,4 @@ test_files:
262
263
  - spec/unit/mongoid/finders_spec.rb
263
264
  - spec/unit/mongoid/timestamps_spec.rb
264
265
  - spec/unit/mongoid/versioning_spec.rb
266
+ - spec/unit/mongoid_spec.rb