mongoid 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.1
1
+ 0.5.2
@@ -24,7 +24,7 @@ module Mongoid #:nodoc:
24
24
  # query has returned it will provided a grouping of keys with counts.
25
25
  #
26
26
  # Example:
27
- #
27
+ #
28
28
  # <tt>criteria.select(:field1).where(:field1 => "Title").aggregate(Person)</tt>
29
29
  def aggregate(klass = nil)
30
30
  @klass = klass if klass
@@ -38,7 +38,7 @@ module Mongoid #:nodoc:
38
38
  #
39
39
  # Options:
40
40
  #
41
- # selections: A +Hash+ where the key is the field name and the value is an
41
+ # selections: A +Hash+ where the key is the field name and the value is an
42
42
  # +Array+ of values that must all match.
43
43
  #
44
44
  # Example:
@@ -52,6 +52,22 @@ module Mongoid #:nodoc:
52
52
  selections.each { |key, value| @selector[key] = { "$all" => value } }; self
53
53
  end
54
54
 
55
+ # Get the count of matching documents in the database for the +Criteria+.
56
+ #
57
+ # Options:
58
+ #
59
+ # klass: Optional class that the collection will be retrieved from.
60
+ #
61
+ # Example:
62
+ #
63
+ # <tt>criteria.count</tt>
64
+ #
65
+ # Returns: <tt>Integer</tt>
66
+ def count(klass = nil)
67
+ @klass = klass if klass
68
+ return @klass.collection.find(@selector, @options).count
69
+ end
70
+
55
71
  # Adds a criterion to the +Criteria+ that specifies values that are not allowed
56
72
  # to match any document in the database. The MongoDB conditional operator that
57
73
  # will be used is "$ne".
@@ -111,7 +127,7 @@ module Mongoid #:nodoc:
111
127
  # query has returned it will provided a grouping of keys with objects.
112
128
  #
113
129
  # Example:
114
- #
130
+ #
115
131
  # <tt>criteria.select(:field1).where(:field1 => "Title").group(Person)</tt>
116
132
  def group(klass = nil)
117
133
  @klass = klass if klass
@@ -256,8 +272,8 @@ module Mongoid #:nodoc:
256
272
  end
257
273
 
258
274
  # Adds a criterion to the +Criteria+ that specifies how many results to skip
259
- # when returning Documents. This is mostly used in conjunction with
260
- # <tt>limit()</tt> to handle paginated results, and is similar to the
275
+ # when returning Documents. This is mostly used in conjunction with
276
+ # <tt>limit()</tt> to handle paginated results, and is similar to the
261
277
  # traditional "offset" parameter.
262
278
  #
263
279
  # Options:
@@ -273,9 +289,9 @@ module Mongoid #:nodoc:
273
289
  @options[:skip] = value; self
274
290
  end
275
291
 
276
- # Translate the supplied arguments into a +Criteria+ object.
277
- #
278
- # If the passed in args is a single +String+, then it will
292
+ # Translate the supplied arguments into a +Criteria+ object.
293
+ #
294
+ # If the passed in args is a single +String+, then it will
279
295
  # construct an id +Criteria+ from it.
280
296
  #
281
297
  # If the passed in args are a type and a hash, then it will construct
@@ -300,7 +316,7 @@ module Mongoid #:nodoc:
300
316
 
301
317
  # Adds a criterion to the +Criteria+ that specifies values that must
302
318
  # be matched in order to return results. This is similar to a SQL "WHERE"
303
- # clause. This is the actual selector that will be provided to MongoDB,
319
+ # clause. This is the actual selector that will be provided to MongoDB,
304
320
  # similar to the Javascript object that is used when performing a find()
305
321
  # in the MongoDB console.
306
322
  #
@@ -39,6 +39,14 @@ module Mongoid #:nodoc:
39
39
  @collection ||= Mongoid.database.collection(@collection_name)
40
40
  end
41
41
 
42
+ # Returns a count of matching records in the database based on the
43
+ # provided arguments.
44
+ #
45
+ # <tt>Person.count(:first, :conditions => { :attribute => "value" })</tt>
46
+ def count(*args)
47
+ Criteria.translate(*args).count(self)
48
+ end
49
+
42
50
  # Defines all the fields that are accessable on the Document
43
51
  # For each field that is defined, a getter and setter will be
44
52
  # added as an instance method to the Document.
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.5.1"
8
+ s.version = "0.5.2"
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-10-27}
12
+ s.date = %q{2009-10-28}
13
13
  s.email = %q{durran@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.textile"
@@ -54,6 +54,25 @@ describe Mongoid::Criteria do
54
54
 
55
55
  end
56
56
 
57
+ describe "#count" do
58
+
59
+ before do
60
+ @criteria = Mongoid::Criteria.new(:all, Person)
61
+ @selector = { :test => "Testing" }
62
+ @criteria.where(@selector)
63
+ @collection = mock
64
+ @cursor = mock
65
+ Person.expects(:collection).returns(@collection)
66
+ end
67
+
68
+ it "returns the count from the cursor without creating the documents" do
69
+ @collection.expects(:find).with(@selector, {}).returns(@cursor)
70
+ @cursor.expects(:count).returns(10)
71
+ @criteria.count.should == 10
72
+ end
73
+
74
+ end
75
+
57
76
  describe "#excludes" do
58
77
 
59
78
  it "adds the $ne query to the selector" do
@@ -109,6 +109,21 @@ describe Mongoid::Document do
109
109
 
110
110
  end
111
111
 
112
+ describe "#count" do
113
+
114
+ before do
115
+ @params = { :conditions => { :title => "Sir" } }
116
+ @criteria = mock
117
+ end
118
+
119
+ it "delegates to the criteria api" do
120
+ Mongoid::Criteria.expects(:translate).with(@params).returns(@criteria)
121
+ @criteria.expects(:count).with(Person).returns(10)
122
+ Person.count(@params).should == 10
123
+ end
124
+
125
+ end
126
+
112
127
  describe "#field" do
113
128
 
114
129
  context "with no options" do
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.5.1
4
+ version: 0.5.2
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-10-27 00:00:00 -04:00
12
+ date: 2009-10-28 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency