mongomatic 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 ["next_document","each","to_a"].include?(meth.to_s)
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
@@ -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
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Myles