mongo_adaptor 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,6 +17,9 @@ class MongoAdaptor
17
17
  def insert(model)
18
18
  @collection.insert( process(model), { :safe => true } )
19
19
  end
20
+ def upsert(model, query = { "_id" => model.id })
21
+ @collection.update( query, { "$set" => process(model) }, { :safe => true, :upsert => true } )
22
+ end
20
23
  def update(model)
21
24
  @collection.update( { "_id" => model.id }, { "$set" => process(model) }, { :safe => true, :upsert => false } )
22
25
  end
@@ -1,3 +1,3 @@
1
1
  class MongoAdaptor
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -19,24 +19,33 @@ describe 'adapting structs into mongo' do
19
19
  let(:adaptor) { MongoAdaptor.new 'test_collection', klass }
20
20
  let(:collection) { Mongo::Configure.current.load.collection 'test_collection' }
21
21
 
22
- describe 'to insert a blank model' do
22
+ describe 'with a new model' do
23
23
  let(:model) { klass.new 'Test Model','Some Data','fake key' }
24
24
  let(:data) { collection.find({}).to_a[-1] }
25
25
 
26
- subject { adaptor.insert model }
27
-
28
- it 'changes the number of items in the collection' do
29
- expect { subject }.to change { collection.size }.by(1)
26
+ shared_examples_for 'new model' do
27
+ it 'changes the number of items in the collection' do
28
+ expect { subject }.to change { collection.size }.by(1)
29
+ end
30
+ it 'generates an _id, ignoring any set key' do
31
+ subject
32
+ data['_id'].should be_a BSON::ObjectId
33
+ data['id'].should be_nil
34
+ end
35
+ it 'sets my fields and values' do
36
+ subject
37
+ data['name'].should == 'Test Model'
38
+ data['other'].should == 'Some Data'
39
+ end
30
40
  end
31
- it 'generates an _id, ignoring any set key' do
32
- subject
33
- data['_id'].should be_a BSON::ObjectId
34
- data['id'].should be_nil
41
+
42
+ context 'inserting' do
43
+ subject { adaptor.insert model }
44
+ it_should_behave_like 'new model'
35
45
  end
36
- it 'sets my fields and values' do
37
- subject
38
- data['name'].should == 'Test Model'
39
- data['other'].should == 'Some Data'
46
+ context 'upserting' do
47
+ subject { adaptor.upsert model, {} }
48
+ it_should_behave_like 'new model'
40
49
  end
41
50
  end
42
51
 
@@ -48,11 +57,9 @@ describe 'adapting structs into mongo' do
48
57
  model.id = id
49
58
  end
50
59
 
51
- describe 'to update it' do
60
+ shared_examples_for 'modifying an existing model' do
52
61
  let(:data) { collection.find({}).to_a[-1] }
53
62
 
54
- subject { adaptor.update model }
55
-
56
63
  it 'doesnt change the number of items in the collection' do
57
64
  expect { subject }.to change { collection.size }.by(0)
58
65
  end
@@ -67,6 +74,16 @@ describe 'adapting structs into mongo' do
67
74
  end
68
75
  end
69
76
 
77
+ describe 'to update it' do
78
+ subject { adaptor.update model }
79
+ it_should_behave_like 'modifying an existing model'
80
+ end
81
+
82
+ describe 'to upsert it' do
83
+ subject { adaptor.upsert model, { name: 'My Model' } }
84
+ it_should_behave_like 'modifying an existing model'
85
+ end
86
+
70
87
  describe 'to fetch it' do
71
88
  subject { adaptor.fetch({ :_id => id }) }
72
89
  it { should be_a klass }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_adaptor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-04 00:00:00.000000000 Z
12
+ date: 2012-10-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongo
@@ -122,7 +122,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
122
122
  version: '0'
123
123
  segments:
124
124
  - 0
125
- hash: 18310851812254504
125
+ hash: 3301762641973492072
126
126
  required_rubygems_version: !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
@@ -131,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
131
  version: '0'
132
132
  segments:
133
133
  - 0
134
- hash: 18310851812254504
134
+ hash: 3301762641973492072
135
135
  requirements: []
136
136
  rubyforge_project:
137
137
  rubygems_version: 1.8.24