mongo_adaptor 0.0.1
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/.gitignore +17 -0
- data/.travis.yml +16 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +35 -0
- data/Rakefile +10 -0
- data/lib/mongo_adaptor.rb +48 -0
- data/lib/mongo_adaptor/version.rb +3 -0
- data/mongo_adaptor.gemspec +24 -0
- data/spec/mongo_adaptor_spec.rb +100 -0
- metadata +142 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jon Rowe
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
[](http://travis-ci.org/JonRowe/MongoAdaptor)
|
2
|
+
# MongoAdaptor
|
3
|
+
|
4
|
+
A simple mongo handler. Translates Structs into Mongo and back.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'mongo_adaptor'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install mongo_adaptor
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
TODO: Write usage instructions here
|
23
|
+
|
24
|
+
## Caveats
|
25
|
+
|
26
|
+
Tests don't pass on Rubinus in 19 mode. Not sure why (some BSON encoding
|
27
|
+
issue?)
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'mongo'
|
2
|
+
require 'mongo-configure'
|
3
|
+
require "mongo_adaptor/version"
|
4
|
+
|
5
|
+
class MongoAdaptor
|
6
|
+
class << self
|
7
|
+
def db
|
8
|
+
@db ||= Mongo::Configure.current.load
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(name,klass)
|
13
|
+
@collection = self.class.db.collection name.to_s.downcase
|
14
|
+
@klass = klass
|
15
|
+
end
|
16
|
+
|
17
|
+
def insert(model)
|
18
|
+
@collection.insert( process(model), { :safe => true } )
|
19
|
+
end
|
20
|
+
def update(model)
|
21
|
+
@collection.update( { "_id" => model.id }, { "$set" => process(model) }, { :safe => true, :upsert => false } )
|
22
|
+
end
|
23
|
+
|
24
|
+
def fetch(*args)
|
25
|
+
@collection.find_one *args, :transformer => builder
|
26
|
+
end
|
27
|
+
|
28
|
+
def find(*args)
|
29
|
+
@collection.find *args, :transformer => builder
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
def builder
|
34
|
+
proc do |result|
|
35
|
+
@klass.new.tap do |model|
|
36
|
+
model[:id] = result.delete('_id') if model.respond_to?(:id)
|
37
|
+
result.each do |field,value|
|
38
|
+
model[field] = value if model.respond_to? field
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
def process(model)
|
44
|
+
fields = {}
|
45
|
+
model.each_pair { |field,value| fields[field] = value unless field == :id }
|
46
|
+
fields
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/mongo_adaptor/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jon Rowe"]
|
6
|
+
gem.email = ["hello@jonrowe.co.uk"]
|
7
|
+
gem.description = %q{A simple mongo handler. Translates Structs into Mongo and back.}
|
8
|
+
gem.summary = %q{A simple mongo handler. Translates Structs into Mongo and back.}
|
9
|
+
gem.homepage = "https://github.com/JonRowe/MongoAdaptor"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "mongo_adaptor"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = MongoAdaptor::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'mongo'
|
19
|
+
gem.add_dependency 'bson'
|
20
|
+
gem.add_dependency 'mongo-configure'
|
21
|
+
|
22
|
+
gem.add_development_dependency 'rake'
|
23
|
+
gem.add_development_dependency 'rspec'
|
24
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'mongo_adaptor'
|
2
|
+
|
3
|
+
describe 'adapting structs into mongo' do
|
4
|
+
before do
|
5
|
+
Mongo::Configure.from_database 'mongo_adaptor_test'
|
6
|
+
end
|
7
|
+
after do
|
8
|
+
Mongo::Configure.current.load.collections.select { |c| c.name !~ /^system\./ }.each &:remove
|
9
|
+
end
|
10
|
+
|
11
|
+
describe 'db setup' do
|
12
|
+
it 'uses the configured database' do
|
13
|
+
MongoAdaptor.db.name.should == 'mongo_adaptor_test'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'useing the adaptor' do
|
18
|
+
let(:klass) { Struct.new :name, :other, :id }
|
19
|
+
let(:adaptor) { MongoAdaptor.new 'test_collection', klass }
|
20
|
+
let(:collection) { Mongo::Configure.current.load.collection 'test_collection' }
|
21
|
+
|
22
|
+
describe 'to insert a blank model' do
|
23
|
+
let(:model) { klass.new 'Test Model','Some Data','fake key' }
|
24
|
+
let(:data) { collection.find({}).to_a[-1] }
|
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)
|
30
|
+
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
|
35
|
+
end
|
36
|
+
it 'sets my fields and values' do
|
37
|
+
subject
|
38
|
+
data['name'].should == 'Test Model'
|
39
|
+
data['other'].should == 'Some Data'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'with an existing model' do
|
44
|
+
let(:model) { klass.new 'Test Model','Some Data' }
|
45
|
+
let(:id) { collection.insert({ :name => 'My Model', :other => 'Some Value' },{ :safe => true }) }
|
46
|
+
|
47
|
+
before do
|
48
|
+
model.id = id
|
49
|
+
end
|
50
|
+
|
51
|
+
describe 'to update it' do
|
52
|
+
let(:data) { collection.find({}).to_a[-1] }
|
53
|
+
|
54
|
+
subject { adaptor.update model }
|
55
|
+
|
56
|
+
it 'doesnt change the number of items in the collection' do
|
57
|
+
expect { subject }.to change { collection.size }.by(0)
|
58
|
+
end
|
59
|
+
it 'doesnt change the id' do
|
60
|
+
expect { subject }.to_not change { collection.find_one['_id'] }
|
61
|
+
end
|
62
|
+
it 'sets my fields and values' do
|
63
|
+
subject
|
64
|
+
data['_id'].should == model.id
|
65
|
+
data['name'].should == 'Test Model'
|
66
|
+
data['other'].should == 'Some Data'
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe 'to fetch it' do
|
71
|
+
subject { adaptor.fetch({ :_id => id }) }
|
72
|
+
it { should be_a klass }
|
73
|
+
its(:id) { should == id }
|
74
|
+
its(:name) { should == 'My Model' }
|
75
|
+
its(:other) { should == 'Some Value' }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe 'finding multiples' do
|
80
|
+
before do
|
81
|
+
3.times do |i|
|
82
|
+
collection.insert({ :name => 'My Model', :other => i },{ :safe => true })
|
83
|
+
end
|
84
|
+
3.times do |i|
|
85
|
+
collection.insert({ :name => 'Other Model', :other => i },{ :safe => true })
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
subject { adaptor.find({ :name => 'My Model' }) }
|
90
|
+
|
91
|
+
its(:count) { should == 3 }
|
92
|
+
it 'translates all to klass' do
|
93
|
+
subject.all? { |k| k.is_a? klass }.should be_true
|
94
|
+
end
|
95
|
+
it 'gets them all' do
|
96
|
+
subject.map(&:other).should == [0,1,2]
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongo_adaptor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jon Rowe
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongo
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bson
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: mongo-configure
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rspec
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: A simple mongo handler. Translates Structs into Mongo and back.
|
95
|
+
email:
|
96
|
+
- hello@jonrowe.co.uk
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- .travis.yml
|
103
|
+
- Gemfile
|
104
|
+
- LICENSE
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- lib/mongo_adaptor.rb
|
108
|
+
- lib/mongo_adaptor/version.rb
|
109
|
+
- mongo_adaptor.gemspec
|
110
|
+
- spec/mongo_adaptor_spec.rb
|
111
|
+
homepage: https://github.com/JonRowe/MongoAdaptor
|
112
|
+
licenses: []
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
none: false
|
119
|
+
requirements:
|
120
|
+
- - ! '>='
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
segments:
|
124
|
+
- 0
|
125
|
+
hash: 3048921819641476928
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ! '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
segments:
|
133
|
+
- 0
|
134
|
+
hash: 3048921819641476928
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 1.8.24
|
138
|
+
signing_key:
|
139
|
+
specification_version: 3
|
140
|
+
summary: A simple mongo handler. Translates Structs into Mongo and back.
|
141
|
+
test_files:
|
142
|
+
- spec/mongo_adaptor_spec.rb
|