mongodb_adapter 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/FIXME +0 -0
- data/LICENSE +20 -0
- data/README.rdoc +6 -0
- data/Rakefile +70 -0
- data/VERSION +1 -0
- data/lib/mongodb_adapter.rb +163 -0
- data/mongodb_adapter.gemspec +47 -0
- data/spec/mongodb_adapter_spec.rb +22 -0
- data/spec/spec_helper.rb +12 -0
- metadata +75 -0
data/.document
ADDED
data/FIXME
ADDED
File without changes
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Mark Rendle
|
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
data/Rakefile
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
require 'rake/gempackagetask'
|
4
|
+
|
5
|
+
gemspec = Gem::Specification.new do |s|
|
6
|
+
s.platform = Gem::Platform::RUBY
|
7
|
+
s.name = "mongodb_adapter"
|
8
|
+
s.version = "0.1.0"
|
9
|
+
s.author = "Mark Rendle"
|
10
|
+
s.date = "2009-12-30"
|
11
|
+
s.email = "mark@okify.com"
|
12
|
+
|
13
|
+
s.add_dependency "mongo", ">= 0.18.0"
|
14
|
+
|
15
|
+
s.files = [
|
16
|
+
".document",
|
17
|
+
".gitignore",
|
18
|
+
"FIXME",
|
19
|
+
"LICENSE",
|
20
|
+
"README.rdoc",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION",
|
23
|
+
"mongodb_adapter.gemspec",
|
24
|
+
"lib/mongodb_adapter.rb",
|
25
|
+
"spec/mongodb_adapter_spec.rb",
|
26
|
+
"spec/spec_helper.rb"
|
27
|
+
]
|
28
|
+
s.homepage = "http://github.com/okify/mongodb_adapter"
|
29
|
+
s.require_paths = ["lib"]
|
30
|
+
s.summary = "MongoDB adapter for DataMapper"
|
31
|
+
s.test_files = [
|
32
|
+
"spec/mongodb_adapter_spec.rb",
|
33
|
+
"spec/spec_helper.rb"
|
34
|
+
]
|
35
|
+
end
|
36
|
+
|
37
|
+
Rake::GemPackageTask.new(gemspec) do |p|
|
38
|
+
p.need_tar = false
|
39
|
+
p.need_zip = true
|
40
|
+
end
|
41
|
+
|
42
|
+
require 'spec/rake/spectask'
|
43
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
44
|
+
spec.libs << 'lib' << 'spec'
|
45
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
46
|
+
end
|
47
|
+
|
48
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
49
|
+
spec.libs << 'lib' << 'spec'
|
50
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
51
|
+
spec.rcov = true
|
52
|
+
end
|
53
|
+
|
54
|
+
task :default => :spec
|
55
|
+
|
56
|
+
require 'rake/rdoctask'
|
57
|
+
Rake::RDocTask.new do |rdoc|
|
58
|
+
if File.exist?('VERSION.yml')
|
59
|
+
config = YAML.load(File.read('VERSION.yml'))
|
60
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
61
|
+
else
|
62
|
+
version = ""
|
63
|
+
end
|
64
|
+
|
65
|
+
rdoc.rdoc_dir = 'rdoc'
|
66
|
+
rdoc.title = "mongodb_adapter #{version}"
|
67
|
+
rdoc.rdoc_files.include('README*')
|
68
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
69
|
+
end
|
70
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.2
|
@@ -0,0 +1,163 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'dm-core'
|
3
|
+
require 'dm-core/adapters/abstract_adapter'
|
4
|
+
|
5
|
+
require 'mongo'
|
6
|
+
require 'json'
|
7
|
+
require 'uuidtools'
|
8
|
+
require 'pp'
|
9
|
+
|
10
|
+
module DataMapper::Adapters
|
11
|
+
class MongoDBAdapter < AbstractAdapter
|
12
|
+
|
13
|
+
def initialize(name, options)
|
14
|
+
super
|
15
|
+
|
16
|
+
@hostname = @options[:hostname] || 'localhost'
|
17
|
+
@port = @options[:port] || 27017
|
18
|
+
@database = @options[:database] || 'dm-mongodb-test'
|
19
|
+
|
20
|
+
@db = Mongo::Connection.new(@hostname, @port).db(@database)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Create mongodb entry
|
24
|
+
def create(resources)
|
25
|
+
resources.each do |resource|
|
26
|
+
collection = @db.collection(resource.model.storage_name)
|
27
|
+
id_hash = {}
|
28
|
+
Mongo::ObjectID.create_pk(id_hash)
|
29
|
+
initialize_serial(resource, id_hash[:_id].to_s.to_i(16))
|
30
|
+
doc = resource.attributes(:field)
|
31
|
+
doc[:_id] = id_hash[:_id]
|
32
|
+
doc = stringify_bignums(doc)
|
33
|
+
collection.insert(doc)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def read(query)
|
38
|
+
collection = @db.collection(query.model.storage_name)
|
39
|
+
criteria = conditions_to_hash(query.conditions)
|
40
|
+
|
41
|
+
result = []
|
42
|
+
|
43
|
+
cur = collection.find(criteria)
|
44
|
+
cur = cur.limit(query.limit) if query.limit
|
45
|
+
cur.each do |document|
|
46
|
+
document['__bignums'].each do |key|
|
47
|
+
document[key] = document[key].to_i
|
48
|
+
end
|
49
|
+
result << document
|
50
|
+
end
|
51
|
+
|
52
|
+
result
|
53
|
+
end
|
54
|
+
|
55
|
+
def update(attributes,resources)
|
56
|
+
collection = @db.collection(resources[0].model.storage_name)
|
57
|
+
resources.each do |resource|
|
58
|
+
doc = resource.attributes(:field)
|
59
|
+
serial = resource.model.serial(name).get(resource)
|
60
|
+
doc[:_id] = Mongo::ObjectID.from_string(serial.to_s(16))
|
61
|
+
doc = stringify_bignums(doc)
|
62
|
+
collection.save(doc)
|
63
|
+
end
|
64
|
+
resources.length
|
65
|
+
end
|
66
|
+
|
67
|
+
def delete(resources)
|
68
|
+
collection = @db.collection(resources[0].model.storage_name)
|
69
|
+
ids = []
|
70
|
+
resources.each do |resource|
|
71
|
+
serial = resource.model.serial(name).get(resource)
|
72
|
+
ids << Mongo::ObjectID.from_string(serial.to_s(16))
|
73
|
+
end
|
74
|
+
collection.remove(:_id => {'$in' => ids})
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
# Generates uuid from mongodb ruby driver
|
79
|
+
def generate_id
|
80
|
+
UUIDTools::UUID.random_create.to_i
|
81
|
+
end
|
82
|
+
|
83
|
+
def stringify_bignums(hash)
|
84
|
+
hash['__bignums'] = []
|
85
|
+
|
86
|
+
hash.each do |key, value|
|
87
|
+
if value.class == Bignum
|
88
|
+
hash[key] = value.to_s
|
89
|
+
hash['__bignums'] << key
|
90
|
+
end
|
91
|
+
end
|
92
|
+
hash
|
93
|
+
end
|
94
|
+
|
95
|
+
def conditions_to_hash(conditions, hash = {}, negate = false)
|
96
|
+
case conditions
|
97
|
+
when DataMapper::Query::Conditions::NotOperation then operation_to_hash(conditions, hash, !negate)
|
98
|
+
when DataMapper::Query::Conditions::AbstractOperation then operation_to_hash(conditions, hash, negate)
|
99
|
+
when DataMapper::Query::Conditions::AbstractComparison then comparison_to_hash(conditions, hash, negate)
|
100
|
+
end
|
101
|
+
hash
|
102
|
+
end
|
103
|
+
|
104
|
+
def operation_to_hash(operation, hash, negate)
|
105
|
+
operation.each do |operand|
|
106
|
+
conditions_to_hash(operand, hash, negate)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def comparison_to_hash(comparison, hash, negate)
|
111
|
+
value = nil
|
112
|
+
if comparison.slug == :eql
|
113
|
+
value = mongify_value(comparison.value,negate)
|
114
|
+
elsif [:gt,:gte,:lt,:lte].include?(comparison.slug)
|
115
|
+
value = mongify_value(comparison.value,negate)
|
116
|
+
value = value.first[1] if value.is_a?(Hash)
|
117
|
+
value = {mongify_inequality(comparison.slug, negate) => value}
|
118
|
+
elsif comparison.slug == :in
|
119
|
+
value = mongify_value(comparison.value,negate)
|
120
|
+
elsif comparison.slug == :like
|
121
|
+
value = Regexp.new(comparison.value.gsub(/%/, '.*'))
|
122
|
+
value = mongify_value(value,negate)
|
123
|
+
elsif comparison.slug == :regexp
|
124
|
+
value = Regexp.new(comparison.value)
|
125
|
+
value = mongify_value(value,negate)
|
126
|
+
else
|
127
|
+
puts "!!! Not handling #{comparison.slug} !!!"
|
128
|
+
end
|
129
|
+
hash[comparison.subject.name.to_s] = value
|
130
|
+
end
|
131
|
+
|
132
|
+
def mongify_value(value, negate)
|
133
|
+
case value
|
134
|
+
when Bignum then mongify_value(value.to_s, negate)
|
135
|
+
when Array then {negate ? '$nin' : '$in' => value}
|
136
|
+
when Range then mongify_range(value, negate)
|
137
|
+
else negate ? {'$ne' => value} : value
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def mongify_range(value, negate)
|
142
|
+
first_op = negate ? '$lt' : '$gte'
|
143
|
+
if value.exclude_end?
|
144
|
+
last_op = negate ? '$gte' : '$lt'
|
145
|
+
else
|
146
|
+
last_op = negate ? '$gt' : '$lte'
|
147
|
+
end
|
148
|
+
{first_op => value.first, last_op => value.last}
|
149
|
+
end
|
150
|
+
|
151
|
+
def mongify_inequality(slug, negate)
|
152
|
+
case slug
|
153
|
+
when :gt then negate ? '$lte' : '$gt'
|
154
|
+
when :gte then negate ? '$lt' : '$gte'
|
155
|
+
when :lt then negate ? '$gte' : '$lt'
|
156
|
+
when :lte then negate ? '$gt' : '$lte'
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
end # class MongodbAdapter
|
161
|
+
|
162
|
+
MongodbAdapter = MongoDBAdapter
|
163
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{mongodb_adapter}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Mark Rendle"]
|
9
|
+
s.date = %q{2009-12-30}
|
10
|
+
s.email = %q{mark@okify.com}
|
11
|
+
s.extra_rdoc_files = [
|
12
|
+
"LICENSE",
|
13
|
+
"README.rdoc"
|
14
|
+
]
|
15
|
+
s.files = [
|
16
|
+
".document",
|
17
|
+
".gitignore",
|
18
|
+
"FIXME",
|
19
|
+
"LICENSE",
|
20
|
+
"README.rdoc",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION",
|
23
|
+
"mongodb_adapter.gemspec",
|
24
|
+
"lib/mongodb_adapter.rb",
|
25
|
+
"spec/mongodb_adapter_spec.rb",
|
26
|
+
"spec/spec_helper.rb"
|
27
|
+
]
|
28
|
+
s.homepage = %q{http://github.com/okify/mongodb_adapter}
|
29
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
30
|
+
s.require_paths = ["lib"]
|
31
|
+
s.rubygems_version = %q{1.3.5}
|
32
|
+
s.summary = %q{MongoDB adapter for DataMapper}
|
33
|
+
s.test_files = [
|
34
|
+
"spec/mongodb_adapter_spec.rb",
|
35
|
+
"spec/spec_helper.rb"
|
36
|
+
]
|
37
|
+
|
38
|
+
if s.respond_to? :specification_version then
|
39
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
40
|
+
s.specification_version = 3
|
41
|
+
|
42
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
43
|
+
else
|
44
|
+
end
|
45
|
+
else
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
3
|
+
require 'mongo'
|
4
|
+
|
5
|
+
require 'dm-core/spec/adapter_shared_spec'
|
6
|
+
|
7
|
+
describe DataMapper::Adapters::MongoDBAdapter do
|
8
|
+
before :all do
|
9
|
+
@adapter = DataMapper.setup(:default,
|
10
|
+
:adapter => 'mongodb',
|
11
|
+
:hostname => 'localhost',
|
12
|
+
:port => 27017,
|
13
|
+
:database => 'dm-mongodb-test'
|
14
|
+
)
|
15
|
+
|
16
|
+
Mongo::Connection.new.db('dm-mongodb-test')['heffalumps'].remove
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
it_should_behave_like 'An Adapter'
|
21
|
+
|
22
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
|
7
|
+
require 'mongodb_adapter'
|
8
|
+
require 'dm-core'
|
9
|
+
|
10
|
+
# Spec::Runner.configure do |config|
|
11
|
+
#
|
12
|
+
# end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongodb_adapter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mark Rendle
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-30 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mongo
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.18.0
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: mark@okify.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- .document
|
35
|
+
- .gitignore
|
36
|
+
- FIXME
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- mongodb_adapter.gemspec
|
42
|
+
- lib/mongodb_adapter.rb
|
43
|
+
- spec/mongodb_adapter_spec.rb
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://github.com/okify/mongodb_adapter
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: MongoDB adapter for DataMapper
|
73
|
+
test_files:
|
74
|
+
- spec/mongodb_adapter_spec.rb
|
75
|
+
- spec/spec_helper.rb
|