mongo_delegate 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = mongo_delegate
2
2
 
3
- Description goes here.
3
+ A DelegateCollection wraps
4
4
 
5
5
  == Note on Patches/Pull Requests
6
6
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -19,6 +19,7 @@ class CompositeCursor
19
19
  attr_accessor :colls, :selector, :options
20
20
  include FromHash
21
21
  include Enumerable
22
+ def to_af; rows; end
22
23
  fattr(:rows) do
23
24
  cursors.map { |x| x.to_af }.flatten
24
25
  end
@@ -26,7 +27,9 @@ class CompositeCursor
26
27
  rows.each(&b)
27
28
  end
28
29
  def count
29
- rows.size
30
+ colls.map do |coll|
31
+ coll.scope_ne('_duplicate' => true).find(selector).count
32
+ end.sum
30
33
  end
31
34
  def first
32
35
  rows.first
@@ -25,7 +25,8 @@ module Mongo
25
25
  [local,remote]
26
26
  end
27
27
  def save(d)
28
- local.save(d.merge('_duplicate' => true))
28
+ d = d.merge('_duplicate' => true) if d['_id'] && remote.find_one('_id' => d['_id'])
29
+ local.save(d)
29
30
  end
30
31
  end
31
32
  end
@@ -15,7 +15,7 @@ context Mongo::DelegatingCollection do
15
15
  @d.find.count.should == @all.size
16
16
  end
17
17
  it 'find doesnt return dups' do
18
- @d.local.save(@d.remote.find_one(:name => 'Randy'))
18
+ @d.save(@d.remote.find_one(:name => 'Randy'))
19
19
  @d.find.count.should == @all.size
20
20
  @d.find.unpruned_rows.size.should == @all.size + 1
21
21
  end
@@ -30,7 +30,7 @@ context Mongo::DelegatingCollection do
30
30
  @d.find_one(:name => 'Randy')['age'].should == 'old'
31
31
  end
32
32
  it 'respects limit' do
33
- @d.find({},:limit => 4).count.should == 4
33
+ @d.find({},:limit => 4).to_af.size.should == 4
34
34
  expected_names = @d.find.map { |x| x['name'] }[0...4].sort
35
35
  @d.find({},:limit => 4).map { |x| x['name'] }.sort.should == expected_names
36
36
  end
@@ -39,21 +39,26 @@ context Mongo::DelegatingCollection do
39
39
  @d.find({},:limit => 2).rows
40
40
  end
41
41
  it 'respects skip' do
42
- @d.find({},:skip => 1).count.should == 5
42
+ @d.find({},:skip => 1).to_af.size.should == 5
43
43
  @d.find({},:skip => 1).map { |x| x['name'] }.sort.should == @all.reject { |x| x == 'Mike' }.sort
44
44
  end
45
45
  it 'respects skip whole collection' do
46
- @d.find({},:skip => 4).count.should == 2
46
+ @d.find({},:skip => 4).to_af.size.should == 2
47
47
  end
48
48
  it 'wont return remote records that were skipped locally' do
49
49
  @d.local.save(@d.remote.find_one(:name => 'Randy'))
50
- @d.find({},:skip => 5).count.should == 1
50
+ @d.find({},:skip => 5).to_af.size.should == 1
51
51
  end
52
52
  it 'respects skip and limit' do
53
- @d.find({},:skip => 1, :limit => 4).count.should == 4
53
+ @d.find({},:skip => 1, :limit => 4).to_af.size.should == 4
54
+ end
55
+ it 'count doesnt fetch records' do
56
+ mock.instance_of(Mongo::Cursor).to_a.times(0)
57
+ @d.count.should == 6
54
58
  end
55
- # it 'count doesnt fetch records' do
56
- # mock.instance_of(Mongo::Cursor).to_a.times(0)
59
+ # it 'count honors remote deletes' do
60
+ # @d.remote.find.each { |x| @d.save(x) }
61
+ # @d.remote.remove
57
62
  # @d.count.should == 6
58
63
  # end
59
64
  end
@@ -65,5 +70,17 @@ context Mongo::DelegatingCollection do
65
70
  @d.remote.count.should == @remotes.size
66
71
  @d.local.count.should == @locals.size + 1
67
72
  end
73
+ it 'updating a non-dup shouldnt mark it as a dup' do
74
+ r = @d.local.find_one(:name => 'Mike').merge(:foo => :bar)
75
+ @d.save(r)
76
+ @d.count.should == 6
77
+ @d.local.find_one(:name => 'Mike')['_duplicate'].should be_nil
78
+ end
79
+ it 'saving a dup for first time should mark it as a dup' do
80
+ r = @d.remote.find_one(:name => 'Randy')
81
+ @d.save(r)
82
+ @d.count.should == 6
83
+ @d.local.find_one(:name => 'Randy')['_duplicate'].should == true
84
+ end
68
85
  end
69
86
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_delegate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Harris