adapter-mongo 0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in adapter-mongo.gemspec
4
+ gemspec
5
+
6
+ group(:development) do
7
+ gem 'rspec', '~> 2.3'
8
+ gem 'log_buddy', '~> 0.5.0'
9
+ gem 'timecop', '~> 0.3.5'
10
+ gem 'i18n', '0.5.0'
11
+ gem 'activesupport', '~> 3', :require => 'active_support'
12
+ gem 'bson_ext', '~> 1.2.0'
13
+
14
+ gem 'ruby-debug', :platform => :ruby_18
15
+ gem 'ruby-debug19', :platform => :ruby_19, :require => 'ruby-debug'
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,64 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ adapter-mongo (0.5)
5
+ adapter (~> 0.5.1)
6
+ mongo (~> 1.2.0)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activesupport (3.0.3)
12
+ adapter (0.5.2)
13
+ archive-tar-minitar (0.5.2)
14
+ bson (1.2.0)
15
+ bson_ext (1.2.0)
16
+ columnize (0.3.2)
17
+ diff-lcs (1.1.2)
18
+ i18n (0.5.0)
19
+ linecache (0.43)
20
+ linecache19 (0.5.11)
21
+ ruby_core_source (>= 0.1.4)
22
+ log_buddy (0.5.0)
23
+ mongo (1.2.0)
24
+ bson (>= 1.2.0)
25
+ rspec (2.4.0)
26
+ rspec-core (~> 2.4.0)
27
+ rspec-expectations (~> 2.4.0)
28
+ rspec-mocks (~> 2.4.0)
29
+ rspec-core (2.4.0)
30
+ rspec-expectations (2.4.0)
31
+ diff-lcs (~> 1.1.2)
32
+ rspec-mocks (2.4.0)
33
+ ruby-debug (0.10.4)
34
+ columnize (>= 0.1)
35
+ ruby-debug-base (~> 0.10.4.0)
36
+ ruby-debug-base (0.10.4)
37
+ linecache (>= 0.3)
38
+ ruby-debug-base19 (0.11.24)
39
+ columnize (>= 0.3.1)
40
+ linecache19 (>= 0.5.11)
41
+ ruby_core_source (>= 0.1.4)
42
+ ruby-debug19 (0.11.6)
43
+ columnize (>= 0.3.1)
44
+ linecache19 (>= 0.5.11)
45
+ ruby-debug-base19 (>= 0.11.19)
46
+ ruby_core_source (0.1.4)
47
+ archive-tar-minitar (>= 0.5.2)
48
+ timecop (0.3.5)
49
+
50
+ PLATFORMS
51
+ ruby
52
+
53
+ DEPENDENCIES
54
+ activesupport (~> 3)
55
+ adapter (~> 0.5.1)
56
+ adapter-mongo!
57
+ bson_ext (~> 1.2.0)
58
+ i18n (= 0.5.0)
59
+ log_buddy (~> 0.5.0)
60
+ mongo (~> 1.2.0)
61
+ rspec (~> 2.3)
62
+ ruby-debug
63
+ ruby-debug19
64
+ timecop (~> 0.3.5)
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 John Nunemaker
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,31 @@
1
+ = adapter-mongo
2
+
3
+ Mongo adapter for adapter gem.
4
+
5
+ require 'adapter/mongo'
6
+
7
+ client = Mongo::Connection.new.db('adapter')['testing']
8
+ adapter = Adapter[:mongo].new(client)
9
+ adapter.clear
10
+
11
+ adapter.write('foo', 'bar')
12
+ puts 'Should be bar: ' + adapter.read('foo').inspect
13
+
14
+ adapter.delete('foo')
15
+ puts 'Should be nil: ' + adapter.read('foo').inspect
16
+
17
+ adapter.write('foo', 'bar')
18
+ adapter.clear
19
+ puts 'Should be nil: ' + adapter.read('foo').inspect
20
+
21
+ puts 'Should be bar: ' + adapter.fetch('foo', 'bar')
22
+
23
+ See examples/ or specs/ for more usage.
24
+
25
+ == Note on Patches/Pull Requests
26
+
27
+ * Fork the project.
28
+ * Make your feature addition or bug fix.
29
+ * Add tests for it. This is important so we don't break it in a future version unintentionally.
30
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine, but bump version in a commit by itself so we can ignore when we pull)
31
+ * Send us a pull request. Bonus points for topic branches.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "adapter/mongo/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "adapter-mongo"
7
+ s.version = Adapter::Mongo::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["John Nunemaker"]
10
+ s.email = ["nunemaker@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Adapter for mongo}
13
+ s.description = %q{Adapter for mongo}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency 'adapter', '~> 0.5.1'
21
+ s.add_dependency 'mongo', '~> 1.2.0'
22
+ end
data/examples/mongo.rb ADDED
@@ -0,0 +1,24 @@
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
+ client = Mongo::Connection.new.db('adapter')['testing']
11
+ adapter = Adapter[:mongo].new(client)
12
+ adapter.clear
13
+
14
+ adapter.write('foo', 'bar')
15
+ puts 'Should be bar: ' + adapter.read('foo').inspect
16
+
17
+ adapter.delete('foo')
18
+ puts 'Should be nil: ' + adapter.read('foo').inspect
19
+
20
+ adapter.write('foo', 'bar')
21
+ adapter.clear
22
+ puts 'Should be nil: ' + adapter.read('foo').inspect
23
+
24
+ puts 'Should be bar: ' + adapter.fetch('foo', 'bar')
@@ -0,0 +1,41 @@
1
+ require 'adapter'
2
+ require 'mongo'
3
+
4
+ module Adapter
5
+ module Mongo
6
+ NonHashValueKeyName = '_value'
7
+
8
+ def read(key)
9
+ if doc = client.find_one('_id' => key_for(key))
10
+ decode(doc)
11
+ end
12
+ end
13
+
14
+ def write(key, value)
15
+ client.save({'_id' => key_for(key)}.merge(encode(value)))
16
+ end
17
+
18
+ def delete(key)
19
+ read(key).tap { client.remove('_id' => key_for(key)) }
20
+ end
21
+
22
+ def clear
23
+ client.remove
24
+ end
25
+
26
+ def key_for(key)
27
+ key.is_a?(BSON::ObjectId) ? key : super
28
+ end
29
+
30
+ def encode(value)
31
+ value.is_a?(Hash) ? value : {NonHashValueKeyName => value}
32
+ end
33
+
34
+ def decode(value)
35
+ return if value.nil?
36
+ value.key?(NonHashValueKeyName) ? value[NonHashValueKeyName] : value
37
+ end
38
+ end
39
+ end
40
+
41
+ Adapter.define(:mongo, Adapter::Mongo)
@@ -0,0 +1,5 @@
1
+ module Adapter
2
+ module Mongo
3
+ VERSION = "0.5"
4
+ end
5
+ end
data/log/test.log ADDED
@@ -0,0 +1,101 @@
1
+ # Logfile created on Thu Feb 03 09:08:05 -0500 2011 by logger.rb
2
+ D, [2011-02-03T09:10:58.204661 #18222] DEBUG -- : key = "key"
3
+
4
+ D, [2011-02-03T09:10:58.209325 #18222] DEBUG -- : key = "key"
5
+
6
+ D, [2011-02-03T09:10:58.211837 #18222] DEBUG -- : key = "key"
7
+
8
+ D, [2011-02-03T09:10:58.214308 #18222] DEBUG -- : key = "key"
9
+
10
+ D, [2011-02-03T09:10:58.216590 #18222] DEBUG -- : key = "key"
11
+
12
+ D, [2011-02-03T09:10:58.218848 #18222] DEBUG -- : key = "key"
13
+
14
+ D, [2011-02-03T09:10:58.221382 #18222] DEBUG -- : key = "key"
15
+
16
+ D, [2011-02-03T09:10:58.224140 #18222] DEBUG -- : key = "key"
17
+
18
+ D, [2011-02-03T09:10:58.226556 #18222] DEBUG -- : key = "key"
19
+
20
+ D, [2011-02-03T09:10:58.229142 #18222] DEBUG -- : key = "key"
21
+
22
+ D, [2011-02-03T09:10:58.231546 #18222] DEBUG -- : key = "key"
23
+
24
+ D, [2011-02-03T09:10:58.233860 #18222] DEBUG -- : key = "key"
25
+
26
+ D, [2011-02-03T09:10:58.236431 #18222] DEBUG -- : key = "key"
27
+
28
+ D, [2011-02-03T09:10:58.242707 #18222] DEBUG -- : key = "key"
29
+
30
+ D, [2011-02-03T09:10:58.245981 #18222] DEBUG -- : key = "key"
31
+
32
+ D, [2011-02-03T09:10:58.249845 #18222] DEBUG -- : key = #<struct #<Class:0x10108ca80> foo=:bar>
33
+
34
+ D, [2011-02-03T09:10:58.253667 #18222] DEBUG -- : key = #<struct #<Class:0x101084a60> foo=:bar>
35
+
36
+ D, [2011-02-03T09:10:58.263219 #18222] DEBUG -- : key = #<struct #<Class:0x101070ee8> foo=:bar>
37
+
38
+ D, [2011-02-03T09:10:58.266937 #18222] DEBUG -- : key = #<struct #<Class:0x101068ba8> foo=:bar>
39
+
40
+ D, [2011-02-03T09:11:55.657172 #18355] DEBUG -- : key = "key"
41
+
42
+ D, [2011-02-03T09:11:55.657825 #18355] DEBUG -- : doc = {"_id"=>"key", "value"=>{"foo"=>:bar}}
43
+
44
+ D, [2011-02-03T09:11:55.663921 #18355] DEBUG -- : key = "key"
45
+
46
+ D, [2011-02-03T09:11:55.666827 #18355] DEBUG -- : key = "key"
47
+
48
+ D, [2011-02-03T09:11:55.667326 #18355] DEBUG -- : doc = {"_id"=>"key", "value"=>"value"}
49
+
50
+ D, [2011-02-03T09:11:55.670437 #18355] DEBUG -- : key = "key"
51
+
52
+ D, [2011-02-03T09:11:55.671005 #18355] DEBUG -- : doc = {"_id"=>"key", "value"=>"value"}
53
+
54
+ D, [2011-02-03T09:11:55.673365 #18355] DEBUG -- : key = "key"
55
+
56
+ D, [2011-02-03T09:11:55.673866 #18355] DEBUG -- : doc = {"_id"=>"key", "value"=>{"foo"=>:bar}}
57
+
58
+ D, [2011-02-03T09:11:55.676317 #18355] DEBUG -- : key = "key"
59
+
60
+ D, [2011-02-03T09:11:55.679298 #18355] DEBUG -- : key = "key"
61
+
62
+ D, [2011-02-03T09:11:55.679806 #18355] DEBUG -- : doc = {"_id"=>"key", "value"=>"value"}
63
+
64
+ D, [2011-02-03T09:11:55.682394 #18355] DEBUG -- : key = "key"
65
+
66
+ D, [2011-02-03T09:11:55.682914 #18355] DEBUG -- : doc = {"_id"=>"key", "value"=>"value"}
67
+
68
+ D, [2011-02-03T09:11:55.685868 #18355] DEBUG -- : key = "key"
69
+
70
+ D, [2011-02-03T09:11:55.689148 #18355] DEBUG -- : key = "key"
71
+
72
+ D, [2011-02-03T09:11:55.690046 #18355] DEBUG -- : key = "key2"
73
+
74
+ D, [2011-02-03T09:11:55.693727 #18355] DEBUG -- : key = "key"
75
+
76
+ D, [2011-02-03T09:11:55.694439 #18355] DEBUG -- : key = "key"
77
+
78
+ D, [2011-02-03T09:11:55.697517 #18355] DEBUG -- : key = "key"
79
+
80
+ D, [2011-02-03T09:11:55.698093 #18355] DEBUG -- : key = "key"
81
+
82
+ D, [2011-02-03T09:11:55.700749 #18355] DEBUG -- : key = "key"
83
+
84
+ D, [2011-02-03T09:11:55.701243 #18355] DEBUG -- : doc = {"_id"=>"key", "value"=>"value"}
85
+
86
+ D, [2011-02-03T09:11:55.703567 #18355] DEBUG -- : key = "key"
87
+
88
+ D, [2011-02-03T09:11:55.704074 #18355] DEBUG -- : doc = {"_id"=>"key", "value"=>"value2"}
89
+
90
+ D, [2011-02-03T09:11:55.708152 #18355] DEBUG -- : key = "key"
91
+
92
+ D, [2011-02-03T09:11:55.708842 #18355] DEBUG -- : doc = {"_id"=>"key", "value"=>"value"}
93
+
94
+ D, [2011-02-03T09:11:55.712637 #18355] DEBUG -- : key = #<struct #<Class:0x10103a5a0> foo=:bar>
95
+
96
+ D, [2011-02-03T09:11:55.716432 #18355] DEBUG -- : key = #<struct #<Class:0x101032f58> foo=:bar>
97
+
98
+ D, [2011-02-03T09:11:55.725740 #18355] DEBUG -- : key = #<struct #<Class:0x10101dec8> foo=:bar>
99
+
100
+ D, [2011-02-03T09:11:55.729651 #18355] DEBUG -- : key = #<struct #<Class:0x1010162b8> foo=:bar>
101
+
data/spec/helper.rb ADDED
@@ -0,0 +1,36 @@
1
+ $:.unshift(File.expand_path('../../lib', __FILE__))
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+
6
+ Bundler.require(:default, :development)
7
+
8
+ require 'pathname'
9
+ require 'logger'
10
+
11
+ root_path = Pathname(__FILE__).dirname.join('..').expand_path
12
+ lib_path = root_path.join('lib')
13
+ log_path = root_path.join('log')
14
+ log_path.mkpath
15
+
16
+ require 'adapter/spec/an_adapter'
17
+ require 'adapter/spec/types'
18
+
19
+ shared_examples_for "a mongo adapter" do
20
+ it_should_behave_like 'an adapter'
21
+
22
+ Adapter::Spec::Types.each do |type, (key, key2)|
23
+ it "writes Object values to keys that are #{type}s like a Hash" do
24
+ adapter[key] = {:foo => :bar}
25
+ # mongo knows hashes and can serialize symbol values
26
+ adapter[key].should == {'_id' => 'key', 'foo' => :bar}
27
+ end
28
+ end
29
+ end
30
+
31
+ logger = Logger.new(log_path.join('test.log'))
32
+ LogBuddy.init(:logger => logger)
33
+
34
+ Rspec.configure do |c|
35
+
36
+ end
@@ -0,0 +1,27 @@
1
+ require 'helper'
2
+ require 'adapter/mongo'
3
+
4
+ describe "Mongo adapter" do
5
+ before do
6
+ @client = Mongo::Connection.new.db('adapter')['testing']
7
+ @adapter = Adapter[:mongo].new(@client)
8
+ @adapter.clear
9
+ end
10
+
11
+ let(:adapter) { @adapter }
12
+ let(:client) { @client }
13
+
14
+ it_should_behave_like 'a mongo adapter'
15
+
16
+ it "allows using object id's as keys in correct type" do
17
+ id = BSON::ObjectId.new
18
+ adapter.write(id, 'ham')
19
+ client.find_one('_id' => id).should_not be_nil
20
+ adapter.read(id).should == 'ham'
21
+ end
22
+
23
+ it "stores hashes right in document" do
24
+ adapter.write('foo', 'steak' => 'bacon')
25
+ client.find_one('_id' => 'foo').should == {'_id' => 'foo', 'steak' => 'bacon'}
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adapter-mongo
3
+ version: !ruby/object:Gem::Version
4
+ hash: 1
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 5
9
+ version: "0.5"
10
+ platform: ruby
11
+ authors:
12
+ - John Nunemaker
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-03 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: adapter
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 9
29
+ segments:
30
+ - 0
31
+ - 5
32
+ - 1
33
+ version: 0.5.1
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: mongo
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 31
45
+ segments:
46
+ - 1
47
+ - 2
48
+ - 0
49
+ version: 1.2.0
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ description: Adapter for mongo
53
+ email:
54
+ - nunemaker@gmail.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files: []
60
+
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE
66
+ - README.rdoc
67
+ - Rakefile
68
+ - adapter-mongo.gemspec
69
+ - examples/mongo.rb
70
+ - lib/adapter/mongo.rb
71
+ - lib/adapter/mongo/version.rb
72
+ - log/test.log
73
+ - spec/helper.rb
74
+ - spec/mongo_spec.rb
75
+ has_rdoc: true
76
+ homepage: ""
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options: []
81
+
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ requirements: []
103
+
104
+ rubyforge_project:
105
+ rubygems_version: 1.3.7
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Adapter for mongo
109
+ test_files:
110
+ - spec/helper.rb
111
+ - spec/mongo_spec.rb