adapter-mongo 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,6 @@ language: ruby
2
2
  rvm:
3
3
  - 1.8.7
4
4
  - ree
5
- - 1.9.2
6
5
  - 1.9.3
7
6
  notifications:
8
7
  email: false
data/Gemfile CHANGED
@@ -1,18 +1,15 @@
1
1
  source "http://rubygems.org"
2
2
  gemspec
3
3
 
4
- gem 'rake', '~> 0.9.0'
5
-
6
- gem 'mongo', '~> 1.6'
7
- gem 'bson_ext', '~> 1.6', :require => false
4
+ gem 'rake'
5
+ gem 'bson_ext', '~> 1.8'
8
6
 
9
7
  group(:test) do
10
- gem 'rspec', '~> 2.8'
8
+ gem 'rspec'
11
9
  end
12
10
 
13
11
  group(:guard) do
14
- gem 'guard', '~> 1.0.0'
15
- gem 'guard-rspec', '~> 0.6.0'
16
- gem 'guard-bundler', '~> 0.1.0'
17
- gem 'growl', '~> 1.0.0'
12
+ gem 'guard'
13
+ gem 'guard-rspec'
14
+ gem 'guard-bundler'
18
15
  end
data/Guardfile CHANGED
@@ -6,6 +6,6 @@ end
6
6
  guard 'rspec', :version => 2 do
7
7
  watch('spec/support/shared_mongo_adapter.rb') { 'spec' }
8
8
  watch(%r{^spec/.+_spec\.rb$})
9
- watch(%r{^lib/adapter/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
9
+ watch(%r{^lib/adapter/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
10
10
  watch('spec/spec_helper.rb') { "spec" }
11
11
  end
data/README.md CHANGED
@@ -5,21 +5,22 @@ Mongo adapter for adapter gem.
5
5
  ```ruby
6
6
  require 'adapter/mongo'
7
7
 
8
- client = Mongo::Connection.new.db('adapter')['testing']
8
+ key = BSON::ObjectId.new
9
+ client = Mongo::MongoClient.new.db('adapter')['testing']
9
10
  adapter = Adapter[:mongo].new(client)
10
11
  adapter.clear
11
12
 
12
- adapter.write('foo', 'bar')
13
- puts 'Should be bar: ' + adapter.read('foo').inspect
13
+ adapter.write(key, {'some' => 'thing'})
14
+ puts 'Should be {"some" => "thing"}: ' + adapter.read(key).inspect
14
15
 
15
- adapter.delete('foo')
16
- puts 'Should be nil: ' + adapter.read('foo').inspect
16
+ adapter.delete(key)
17
+ puts 'Should be nil: ' + adapter.read(key).inspect
17
18
 
18
- adapter.write('foo', 'bar')
19
+ adapter.write(key, {'some' => 'thing'})
19
20
  adapter.clear
20
- puts 'Should be nil: ' + adapter.read('foo').inspect
21
+ puts 'Should be nil: ' + adapter.read(key).inspect
21
22
 
22
- puts 'Should be bar: ' + adapter.fetch('foo', 'bar')
23
+ puts 'Should be {"some" => "thing"}: ' + adapter.fetch(key, {'some' => 'thing'}).inspect
23
24
  ```
24
25
 
25
26
  ## Flavors
@@ -32,7 +33,7 @@ require 'adapter/mongo_atomic'
32
33
  key = BSON::ObjectId.new
33
34
  full_doc = {'a' => 'c', 'b' => 'd'}
34
35
  partial_doc = {'a' => 'z'}
35
- client = Mongo::Connection.new.db('adapter')['testing']
36
+ client = Mongo::MongoClient.new.db('adapter')['testing']
36
37
  adapter = Adapter[:mongo].new(client)
37
38
  atomic_adapter = Adapter[:mongo_atomic].new(client)
38
39
 
@@ -17,6 +17,6 @@ Gem::Specification.new do |s|
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
18
  s.require_paths = ["lib"]
19
19
 
20
- s.add_dependency 'adapter', '~> 0.6.1'
21
- s.add_dependency 'mongo', '~> 1.5'
20
+ s.add_dependency 'adapter', '0.7.0'
21
+ s.add_dependency 'mongo', '~> 1.8'
22
22
  end
@@ -7,18 +7,19 @@ $:.unshift(lib_path)
7
7
 
8
8
  require 'adapter/mongo'
9
9
 
10
- client = Mongo::Connection.new.db('adapter')['testing']
10
+ key = BSON::ObjectId.new
11
+ client = Mongo::MongoClient.new.db('adapter')['testing']
11
12
  adapter = Adapter[:mongo].new(client)
12
13
  adapter.clear
13
14
 
14
- adapter.write('foo', 'bar')
15
- puts 'Should be bar: ' + adapter.read('foo').inspect
15
+ adapter.write(key, {'some' => 'thing'})
16
+ puts 'Should be {"some" => "thing"}: ' + adapter.read(key).inspect
16
17
 
17
- adapter.delete('foo')
18
- puts 'Should be nil: ' + adapter.read('foo').inspect
18
+ adapter.delete(key)
19
+ puts 'Should be nil: ' + adapter.read(key).inspect
19
20
 
20
- adapter.write('foo', 'bar')
21
+ adapter.write(key, {'some' => 'thing'})
21
22
  adapter.clear
22
- puts 'Should be nil: ' + adapter.read('foo').inspect
23
+ puts 'Should be nil: ' + adapter.read(key).inspect
23
24
 
24
- puts 'Should be bar: ' + adapter.fetch('foo', 'bar')
25
+ puts 'Should be {"some" => "thing"}: ' + adapter.fetch(key, {'some' => 'thing'}).inspect
@@ -10,7 +10,7 @@ require 'adapter/mongo_atomic'
10
10
  key = BSON::ObjectId.new
11
11
  full_doc = {'a' => 'c', 'b' => 'd'}
12
12
  partial_doc = {'a' => 'z'}
13
- client = Mongo::Connection.new.db('adapter')['testing']
13
+ client = Mongo::MongoClient.new.db('adapter')['testing']
14
14
  adapter = Adapter[:mongo].new(client)
15
15
  atomic_adapter = Adapter[:mongo_atomic].new(client)
16
16
 
@@ -3,14 +3,17 @@ require 'mongo'
3
3
 
4
4
  module Adapter
5
5
  module Mongo
6
- def read(key)
7
- if doc = client.find_one('_id' => key_for(key))
8
- decode(doc)
6
+
7
+ # Public
8
+ def read(key, options = nil)
9
+ if doc = client.find_one('_id' => key)
10
+ clean(doc)
9
11
  end
10
12
  end
11
13
 
12
- def read_multiple(*keys)
13
- ids = keys.map { |key| key_for(key) }
14
+ # Public
15
+ def read_multiple(keys, options = nil)
16
+ ids = keys.map { |key| key }
14
17
  docs = client.find('_id' => {'$in' => ids}).to_a
15
18
  keys_and_values = docs.map { |doc| [doc.delete('_id'), doc] }
16
19
 
@@ -18,27 +21,52 @@ module Adapter
18
21
 
19
22
  result = {}
20
23
  keys.each do |key|
21
- key = key_for(key)
24
+ key = key
22
25
  result[key] = docs_by_id[key]
23
26
  end
24
27
  result
25
28
  end
26
29
 
27
- def write(key, value)
28
- client.save({'_id' => key_for(key)}.merge(encode(value)), {:safe => options[:safe]})
30
+ # Public
31
+ def write(key, attributes, options = nil)
32
+ options = operation_options(options)
33
+ client.save(attributes.merge('_id' => key), options)
34
+ end
35
+
36
+ # Public
37
+ def delete(key, options = nil)
38
+ options = operation_options(options)
39
+ client.remove({:_id => key}, options)
29
40
  end
30
41
 
31
- def delete(key)
32
- read(key).tap { client.remove({'_id' => key_for(key)}, {:safe => options[:safe]}) }
42
+ # Public
43
+ def clear(options = nil)
44
+ options = operation_options(options)
45
+ client.remove({}, options)
33
46
  end
34
47
 
35
- def clear
36
- client.remove
48
+ # Private
49
+ def clean(doc)
50
+ doc.delete('_id')
51
+ doc
37
52
  end
38
53
 
39
- def decode(value)
40
- value.delete('_id')
41
- value
54
+ # Private
55
+ def operation_options(options)
56
+ write_concern.merge(options || {})
57
+ end
58
+
59
+ # Private
60
+ def write_concern
61
+ if options[:write_concern]
62
+ options[:write_concern]
63
+ else
64
+ if options[:safe]
65
+ {:w => 1}
66
+ else
67
+ {:w => 0}
68
+ end
69
+ end
42
70
  end
43
71
  end
44
72
  end
@@ -1,5 +1,5 @@
1
1
  module Adapter
2
2
  module Mongo
3
- VERSION = "0.7.0"
3
+ VERSION = "0.8.0"
4
4
  end
5
5
  end
@@ -1,9 +1,11 @@
1
1
  require 'adapter/mongo'
2
2
 
3
3
  Adapter.define(:mongo_atomic, Adapter::Mongo) do
4
- def write(key, value)
5
- criteria = {:_id => key_for(key)}
6
- updates = {'$set' => encode(value)}
7
- client.update(criteria, updates, :upsert => true, :safe => options[:safe])
4
+ # Public
5
+ def write(key, attributes, options = nil)
6
+ criteria = {:_id => key}
7
+ updates = {'$set' => attributes}
8
+ options = operation_options(options).merge(:upsert => true)
9
+ client.update(criteria, updates, options)
8
10
  end
9
11
  end
@@ -2,7 +2,7 @@ require 'helper'
2
2
 
3
3
  describe "Mongo atomic adapter" do
4
4
  before do
5
- @client = Mongo::Connection.new.db('test')['test']
5
+ @client = Mongo::MongoClient.new.db('test')['test']
6
6
  @adapter = Adapter[adapter_name].new(@client)
7
7
  @adapter.clear
8
8
  end
@@ -2,7 +2,7 @@ require 'helper'
2
2
 
3
3
  describe "Mongo adapter" do
4
4
  before do
5
- @client = Mongo::Connection.new.db('test')['test']
5
+ @client = Mongo::MongoClient.new.db('test')['test']
6
6
  @adapter = Adapter[adapter_name].new(@client)
7
7
  @adapter.clear
8
8
  end
@@ -31,9 +31,61 @@ shared_examples_for "a mongo adapter" do
31
31
  end
32
32
 
33
33
  describe "with safe option" do
34
+ context "set to true" do
35
+ before do
36
+ client.ensure_index([['email', 1]], :unique => true)
37
+ @adapter = Adapter[adapter_name].new(client, :safe => true)
38
+ end
39
+
40
+ after do
41
+ client.drop_index('email_1')
42
+ end
43
+
44
+ it "does not raise operation failure on write if operation succeeds" do
45
+ adapter.write(BSON::ObjectId.new, {'email' => 'john@orderedlist.com'})
46
+ lambda {
47
+ adapter.write(BSON::ObjectId.new, {'email' => 'steve@orderedlist.com'})
48
+ }.should_not raise_error(Mongo::OperationFailure)
49
+ end
50
+
51
+ it "raises operation failure on write if operation fails" do
52
+ adapter.write(BSON::ObjectId.new, {'email' => 'john@orderedlist.com'})
53
+ lambda {
54
+ adapter.write(BSON::ObjectId.new, {'email' => 'john@orderedlist.com'})
55
+ }.should raise_error(Mongo::OperationFailure)
56
+ end
57
+ end
58
+
59
+ context "set to false" do
60
+ before do
61
+ client.ensure_index([['email', 1]], :unique => true)
62
+ @adapter = Adapter[adapter_name].new(client, :safe => false)
63
+ end
64
+
65
+ after do
66
+ client.drop_index('email_1')
67
+ end
68
+
69
+ it "does not raise operation failure on write if operation succeeds" do
70
+ adapter.write(BSON::ObjectId.new, {'email' => 'john@orderedlist.com'})
71
+ lambda {
72
+ adapter.write(BSON::ObjectId.new, {'email' => 'steve@orderedlist.com'})
73
+ }.should_not raise_error(Mongo::OperationFailure)
74
+ end
75
+
76
+ it "does not raise operation failure on write if operation fails" do
77
+ adapter.write(BSON::ObjectId.new, {'email' => 'john@orderedlist.com'})
78
+ lambda {
79
+ adapter.write(BSON::ObjectId.new, {'email' => 'john@orderedlist.com'})
80
+ }.should_not raise_error(Mongo::OperationFailure)
81
+ end
82
+ end
83
+ end
84
+
85
+ describe "with :write_concern" do
34
86
  before do
35
87
  client.ensure_index([['email', 1]], :unique => true)
36
- @adapter = Adapter[adapter_name].new(client, :safe => true)
88
+ @adapter = Adapter[adapter_name].new(client, :write_concern => {:w => 1})
37
89
  end
38
90
 
39
91
  after do
@@ -53,5 +105,40 @@ shared_examples_for "a mongo adapter" do
53
105
  adapter.write(BSON::ObjectId.new, {'email' => 'john@orderedlist.com'})
54
106
  }.should raise_error(Mongo::OperationFailure)
55
107
  end
108
+
109
+ it "allows overriding write concern for write" do
110
+ id = BSON::ObjectId.new
111
+ client.should_receive(:update).
112
+ with(
113
+ hash_including(:_id),
114
+ kind_of(Hash),
115
+ hash_including(:w => 0)
116
+ )
117
+ adapter.write(id, {:foo => 'bar'}, :w => 0)
118
+ end
119
+
120
+ it "uses write concern for delete" do
121
+ id = BSON::ObjectId.new
122
+ client.should_receive(:remove).with({:_id => id}, :w => 1)
123
+ adapter.delete(id)
124
+ end
125
+
126
+ it "allows overriding write concern for delete" do
127
+ id = BSON::ObjectId.new
128
+ client.should_receive(:remove).with({:_id => id}, :w => 0)
129
+ adapter.delete(id, :w => 0)
130
+ end
131
+
132
+ it "uses write concern for clear" do
133
+ id = BSON::ObjectId.new
134
+ client.should_receive(:remove).with({}, :w => 1)
135
+ adapter.clear
136
+ end
137
+
138
+ it "allows overriding write concern for clear" do
139
+ id = BSON::ObjectId.new
140
+ client.should_receive(:remove).with({}, :w => 0)
141
+ adapter.clear(:w => 0)
142
+ end
56
143
  end
57
144
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adapter-mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-07 00:00:00.000000000 Z
12
+ date: 2012-12-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: adapter
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ~>
19
+ - - '='
20
20
  - !ruby/object:Gem::Version
21
- version: 0.6.1
21
+ version: 0.7.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ~>
27
+ - - '='
28
28
  - !ruby/object:Gem::Version
29
- version: 0.6.1
29
+ version: 0.7.0
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: mongo
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -34,7 +34,7 @@ dependencies:
34
34
  requirements:
35
35
  - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: '1.5'
37
+ version: '1.8'
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: '1.5'
45
+ version: '1.8'
46
46
  description: Adapter for mongo
47
47
  email:
48
48
  - nunemaker@gmail.com
@@ -84,7 +84,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
84
  version: '0'
85
85
  segments:
86
86
  - 0
87
- hash: 1570202844735750411
87
+ hash: -2103584337757372266
88
88
  required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  none: false
90
90
  requirements:
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  version: '0'
94
94
  segments:
95
95
  - 0
96
- hash: 1570202844735750411
96
+ hash: -2103584337757372266
97
97
  requirements: []
98
98
  rubyforge_project:
99
99
  rubygems_version: 1.8.23