mongodb_adapter 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -4,3 +4,19 @@ A mostly functional DataMapper adapter for MongoDB.
4
4
 
5
5
  4 specs are not passing, to do with OR conditions or
6
6
  negated regular expressions.
7
+
8
+ == Installation
9
+
10
+ gem install mongodb_adapter
11
+
12
+ == Usage
13
+
14
+ adapter: mongodb
15
+ database: databasename
16
+ host: hostname # defaults to 'localhost'
17
+ port: portnum # defaults to 27017
18
+
19
+ == Notes
20
+
21
+ Uses the 'mongo' gem. For extra performance you should install the
22
+ 'mongo_ext' gem which adds native compiled components.
data/Rakefile CHANGED
@@ -5,9 +5,9 @@ require 'rake/gempackagetask'
5
5
  gemspec = Gem::Specification.new do |s|
6
6
  s.platform = Gem::Platform::RUBY
7
7
  s.name = "mongodb_adapter"
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
  s.author = "Mark Rendle"
10
- s.date = "2009-12-30"
10
+ s.date = "2010-01-13"
11
11
  s.email = "mark@okify.com"
12
12
 
13
13
  s.add_dependency "mongo", ">= 0.18.0"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.1.1
@@ -7,157 +7,186 @@ require 'json'
7
7
  require 'uuidtools'
8
8
  require 'pp'
9
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'
10
+ module DataMapper
11
+ module Adapters
12
+ class MongoDBAdapter < AbstractAdapter
13
+
14
+ def initialize(name, options)
15
+ super
16
+
17
+ @hostname = @options[:hostname] || 'localhost'
18
+ @port = @options[:port] || 27017
19
+ @database = @options[:database] || 'dm-mongodb-test'
20
+
21
+ @db = Mongo::Connection.new(@hostname, @port).db(@database)
22
+ end
19
23
 
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)
24
+ # Create mongodb entry
25
+ def create(resources)
26
+ resources.each do |resource|
27
+ collection = @db.collection(resource.model.storage_name)
28
+ id_hash = {}
29
+ Mongo::ObjectID.create_pk(id_hash)
30
+ initialize_serial(resource, id_hash[:_id].to_s.to_i(16))
31
+ doc = resource.attributes(:field)
32
+ doc[:_id] = id_hash[:_id]
33
+ doc = stringify_bignums(doc)
34
+ doc = stringify_discriminators(doc)
35
+ collection.insert(doc)
36
+ end
34
37
  end
35
- end
36
38
 
37
- def read(query)
38
- collection = @db.collection(query.model.storage_name)
39
- criteria = conditions_to_hash(query.conditions)
39
+ def read(query)
40
+ collection = @db.collection(query.model.storage_name)
41
+ criteria = conditions_to_hash(query.conditions)
40
42
 
41
- result = []
43
+ result = []
42
44
 
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
45
+ cur = collection.find(criteria)
46
+ cur = cur.limit(query.limit) if query.limit
47
+ cur.each do |document|
48
+ document['__bignums'].each do |key|
49
+ document[key] = document[key].to_i
50
+ end
51
+ document['__discriminators'].each do |key|
52
+ document[key] = eval(document[key])
53
+ end
54
+ result << document
48
55
  end
49
- result << document
50
- end
51
-
52
- result
53
- end
54
56
 
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)
57
+ result
63
58
  end
64
- resources.length
65
- end
66
59
 
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))
60
+ def update(attributes,resources)
61
+ collection = @db.collection(resources[0].model.storage_name)
62
+ resources.each do |resource|
63
+ doc = resource.attributes(:field)
64
+ serial = resource.model.serial(name).get(resource)
65
+ doc[:_id] = Mongo::ObjectID.from_string(serial.to_s(16))
66
+ doc = stringify_bignums(doc)
67
+ doc = stringify_discriminators(doc)
68
+ collection.save(doc)
69
+ end
70
+ resources.length
73
71
  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
72
+
73
+ def delete(resources)
74
+ collection = @db.collection(resources[0].model.storage_name)
75
+ ids = []
76
+ resources.each do |resource|
77
+ serial = resource.model.serial(name).get(resource)
78
+ ids << Mongo::ObjectID.from_string(serial.to_s(16))
79
+ end
80
+ collection.remove(:_id => {'$in' => ids})
81
81
  end
82
+
83
+ private
84
+ # Generates uuid from mongodb ruby driver
85
+ def generate_id
86
+ UUIDTools::UUID.random_create.to_i
87
+ end
82
88
 
83
- def stringify_bignums(hash)
84
- hash['__bignums'] = []
89
+ def stringify_bignums(hash)
90
+ hash['__bignums'] = []
85
91
 
86
- hash.each do |key, value|
87
- if value.class == Bignum
88
- hash[key] = value.to_s
89
- hash['__bignums'] << key
92
+ hash.each do |key, value|
93
+ if value.class == Bignum
94
+ hash[key] = value.to_s
95
+ hash['__bignums'] << key
96
+ end
90
97
  end
98
+ hash
91
99
  end
92
- hash
93
- end
94
100
 
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)
101
+ def stringify_discriminators(hash)
102
+ hash['__discriminators'] = []
103
+
104
+ hash.each do |key, value|
105
+ if value.class == Class
106
+ hash[key] = value.name
107
+ hash['__discriminators'] << key
108
+ end
109
+ end
110
+ hash
100
111
  end
101
- hash
102
- end
103
112
 
104
- def operation_to_hash(operation, hash, negate)
105
- operation.each do |operand|
106
- conditions_to_hash(operand, hash, negate)
113
+ def conditions_to_hash(conditions, hash = {}, negate = false)
114
+ case conditions
115
+ when DataMapper::Query::Conditions::NotOperation then operation_to_hash(conditions, hash, !negate)
116
+ when DataMapper::Query::Conditions::AbstractOperation then operation_to_hash(conditions, hash, negate)
117
+ when DataMapper::Query::Conditions::AbstractComparison then comparison_to_hash(conditions, hash, negate)
118
+ end
119
+ hash
107
120
  end
108
- end
109
121
 
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} !!!"
122
+ def operation_to_hash(operation, hash, negate)
123
+ operation.each do |operand|
124
+ conditions_to_hash(operand, hash, negate)
125
+ end
128
126
  end
129
- hash[comparison.subject.name.to_s] = value
130
- end
131
127
 
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
128
+ def comparison_to_hash(comparison, hash, negate)
129
+ value = nil
130
+ if comparison.slug == :eql
131
+ value = mongify_value(comparison.value,negate)
132
+ elsif [:gt,:gte,:lt,:lte].include?(comparison.slug)
133
+ value = mongify_value(comparison.value,negate)
134
+ value = value.first[1] if value.is_a?(Hash)
135
+ value = {mongify_inequality(comparison.slug, negate) => value}
136
+ elsif comparison.slug == :in
137
+ value = mongify_value(comparison.value,negate)
138
+ elsif comparison.slug == :like
139
+ value = Regexp.new(comparison.value.gsub(/%/, '.*'))
140
+ value = mongify_value(value,negate)
141
+ elsif comparison.slug == :regexp
142
+ value = Regexp.new(comparison.value)
143
+ value = mongify_value(value,negate)
144
+ else
145
+ puts "!!! Not handling #{comparison.slug} !!!"
146
+ end
147
+ hash[comparison.subject.name.to_s] = value
138
148
  end
139
- end
140
149
 
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'
150
+ def mongify_value(value, negate)
151
+ case value
152
+ when Bignum then mongify_value(value.to_s, negate)
153
+ when Class then mongify_value(value.name, negate)
154
+ when Array then {negate ? '$nin' : '$in' => value}
155
+ when Range then mongify_range(value, negate)
156
+ else negate ? {'$ne' => value} : value
157
+ end
158
+ end
159
+
160
+ def mongify_range(value, negate)
161
+ first_op = negate ? '$lt' : '$gte'
162
+ if value.exclude_end?
163
+ last_op = negate ? '$gte' : '$lt'
164
+ else
165
+ last_op = negate ? '$gt' : '$lte'
166
+ end
167
+ {first_op => value.first, last_op => value.last}
147
168
  end
148
- {first_op => value.first, last_op => value.last}
149
- end
150
169
 
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'
170
+ def mongify_inequality(slug, negate)
171
+ case slug
172
+ when :gt then negate ? '$lte' : '$gt'
173
+ when :gte then negate ? '$lt' : '$gte'
174
+ when :lt then negate ? '$gte' : '$lt'
175
+ when :lte then negate ? '$gt' : '$lte'
176
+ end
157
177
  end
158
- end
159
-
160
- end # class MongodbAdapter
178
+
179
+ end # class MongodbAdapter
180
+
181
+ MongodbAdapter = MongoDBAdapter
182
+ end
183
+
184
+ module Types
185
+
186
+ class Array < DataMapper::Type
187
+ primitive ::Object
188
+ end
189
+
190
+ end
161
191
 
162
- MongodbAdapter = MongoDBAdapter
163
192
  end
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{mongodb_adapter}
5
- s.version = "0.1.0"
5
+ s.version = "0.1.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Mark Rendle"]
9
- s.date = %q{2009-12-30}
9
+ s.date = %q{2010-01-13}
10
10
  s.email = %q{mark@okify.com}
11
11
  s.extra_rdoc_files = [
12
12
  "LICENSE",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongodb_adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Rendle
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-30 00:00:00 +00:00
12
+ date: 2010-01-13 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency