mongomatic 0.0.3 → 0.0.4
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/lib/mongomatic/cursor.rb +21 -1
- data/test/test_meta_mongo.rb +26 -0
- metadata +2 -2
data/lib/mongomatic/cursor.rb
CHANGED
@@ -7,7 +7,7 @@ module Mongomatic
|
|
7
7
|
@mongo_cursor = mongo_cursor
|
8
8
|
|
9
9
|
@mongo_cursor.public_methods(false).each do |meth|
|
10
|
-
next if
|
10
|
+
next if self.methods.include?(meth.to_sym)
|
11
11
|
(class << self; self; end).class_eval do
|
12
12
|
define_method meth do |*args|
|
13
13
|
@mongo_cursor.send meth, *args
|
@@ -33,5 +33,25 @@ module Mongomatic
|
|
33
33
|
def to_a
|
34
34
|
@mongo_cursor.to_a.collect { |doc| @obj_class.new(doc) }
|
35
35
|
end
|
36
|
+
|
37
|
+
def current_limit
|
38
|
+
@mongo_cursor.limit
|
39
|
+
end
|
40
|
+
|
41
|
+
def limit(number_to_return)
|
42
|
+
@mongo_cursor.limit(number_to_return); self
|
43
|
+
end
|
44
|
+
|
45
|
+
def current_skip
|
46
|
+
@mongo_cursor.skip
|
47
|
+
end
|
48
|
+
|
49
|
+
def skip(number_to_skip)
|
50
|
+
@mongo_cursor.skip(number_to_skip); self
|
51
|
+
end
|
52
|
+
|
53
|
+
def sort(key_or_list, direction = nil)
|
54
|
+
@mongo_cursor.sort(key_or_list, direction); self
|
55
|
+
end
|
36
56
|
end
|
37
57
|
end
|
data/test/test_meta_mongo.rb
CHANGED
@@ -39,4 +39,30 @@ class TestMongomatic < Test::Unit::TestCase
|
|
39
39
|
found = cursor.next
|
40
40
|
assert_nil found
|
41
41
|
end
|
42
|
+
|
43
|
+
should "be able to limit and sort" do
|
44
|
+
Person.collection.remove
|
45
|
+
p = Person.new(:name => "Ben", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
46
|
+
assert p.insert.is_a?(BSON::ObjectID)
|
47
|
+
assert_equal 1, Person.collection.count
|
48
|
+
p2 = Person.new(:name => "Ben2", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
49
|
+
assert p2.insert.is_a?(BSON::ObjectID)
|
50
|
+
assert_equal 2, Person.collection.count
|
51
|
+
|
52
|
+
cursor = Person.find({"_id" => p["_id"]})
|
53
|
+
found = cursor.next
|
54
|
+
assert_equal p, found
|
55
|
+
|
56
|
+
cursor = Person.find()
|
57
|
+
assert_equal 0, cursor.current_limit
|
58
|
+
assert_equal 2, cursor.to_a.size
|
59
|
+
cursor = Person.find().limit(1)
|
60
|
+
assert_equal 1, cursor.current_limit
|
61
|
+
assert_equal 1, cursor.to_a.size
|
62
|
+
cursor = Person.find().limit(1)
|
63
|
+
assert_equal p, cursor.next
|
64
|
+
assert_equal nil, cursor.next
|
65
|
+
cursor = Person.find().limit(1).skip(1)
|
66
|
+
assert_equal p2, cursor.next
|
67
|
+
end
|
42
68
|
end
|