mongomodel 0.2.14 → 0.2.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -111,7 +111,7 @@ module MongoModel
111
111
 
112
112
  class Proxy < Base::Proxy
113
113
  # Pass these methods to the scope rather than the Array target
114
- OVERRIDE_METHODS = [ :find ]
114
+ OVERRIDE_METHODS = [ :find, :first, :last, :count ]
115
115
 
116
116
  delegate :ensure_class, :to => :association
117
117
 
@@ -21,19 +21,19 @@ module MongoModel
21
21
  end
22
22
  end
23
23
 
24
- def first
24
+ def first(count=nil)
25
25
  if loaded?
26
- @documents.first
26
+ count ? to_a.first(count) : to_a.first
27
27
  else
28
- @first ||= limit(1).to_a[0]
28
+ count ? limit(count).to_a : limit(1).to_a[0]
29
29
  end
30
30
  end
31
31
 
32
- def last
32
+ def last(count=nil)
33
33
  if loaded?
34
- @documents.last
34
+ count ? to_a.last(count) : to_a.last
35
35
  else
36
- @last ||= reverse_order.limit(1).to_a[0]
36
+ count ? reverse_order.limit(count).to_a : reverse_order.limit(1).to_a[0]
37
37
  end
38
38
  end
39
39
 
@@ -1,3 +1,3 @@
1
1
  module MongoModel
2
- VERSION = "0.2.14"
2
+ VERSION = "0.2.15"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'active_support/time'
2
3
 
3
4
  module MongoModel
4
5
  describe Scope do
@@ -316,47 +317,78 @@ module MongoModel
316
317
  end
317
318
 
318
319
  describe "#first" do
319
- context "when no matching documents exist" do
320
- before(:each) { model.stub_find([]) }
320
+ context "with count argument" do
321
+ context "when no matching documents exist" do
322
+ before(:each) { model.stub_find([]) }
321
323
 
322
- always do
323
- it "should return nil" do
324
- subject.first.should be_nil
324
+ always do
325
+ it "should return an empty array" do
326
+ subject.first(3).should == []
327
+ end
325
328
  end
326
- end
327
329
 
328
- subject_loaded do
329
- it "should not perform a find" do
330
- model.should_not_find do
331
- subject.first
330
+ subject_loaded do
331
+ it "should not perform a find" do
332
+ model.should_not_find { subject.first(3) }
333
+ end
334
+ end
335
+
336
+ subject_not_loaded do
337
+ it "should find with a limit of 3" do
338
+ model.should_find(finder_options.merge(:limit => 3), []) { subject.first(3) }
332
339
  end
333
340
  end
334
341
  end
342
+
343
+ context "when matching documents exist" do
344
+ before(:each) { model.stub_find([posts[0], posts[1], posts[2]]) }
335
345
 
336
- subject_not_loaded do
337
- it "should find with a limit of 1" do
338
- model.should_find(finder_options.merge(:limit => 1), []) do
339
- subject.first
346
+ always do
347
+ it "should return the first documents in an array" do
348
+ subject.first(3).should == [posts[0], posts[1], posts[2]]
340
349
  end
341
350
  end
342
351
  end
343
352
  end
344
353
 
345
- context "when matching documents exist" do
346
- let(:post) { posts.first }
347
- before(:each) { model.stub_find([post]) }
354
+ context "with no argument" do
355
+ context "when no matching documents exist" do
356
+ before(:each) { model.stub_find([]) }
348
357
 
349
- always do
350
- it "should return the first document" do
351
- subject.first.should == post
358
+ always do
359
+ it "should return nil" do
360
+ subject.first.should be_nil
361
+ end
362
+ end
363
+
364
+ subject_loaded do
365
+ it "should not perform a find" do
366
+ model.should_not_find { subject.first }
367
+ end
368
+ end
369
+
370
+ subject_not_loaded do
371
+ it "should find with a limit of 1" do
372
+ model.should_find(finder_options.merge(:limit => 1), []) { subject.first }
373
+ end
352
374
  end
353
375
  end
376
+
377
+ context "when matching documents exist" do
378
+ before(:each) { model.stub_find([posts[0]]) }
379
+
380
+ always do
381
+ it "should return the first document" do
382
+ subject.first.should == posts[0]
383
+ end
384
+ end
354
385
 
355
- subject_not_loaded do
356
- it "should cache find result" do
357
- model.should_find(finder_options.merge(:limit => 1), [post]) do
358
- subject.first
359
- subject.first
386
+ subject_not_loaded do
387
+ it "should cache find result" do
388
+ model.should_find(finder_options.merge(:limit => 1), [posts[0]]) do
389
+ subject.first
390
+ subject.first
391
+ end
360
392
  end
361
393
  end
362
394
  end
@@ -369,47 +401,79 @@ module MongoModel
369
401
  finder_options.merge(:order => order.reverse.to_a)
370
402
  end
371
403
 
372
- context "when no matching documents exist" do
373
- before(:each) { model.stub_find([]) }
404
+ context "with count argument" do
405
+ context "when no matching documents exist" do
406
+ before(:each) { model.stub_find([]) }
374
407
 
375
- always do
376
- it "should return nil" do
377
- subject.last.should be_nil
408
+ always do
409
+ it "should return an empty array" do
410
+ subject.last(2).should == []
411
+ end
378
412
  end
379
- end
380
413
 
381
- subject_loaded do
382
- it "should not perform a find" do
383
- model.should_not_find do
384
- subject.last
414
+ subject_loaded do
415
+ it "should not perform a find" do
416
+ model.should_not_find { subject.last(2) }
417
+ end
418
+ end
419
+
420
+ subject_not_loaded do
421
+ it "should find with a limit of 2" do
422
+ model.should_find(reversed_finder_options.merge(:limit => 2), []) { subject.last(2) }
385
423
  end
386
424
  end
387
425
  end
426
+
427
+ context "when matching documents exist" do
428
+ before(:each) { model.stub_find([posts[0], posts[1]]) }
388
429
 
389
- subject_not_loaded do
390
- it "should find with a limit of 1" do
391
- model.should_find(reversed_finder_options.merge(:limit => 1), []) do
392
- subject.last
430
+ always do
431
+ it "should return the last documents in an array" do
432
+ subject.last(2).should == [posts[0], posts[1]]
393
433
  end
394
434
  end
395
435
  end
396
436
  end
397
437
 
398
- context "when matching documents exist" do
399
- let(:post) { posts.last }
400
- before(:each) { model.stub_find([post]) }
438
+ context "with no argument" do
439
+ context "when no matching documents exist" do
440
+ before(:each) { model.stub_find([]) }
401
441
 
402
- always do
403
- it "should return the last document" do
404
- subject.last.should == post
442
+ always do
443
+ it "should return nil" do
444
+ subject.last.should be_nil
445
+ end
446
+ end
447
+
448
+ subject_loaded do
449
+ it "should not perform a find" do
450
+ model.should_not_find { subject.last }
451
+ end
452
+ end
453
+
454
+ subject_not_loaded do
455
+ it "should find with a limit of 1" do
456
+ model.should_find(reversed_finder_options.merge(:limit => 1), []) { subject.last }
457
+ end
405
458
  end
406
459
  end
460
+
461
+ context "when matching documents exist" do
462
+ let(:post) { posts.last }
463
+ before(:each) { model.stub_find([post]) }
464
+
465
+ always do
466
+ it "should return the last document" do
467
+ subject.last.should == post
468
+ end
469
+ end
407
470
 
408
- subject_not_loaded do
409
- it "should cache find result" do
410
- model.should_find(reversed_finder_options.merge(:limit => 1), [post]) do
411
- subject.last
412
- subject.last
471
+ subject_not_loaded do
472
+ it "should cache find result" do
473
+ model.should_find(reversed_finder_options.merge(:limit => 1), [post]) do
474
+ subject.last
475
+ subject.last
476
+ end
413
477
  end
414
478
  end
415
479
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongomodel
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 14
10
- version: 0.2.14
9
+ - 15
10
+ version: 0.2.15
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sam Pohlenz
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-15 00:00:00 +10:30
18
+ date: 2010-12-23 00:00:00 +10:30
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency