djsun-mongomapper 0.3.5.5 → 0.4.1.2
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/README.rdoc +38 -38
- data/Rakefile +87 -73
- data/VERSION +1 -1
- data/lib/mongomapper.rb +67 -71
- data/lib/mongomapper/associations.rb +86 -84
- data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +34 -34
- data/lib/mongomapper/associations/many_embedded_proxy.rb +67 -17
- data/lib/mongomapper/associations/proxy.rb +74 -73
- data/lib/mongomapper/document.rb +342 -348
- data/lib/mongomapper/embedded_document.rb +354 -274
- data/lib/mongomapper/finder_options.rb +84 -84
- data/lib/mongomapper/key.rb +32 -76
- data/lib/mongomapper/rails_compatibility/document.rb +14 -14
- data/lib/mongomapper/rails_compatibility/embedded_document.rb +26 -24
- data/lib/mongomapper/support.rb +156 -29
- data/lib/mongomapper/validations.rb +69 -47
- data/test/custom_matchers.rb +48 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +53 -56
- data/test/functional/associations/test_belongs_to_proxy.rb +48 -49
- data/test/functional/associations/test_many_documents_as_proxy.rb +208 -253
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +130 -130
- data/test/functional/associations/test_many_embedded_proxy.rb +168 -106
- data/test/functional/associations/test_many_polymorphic_proxy.rb +261 -262
- data/test/functional/test_binary.rb +21 -0
- data/test/functional/test_document.rb +946 -952
- data/test/functional/test_embedded_document.rb +98 -0
- data/test/functional/test_pagination.rb +87 -80
- data/test/functional/test_rails_compatibility.rb +29 -29
- data/test/functional/test_validations.rb +262 -172
- data/test/models.rb +169 -169
- data/test/test_helper.rb +28 -66
- data/test/unit/serializers/test_json_serializer.rb +193 -193
- data/test/unit/test_document.rb +161 -123
- data/test/unit/test_embedded_document.rb +643 -547
- data/test/unit/test_finder_options.rb +183 -183
- data/test/unit/test_key.rb +175 -247
- data/test/unit/test_rails_compatibility.rb +38 -33
- data/test/unit/test_serializations.rb +52 -52
- data/test/unit/test_support.rb +268 -0
- data/test/unit/test_time_zones.rb +40 -0
- data/test/unit/test_validations.rb +499 -258
- metadata +22 -12
- data/History +0 -76
- data/mongomapper.gemspec +0 -145
@@ -1,274 +1,354 @@
|
|
1
|
-
require 'observer'
|
2
|
-
|
3
|
-
module MongoMapper
|
4
|
-
module EmbeddedDocument
|
5
|
-
def self.included(model)
|
6
|
-
model.class_eval do
|
7
|
-
extend ClassMethods
|
8
|
-
include InstanceMethods
|
9
|
-
|
10
|
-
extend Associations::ClassMethods
|
11
|
-
include Associations::InstanceMethods
|
12
|
-
|
13
|
-
include RailsCompatibility::EmbeddedDocument
|
14
|
-
include Validatable
|
15
|
-
include Serialization
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
key
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
end
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
end
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
1
|
+
require 'observer'
|
2
|
+
|
3
|
+
module MongoMapper
|
4
|
+
module EmbeddedDocument
|
5
|
+
def self.included(model)
|
6
|
+
model.class_eval do
|
7
|
+
extend ClassMethods
|
8
|
+
include InstanceMethods
|
9
|
+
|
10
|
+
extend Associations::ClassMethods
|
11
|
+
include Associations::InstanceMethods
|
12
|
+
|
13
|
+
include RailsCompatibility::EmbeddedDocument
|
14
|
+
include Validatable
|
15
|
+
include Serialization
|
16
|
+
|
17
|
+
extend Validations::Macros
|
18
|
+
|
19
|
+
key :_id, String
|
20
|
+
attr_accessor :_root_document
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module ClassMethods
|
25
|
+
def inherited(subclass)
|
26
|
+
unless subclass.embeddable?
|
27
|
+
subclass.set_collection_name(collection_name)
|
28
|
+
end
|
29
|
+
|
30
|
+
(@subclasses ||= []) << subclass
|
31
|
+
end
|
32
|
+
|
33
|
+
def subclasses
|
34
|
+
@subclasses
|
35
|
+
end
|
36
|
+
|
37
|
+
def keys
|
38
|
+
@keys ||= if parent = parent_model
|
39
|
+
parent.keys.dup
|
40
|
+
else
|
41
|
+
HashWithIndifferentAccess.new
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def key(*args)
|
46
|
+
key = Key.new(*args)
|
47
|
+
|
48
|
+
if keys[key.name].blank?
|
49
|
+
keys[key.name] = key
|
50
|
+
|
51
|
+
create_accessors_for(key)
|
52
|
+
add_to_subclasses(*args)
|
53
|
+
apply_validations_for(key)
|
54
|
+
create_indexes_for(key)
|
55
|
+
|
56
|
+
key
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def add_to_subclasses(*args)
|
61
|
+
return if subclasses.blank?
|
62
|
+
|
63
|
+
subclasses.each do |subclass|
|
64
|
+
subclass.key(*args)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def ensure_index(name_or_array, options={})
|
69
|
+
keys_to_index = if name_or_array.is_a?(Array)
|
70
|
+
name_or_array.map { |pair| [pair[0], pair[1]] }
|
71
|
+
else
|
72
|
+
name_or_array
|
73
|
+
end
|
74
|
+
|
75
|
+
collection.create_index(keys_to_index, options.delete(:unique))
|
76
|
+
end
|
77
|
+
|
78
|
+
def embeddable?
|
79
|
+
!self.ancestors.include?(Document)
|
80
|
+
end
|
81
|
+
|
82
|
+
def parent_model
|
83
|
+
(ancestors - [self,EmbeddedDocument]).find do |parent_class|
|
84
|
+
parent_class.ancestors.include?(EmbeddedDocument)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def to_mongo(instance)
|
89
|
+
return nil if instance.nil?
|
90
|
+
instance.to_mongo
|
91
|
+
end
|
92
|
+
|
93
|
+
def from_mongo(instance_or_hash)
|
94
|
+
return nil if instance_or_hash.nil?
|
95
|
+
|
96
|
+
if instance_or_hash.is_a?(self)
|
97
|
+
instance_or_hash
|
98
|
+
else
|
99
|
+
new(instance_or_hash)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
def accessors_module
|
105
|
+
if const_defined?('MongoMapperKeys')
|
106
|
+
const_get 'MongoMapperKeys'
|
107
|
+
else
|
108
|
+
const_set 'MongoMapperKeys', Module.new
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def create_accessors_for(key)
|
113
|
+
accessors_module.module_eval <<-end_eval
|
114
|
+
def #{key.name}
|
115
|
+
read_attribute(:'#{key.name}')
|
116
|
+
end
|
117
|
+
|
118
|
+
def #{key.name}_before_typecast
|
119
|
+
read_attribute_before_typecast(:'#{key.name}')
|
120
|
+
end
|
121
|
+
|
122
|
+
def #{key.name}=(value)
|
123
|
+
write_attribute(:'#{key.name}', value)
|
124
|
+
end
|
125
|
+
|
126
|
+
def #{key.name}?
|
127
|
+
read_attribute(:#{key.name}).present?
|
128
|
+
end
|
129
|
+
end_eval
|
130
|
+
include accessors_module
|
131
|
+
end
|
132
|
+
|
133
|
+
def create_indexes_for(key)
|
134
|
+
ensure_index key.name if key.options[:index]
|
135
|
+
end
|
136
|
+
|
137
|
+
def apply_validations_for(key)
|
138
|
+
attribute = key.name.to_sym
|
139
|
+
|
140
|
+
if key.options[:required]
|
141
|
+
validates_presence_of(attribute)
|
142
|
+
end
|
143
|
+
|
144
|
+
if key.options[:unique]
|
145
|
+
validates_uniqueness_of(attribute)
|
146
|
+
end
|
147
|
+
|
148
|
+
if key.options[:numeric]
|
149
|
+
number_options = key.type == Integer ? {:only_integer => true} : {}
|
150
|
+
validates_numericality_of(attribute, number_options)
|
151
|
+
end
|
152
|
+
|
153
|
+
if key.options[:format]
|
154
|
+
validates_format_of(attribute, :with => key.options[:format])
|
155
|
+
end
|
156
|
+
|
157
|
+
if key.options[:length]
|
158
|
+
length_options = case key.options[:length]
|
159
|
+
when Integer
|
160
|
+
{:minimum => 0, :maximum => key.options[:length]}
|
161
|
+
when Range
|
162
|
+
{:within => key.options[:length]}
|
163
|
+
when Hash
|
164
|
+
key.options[:length]
|
165
|
+
end
|
166
|
+
validates_length_of(attribute, length_options)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
module InstanceMethods
|
172
|
+
def initialize(attrs={})
|
173
|
+
unless attrs.nil?
|
174
|
+
self.class.associations.each_pair do |name, association|
|
175
|
+
if collection = attrs.delete(name)
|
176
|
+
if association.many? && association.klass.embeddable?
|
177
|
+
root_document = attrs[:_root_document] || self
|
178
|
+
collection.each do |doc|
|
179
|
+
doc[:_root_document] = root_document
|
180
|
+
end
|
181
|
+
end
|
182
|
+
send("#{association.name}=", collection)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
self.attributes = attrs
|
187
|
+
|
188
|
+
if respond_to?(:_type=) && self['_type'].blank?
|
189
|
+
self._type = self.class.name
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
if self.class.embeddable?
|
194
|
+
if read_attribute(:_id).blank?
|
195
|
+
write_attribute :_id, Mongo::ObjectID.new.to_s
|
196
|
+
@new_document = true
|
197
|
+
else
|
198
|
+
@new_document = false
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
def new?
|
204
|
+
!!@new_document
|
205
|
+
end
|
206
|
+
|
207
|
+
def attributes=(attrs)
|
208
|
+
return if attrs.blank?
|
209
|
+
attrs.each_pair do |name, value|
|
210
|
+
writer_method = "#{name}="
|
211
|
+
|
212
|
+
if respond_to?(writer_method)
|
213
|
+
self.send(writer_method, value)
|
214
|
+
else
|
215
|
+
self[name.to_s] = value
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
def attributes
|
221
|
+
attrs = HashWithIndifferentAccess.new
|
222
|
+
|
223
|
+
embedded_keys.each do |key|
|
224
|
+
puts key.inspect
|
225
|
+
attrs[key.name] = read_attribute(key.name).try(:attributes)
|
226
|
+
end
|
227
|
+
|
228
|
+
non_embedded_keys.each do |key|
|
229
|
+
attrs[key.name] = read_attribute(key.name)
|
230
|
+
end
|
231
|
+
|
232
|
+
embedded_associations.each do |association|
|
233
|
+
documents = instance_variable_get(association.ivar)
|
234
|
+
next if documents.nil?
|
235
|
+
attrs[association.name] = documents.collect { |doc| doc.attributes }
|
236
|
+
end
|
237
|
+
|
238
|
+
attrs
|
239
|
+
end
|
240
|
+
|
241
|
+
def to_mongo
|
242
|
+
attrs = HashWithIndifferentAccess.new
|
243
|
+
|
244
|
+
_keys.each_pair do |name, key|
|
245
|
+
value = key.set(read_attribute(key.name))
|
246
|
+
attrs[name] = value
|
247
|
+
end
|
248
|
+
|
249
|
+
embedded_associations.each do |association|
|
250
|
+
if documents = instance_variable_get(association.ivar)
|
251
|
+
attrs[association.name] = documents.map { |document| document.to_mongo }
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
attrs
|
256
|
+
end
|
257
|
+
|
258
|
+
def clone
|
259
|
+
clone_attributes = self.attributes
|
260
|
+
clone_attributes.delete("_id")
|
261
|
+
self.class.new(clone_attributes)
|
262
|
+
end
|
263
|
+
|
264
|
+
def [](name)
|
265
|
+
read_attribute(name)
|
266
|
+
end
|
267
|
+
|
268
|
+
def []=(name, value)
|
269
|
+
ensure_key_exists(name)
|
270
|
+
write_attribute(name, value)
|
271
|
+
end
|
272
|
+
|
273
|
+
def ==(other)
|
274
|
+
other.is_a?(self.class) && id == other.id
|
275
|
+
end
|
276
|
+
|
277
|
+
def id
|
278
|
+
read_attribute(:_id)
|
279
|
+
end
|
280
|
+
|
281
|
+
def id=(value)
|
282
|
+
@using_custom_id = true
|
283
|
+
write_attribute :_id, value
|
284
|
+
end
|
285
|
+
|
286
|
+
def using_custom_id?
|
287
|
+
!!@using_custom_id
|
288
|
+
end
|
289
|
+
|
290
|
+
def inspect
|
291
|
+
attributes_as_nice_string = key_names.collect do |name|
|
292
|
+
"#{name}: #{read_attribute(name).inspect}"
|
293
|
+
end.join(", ")
|
294
|
+
"#<#{self.class} #{attributes_as_nice_string}>"
|
295
|
+
end
|
296
|
+
|
297
|
+
def save
|
298
|
+
if _root_document
|
299
|
+
_root_document.save
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
def update_attributes(attrs={})
|
304
|
+
self.attributes = attrs
|
305
|
+
save
|
306
|
+
end
|
307
|
+
|
308
|
+
private
|
309
|
+
def _keys
|
310
|
+
self.class.keys
|
311
|
+
end
|
312
|
+
|
313
|
+
def key_names
|
314
|
+
_keys.keys
|
315
|
+
end
|
316
|
+
|
317
|
+
def non_embedded_keys
|
318
|
+
_keys.values.select { |key| !key.embeddable? }
|
319
|
+
end
|
320
|
+
|
321
|
+
def embedded_keys
|
322
|
+
_keys.values.select { |key| key.embeddable? }
|
323
|
+
end
|
324
|
+
|
325
|
+
def ensure_key_exists(name)
|
326
|
+
self.class.key(name) unless respond_to?("#{name}=")
|
327
|
+
end
|
328
|
+
|
329
|
+
def read_attribute(name)
|
330
|
+
value = _keys[name].get(instance_variable_get("@#{name}"))
|
331
|
+
instance_variable_set "@#{name}", value if !frozen?
|
332
|
+
value
|
333
|
+
end
|
334
|
+
|
335
|
+
def read_attribute_before_typecast(name)
|
336
|
+
instance_variable_get("@#{name}_before_typecast")
|
337
|
+
end
|
338
|
+
|
339
|
+
def write_attribute(name, value)
|
340
|
+
key = _keys[name]
|
341
|
+
instance_variable_set "@#{name}_before_typecast", value
|
342
|
+
instance_variable_set "@#{name}", key.set(value)
|
343
|
+
end
|
344
|
+
|
345
|
+
def embedded_associations
|
346
|
+
self.class.associations.select do |name, association|
|
347
|
+
association.embeddable?
|
348
|
+
end.map do |name, association|
|
349
|
+
association
|
350
|
+
end
|
351
|
+
end
|
352
|
+
end # InstanceMethods
|
353
|
+
end # EmbeddedDocument
|
354
|
+
end # MongoMapper
|