mongomatic 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -26,6 +26,9 @@ Mongomatic allows you to map your Ruby objects to Mongo documents. It is designe
26
26
  # or you can set it for a specific model:
27
27
  User.db = Mongo::Connection.new.db("mongomatic_test_user")
28
28
 
29
+ User.empty?
30
+ => true
31
+
29
32
  u = User.new(:name => "Ben")
30
33
  => #<User:0x00000100d0cbf8 @doc={"name"=>"Ben"}, @removed=false>
31
34
  u.valid?
@@ -35,6 +38,9 @@ Mongomatic allows you to map your Ruby objects to Mongo documents. It is designe
35
38
  => true
36
39
  u.insert
37
40
  => BSON::ObjectID('4c32834f0218236321000001')
41
+
42
+ User.empty?
43
+ => false
38
44
 
39
45
  u["name"] = "Ben Myles"
40
46
  => "Ben Myles"
@@ -52,6 +52,11 @@ module Mongomatic
52
52
  find.limit(1).next_document
53
53
  end
54
54
 
55
+ # Is the collection empty? This method is much more efficient than doing Collection.count == 0
56
+ def empty?
57
+ find.limit(1).has_next? == false
58
+ end
59
+
55
60
  # Return the number of documents in the collection
56
61
  def count
57
62
  find.count
@@ -25,6 +25,11 @@ module Mongomatic
25
25
  end
26
26
  end
27
27
 
28
+ # Is the cursor empty? This method is much more efficient than doing cursor.count == 0
29
+ def empty?
30
+ @mongo_cursor.has_next? == false
31
+ end
32
+
28
33
  def next_document
29
34
  if doc = @mongo_cursor.next_document
30
35
  @obj_class.new(doc, false)
@@ -17,6 +17,22 @@ class TestMongomatic < Test::Unit::TestCase
17
17
  assert_equal p1, Person.find_one(p1['_id'])
18
18
  end
19
19
 
20
+ should "accurately return whether the cursor is or is not empty" do
21
+ Person.collection.drop
22
+ assert Person.empty?
23
+ assert Person.find.empty?
24
+ p1 = Person.new(:name => "Jordan")
25
+ p1.insert
26
+ assert !Person.empty?
27
+ assert !Person.find.empty?
28
+ assert !Person.find({"name" => "Jordan"}).empty?
29
+ assert Person.find({"name" => "Ben"}).empty?
30
+ p1.remove!
31
+ assert Person.empty?
32
+ assert Person.find.empty?
33
+ assert Person.find({"name" => "Jordan"}).empty?
34
+ end
35
+
20
36
  should "find one with ObjectID or hash only" do
21
37
  Person.collection.drop
22
38
  Person.create_indexes
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 1
9
- version: 0.3.1
8
+ - 2
9
+ version: 0.3.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Myles
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-15 00:00:00 -07:00
17
+ date: 2010-08-19 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency