adapter-mongo 0.5.5 → 0.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Guardfile +1 -0
- data/README.md +36 -0
- data/examples/mongo_atomic.rb +36 -0
- data/examples/object_for_key.rb +19 -0
- data/lib/adapter/mongo.rb +7 -2
- data/lib/adapter/mongo/version.rb +1 -1
- data/spec/helper.rb +2 -48
- data/spec/support/shared_mongo_adapter.rb +62 -0
- metadata +45 -61
data/Guardfile
CHANGED
data/README.md
CHANGED
@@ -22,6 +22,42 @@ puts 'Should be nil: ' + adapter.read('foo').inspect
|
|
22
22
|
puts 'Should be bar: ' + adapter.fetch('foo', 'bar')
|
23
23
|
```
|
24
24
|
|
25
|
+
## Flavors
|
26
|
+
|
27
|
+
There are two adapters included with this gem -- `:mongo` and `:mongo_atomic`. `:mongo` assumes that you are writing the full document each time. `:mongo_atomic` allows for partially updating documents. The difference is best shown with a bit of code.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'adapter/mongo_atomic'
|
31
|
+
|
32
|
+
key = BSON::ObjectId.new
|
33
|
+
full_doc = {'a' => 'c', 'b' => 'd'}
|
34
|
+
partial_doc = {'a' => 'z'}
|
35
|
+
client = Mongo::Connection.new.db('adapter')['testing']
|
36
|
+
adapter = Adapter[:mongo].new(client)
|
37
|
+
atomic_adapter = Adapter[:mongo_atomic].new(client)
|
38
|
+
|
39
|
+
adapter.clear
|
40
|
+
atomic_adapter.clear
|
41
|
+
|
42
|
+
adapter.write(key, full_doc)
|
43
|
+
adapter.write(key, partial_doc)
|
44
|
+
|
45
|
+
doc = adapter.read(key)
|
46
|
+
doc.delete('_id')
|
47
|
+
|
48
|
+
# full doc must always be written with :mongo adapter
|
49
|
+
puts 'Should be {"a"=>"z"}: ' + doc.inspect
|
50
|
+
|
51
|
+
atomic_adapter.write(key, full_doc)
|
52
|
+
atomic_adapter.write(key, partial_doc)
|
53
|
+
|
54
|
+
doc = atomic_adapter.read(key)
|
55
|
+
doc.delete('_id')
|
56
|
+
|
57
|
+
# partial updates can be written with atomic adapter as $set is used
|
58
|
+
puts 'Should be {"a"=>"z", "b"=>"d"}: ' + doc.inspect
|
59
|
+
```
|
60
|
+
|
25
61
|
See examples/ or specs/ for more usage.
|
26
62
|
|
27
63
|
## Contributing
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
root_path = Pathname(__FILE__).dirname.join('..').expand_path
|
5
|
+
lib_path = root_path.join('lib')
|
6
|
+
$:.unshift(lib_path)
|
7
|
+
|
8
|
+
require 'adapter/mongo_atomic'
|
9
|
+
|
10
|
+
key = BSON::ObjectId.new
|
11
|
+
full_doc = {'a' => 'c', 'b' => 'd'}
|
12
|
+
partial_doc = {'a' => 'z'}
|
13
|
+
client = Mongo::Connection.new.db('adapter')['testing']
|
14
|
+
adapter = Adapter[:mongo].new(client)
|
15
|
+
atomic_adapter = Adapter[:mongo_atomic].new(client)
|
16
|
+
|
17
|
+
adapter.clear
|
18
|
+
atomic_adapter.clear
|
19
|
+
|
20
|
+
adapter.write(key, full_doc)
|
21
|
+
adapter.write(key, partial_doc)
|
22
|
+
|
23
|
+
doc = adapter.read(key)
|
24
|
+
doc.delete('_id')
|
25
|
+
|
26
|
+
# full doc must always be written with :mongo adapter
|
27
|
+
puts 'Should be {"a"=>"z"}: ' + doc.inspect
|
28
|
+
|
29
|
+
atomic_adapter.write(key, full_doc)
|
30
|
+
atomic_adapter.write(key, partial_doc)
|
31
|
+
|
32
|
+
doc = atomic_adapter.read(key)
|
33
|
+
doc.delete('_id')
|
34
|
+
|
35
|
+
# partial updates can be written with atomic adapter as $set is used
|
36
|
+
puts 'Should be {"a"=>"z", "b"=>"d"}: ' + doc.inspect
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
root_path = Pathname(__FILE__).dirname.join('..').expand_path
|
5
|
+
lib_path = root_path.join('lib')
|
6
|
+
$:.unshift(lib_path)
|
7
|
+
|
8
|
+
require 'adapter/mongo'
|
9
|
+
|
10
|
+
key = BSON::OrderedHash['s' => 1, 'n' => 1]
|
11
|
+
client = Mongo::Connection.new.db('adapter')['testing']
|
12
|
+
adapter = Adapter[:mongo].new(client)
|
13
|
+
adapter.clear
|
14
|
+
|
15
|
+
adapter.write(key, :v => 1)
|
16
|
+
|
17
|
+
doc = adapter.read(key)
|
18
|
+
|
19
|
+
puts doc.inspect
|
data/lib/adapter/mongo.rb
CHANGED
@@ -24,7 +24,12 @@ module Adapter
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def key_for(key)
|
27
|
-
|
27
|
+
case key
|
28
|
+
when BSON::ObjectId, Hash
|
29
|
+
key
|
30
|
+
else
|
31
|
+
super
|
32
|
+
end
|
28
33
|
end
|
29
34
|
|
30
35
|
def encode(value)
|
@@ -38,4 +43,4 @@ module Adapter
|
|
38
43
|
end
|
39
44
|
end
|
40
45
|
|
41
|
-
Adapter.define(:mongo, Adapter::Mongo)
|
46
|
+
Adapter.define(:mongo, Adapter::Mongo)
|
data/spec/helper.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
$:.unshift(File.expand_path('../../lib', __FILE__))
|
2
|
+
$:.unshift(File.expand_path('../', __FILE__))
|
2
3
|
|
3
4
|
require 'rubygems'
|
4
5
|
require 'bundler'
|
@@ -9,54 +10,7 @@ require 'adapter/spec/an_adapter'
|
|
9
10
|
require 'adapter/spec/types'
|
10
11
|
require 'adapter-mongo'
|
11
12
|
|
12
|
-
|
13
|
-
it_should_behave_like 'an adapter'
|
14
|
-
|
15
|
-
Adapter::Spec::Types.each do |type, (key, key2)|
|
16
|
-
it "writes Object values to keys that are #{type}s like a Hash" do
|
17
|
-
adapter[key] = {:foo => :bar}
|
18
|
-
# mongo knows hashes and can serialize symbol values
|
19
|
-
adapter[key].should == {'_id' => 'key', 'foo' => :bar}
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
it "allows using object id's as keys in correct type" do
|
24
|
-
id = BSON::ObjectId.new
|
25
|
-
adapter.write(id, 'ham')
|
26
|
-
client.find_one('_id' => id).should_not be_nil
|
27
|
-
adapter.read(id).should == 'ham'
|
28
|
-
end
|
29
|
-
|
30
|
-
it "stores hashes right in document" do
|
31
|
-
adapter.write('foo', 'steak' => 'bacon')
|
32
|
-
client.find_one('_id' => 'foo').should == {'_id' => 'foo', 'steak' => 'bacon'}
|
33
|
-
end
|
34
|
-
|
35
|
-
describe "with safe option" do
|
36
|
-
before do
|
37
|
-
client.ensure_index([['email', 1]], :unique => true)
|
38
|
-
@adapter = Adapter[adapter_name].new(client, :safe => true)
|
39
|
-
end
|
40
|
-
|
41
|
-
after do
|
42
|
-
client.drop_index('email_1')
|
43
|
-
end
|
44
|
-
|
45
|
-
it "does not raise operation failure on write if operation succeeds" do
|
46
|
-
adapter.write(BSON::ObjectId.new, {'email' => 'john@orderedlist.com'})
|
47
|
-
lambda {
|
48
|
-
adapter.write(BSON::ObjectId.new, {'email' => 'steve@orderedlist.com'})
|
49
|
-
}.should_not raise_error(Mongo::OperationFailure)
|
50
|
-
end
|
51
|
-
|
52
|
-
it "raises operation failure on write if operation fails" do
|
53
|
-
adapter.write(BSON::ObjectId.new, {'email' => 'john@orderedlist.com'})
|
54
|
-
lambda {
|
55
|
-
adapter.write(BSON::ObjectId.new, {'email' => 'john@orderedlist.com'})
|
56
|
-
}.should raise_error(Mongo::OperationFailure)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
13
|
+
require 'support/shared_mongo_adapter'
|
60
14
|
|
61
15
|
RSpec.configure do |c|
|
62
16
|
|
@@ -0,0 +1,62 @@
|
|
1
|
+
shared_examples_for "a mongo adapter" do
|
2
|
+
it_should_behave_like 'an adapter'
|
3
|
+
|
4
|
+
Adapter::Spec::Types.each do |type, (key, key2)|
|
5
|
+
it "writes Object values to keys that are #{type}s like a Hash" do
|
6
|
+
adapter[key] = {:foo => :bar}
|
7
|
+
# mongo knows hashes and can serialize symbol values
|
8
|
+
adapter[key].should == {'_id' => 'key', 'foo' => :bar}
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
it "allows using object id's as keys in correct type" do
|
13
|
+
id = BSON::ObjectId.new
|
14
|
+
adapter.write(id, 'ham')
|
15
|
+
client.find_one('_id' => id).should_not be_nil
|
16
|
+
adapter.read(id).should == 'ham'
|
17
|
+
end
|
18
|
+
|
19
|
+
it "allows using ordered hashes as keys" do
|
20
|
+
key = BSON::OrderedHash['d', 1, 'n', 1]
|
21
|
+
adapter.write(key, 'ham')
|
22
|
+
client.find_one('_id' => key).should_not be_nil
|
23
|
+
adapter.read(key).should == 'ham'
|
24
|
+
end
|
25
|
+
|
26
|
+
it "allows using hashes as keys" do
|
27
|
+
key = {:d => 1}
|
28
|
+
adapter.write(key, 'ham')
|
29
|
+
client.find_one('_id' => key).should_not be_nil
|
30
|
+
adapter.read(key).should == 'ham'
|
31
|
+
end
|
32
|
+
|
33
|
+
it "stores hashes right in document" do
|
34
|
+
adapter.write('foo', 'steak' => 'bacon')
|
35
|
+
client.find_one('_id' => 'foo').should == {'_id' => 'foo', 'steak' => 'bacon'}
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "with safe option" do
|
39
|
+
before do
|
40
|
+
client.ensure_index([['email', 1]], :unique => true)
|
41
|
+
@adapter = Adapter[adapter_name].new(client, :safe => true)
|
42
|
+
end
|
43
|
+
|
44
|
+
after do
|
45
|
+
client.drop_index('email_1')
|
46
|
+
end
|
47
|
+
|
48
|
+
it "does not raise operation failure on write if operation succeeds" do
|
49
|
+
adapter.write(BSON::ObjectId.new, {'email' => 'john@orderedlist.com'})
|
50
|
+
lambda {
|
51
|
+
adapter.write(BSON::ObjectId.new, {'email' => 'steve@orderedlist.com'})
|
52
|
+
}.should_not raise_error(Mongo::OperationFailure)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "raises operation failure on write if operation fails" do
|
56
|
+
adapter.write(BSON::ObjectId.new, {'email' => 'john@orderedlist.com'})
|
57
|
+
lambda {
|
58
|
+
adapter.write(BSON::ObjectId.new, {'email' => 'john@orderedlist.com'})
|
59
|
+
}.should raise_error(Mongo::OperationFailure)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
metadata
CHANGED
@@ -1,62 +1,45 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: adapter-mongo
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.6
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 5
|
9
|
-
- 5
|
10
|
-
version: 0.5.5
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- John Nunemaker
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
prerelease: false
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
12
|
+
date: 2012-07-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: adapter
|
16
|
+
requirement: &70364766280940 !ruby/object:Gem::Requirement
|
24
17
|
none: false
|
25
|
-
requirements:
|
18
|
+
requirements:
|
26
19
|
- - ~>
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
segments:
|
30
|
-
- 0
|
31
|
-
- 5
|
32
|
-
version: "0.5"
|
33
|
-
version_requirements: *id001
|
34
|
-
name: adapter
|
35
|
-
- !ruby/object:Gem::Dependency
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.5'
|
36
22
|
type: :runtime
|
37
23
|
prerelease: false
|
38
|
-
|
24
|
+
version_requirements: *70364766280940
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mongo
|
27
|
+
requirement: &70364766280440 !ruby/object:Gem::Requirement
|
39
28
|
none: false
|
40
|
-
requirements:
|
29
|
+
requirements:
|
41
30
|
- - ~>
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
version: "1.5"
|
48
|
-
version_requirements: *id002
|
49
|
-
name: mongo
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '1.5'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70364766280440
|
50
36
|
description: Adapter for mongo
|
51
|
-
email:
|
37
|
+
email:
|
52
38
|
- nunemaker@gmail.com
|
53
39
|
executables: []
|
54
|
-
|
55
40
|
extensions: []
|
56
|
-
|
57
41
|
extra_rdoc_files: []
|
58
|
-
|
59
|
-
files:
|
42
|
+
files:
|
60
43
|
- .gitignore
|
61
44
|
- .travis.yml
|
62
45
|
- Gemfile
|
@@ -66,6 +49,8 @@ files:
|
|
66
49
|
- Rakefile
|
67
50
|
- adapter-mongo.gemspec
|
68
51
|
- examples/mongo.rb
|
52
|
+
- examples/mongo_atomic.rb
|
53
|
+
- examples/object_for_key.rb
|
69
54
|
- lib/adapter-mongo.rb
|
70
55
|
- lib/adapter/mongo.rb
|
71
56
|
- lib/adapter/mongo/version.rb
|
@@ -73,40 +58,39 @@ files:
|
|
73
58
|
- spec/helper.rb
|
74
59
|
- spec/mongo_atomic_spec.rb
|
75
60
|
- spec/mongo_spec.rb
|
76
|
-
|
61
|
+
- spec/support/shared_mongo_adapter.rb
|
62
|
+
homepage: ''
|
77
63
|
licenses: []
|
78
|
-
|
79
64
|
post_install_message:
|
80
65
|
rdoc_options: []
|
81
|
-
|
82
|
-
require_paths:
|
66
|
+
require_paths:
|
83
67
|
- lib
|
84
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
69
|
none: false
|
86
|
-
requirements:
|
87
|
-
- -
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
|
90
|
-
segments:
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
segments:
|
91
75
|
- 0
|
92
|
-
|
93
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
hash: 3131877323408847219
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
78
|
none: false
|
95
|
-
requirements:
|
96
|
-
- -
|
97
|
-
- !ruby/object:Gem::Version
|
98
|
-
|
99
|
-
segments:
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
segments:
|
100
84
|
- 0
|
101
|
-
|
85
|
+
hash: 3131877323408847219
|
102
86
|
requirements: []
|
103
|
-
|
104
87
|
rubyforge_project:
|
105
88
|
rubygems_version: 1.8.10
|
106
89
|
signing_key:
|
107
90
|
specification_version: 3
|
108
91
|
summary: Adapter for mongo
|
109
|
-
test_files:
|
92
|
+
test_files:
|
110
93
|
- spec/helper.rb
|
111
94
|
- spec/mongo_atomic_spec.rb
|
112
95
|
- spec/mongo_spec.rb
|
96
|
+
- spec/support/shared_mongo_adapter.rb
|