mongo_mapper 0.13.1 → 0.14.0.rc1

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.
Files changed (103) hide show
  1. checksums.yaml +13 -5
  2. data/README.rdoc +3 -6
  3. data/lib/mongo_mapper.rb +1 -0
  4. data/lib/mongo_mapper/document.rb +2 -0
  5. data/lib/mongo_mapper/extensions/array.rb +14 -6
  6. data/lib/mongo_mapper/extensions/hash.rb +15 -3
  7. data/lib/mongo_mapper/extensions/object.rb +4 -0
  8. data/lib/mongo_mapper/extensions/string.rb +13 -5
  9. data/lib/mongo_mapper/plugins/accessible.rb +12 -11
  10. data/lib/mongo_mapper/plugins/associations.rb +7 -6
  11. data/lib/mongo_mapper/plugins/associations/base.rb +13 -12
  12. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +9 -8
  13. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +12 -11
  14. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +4 -4
  15. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +24 -23
  16. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +18 -16
  17. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +55 -48
  18. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +14 -13
  19. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +7 -6
  20. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +7 -5
  21. data/lib/mongo_mapper/plugins/associations/one_as_proxy.rb +14 -11
  22. data/lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +14 -13
  23. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +9 -9
  24. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +27 -26
  25. data/lib/mongo_mapper/plugins/associations/proxy.rb +29 -26
  26. data/lib/mongo_mapper/plugins/callbacks.rb +13 -0
  27. data/lib/mongo_mapper/plugins/counter_cache.rb +23 -4
  28. data/lib/mongo_mapper/plugins/dirty.rb +2 -2
  29. data/lib/mongo_mapper/plugins/dynamic_querying.rb +10 -9
  30. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +17 -16
  31. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +1 -1
  32. data/lib/mongo_mapper/plugins/identity_map.rb +1 -1
  33. data/lib/mongo_mapper/plugins/indexes.rb +1 -1
  34. data/lib/mongo_mapper/plugins/keys.rb +158 -158
  35. data/lib/mongo_mapper/plugins/keys/key.rb +16 -10
  36. data/lib/mongo_mapper/plugins/keys/static.rb +45 -0
  37. data/lib/mongo_mapper/plugins/modifiers.rb +27 -26
  38. data/lib/mongo_mapper/plugins/partial_updates.rb +86 -0
  39. data/lib/mongo_mapper/plugins/persistence.rb +7 -6
  40. data/lib/mongo_mapper/plugins/protected.rb +6 -5
  41. data/lib/mongo_mapper/plugins/querying.rb +80 -43
  42. data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +14 -9
  43. data/lib/mongo_mapper/plugins/scopes.rb +78 -7
  44. data/lib/mongo_mapper/plugins/timestamps.rb +1 -0
  45. data/lib/mongo_mapper/plugins/validations.rb +0 -0
  46. data/lib/mongo_mapper/version.rb +1 -1
  47. data/lib/rails/generators/mongo_mapper/config/config_generator.rb +12 -13
  48. data/lib/rails/generators/mongo_mapper/model/model_generator.rb +9 -9
  49. data/spec/functional/accessible_spec.rb +12 -12
  50. data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +11 -11
  51. data/spec/functional/associations/belongs_to_proxy_spec.rb +14 -15
  52. data/spec/functional/associations/in_array_proxy_spec.rb +6 -6
  53. data/spec/functional/associations/many_documents_proxy_spec.rb +89 -18
  54. data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +11 -11
  55. data/spec/functional/associations/many_embedded_proxy_spec.rb +1 -1
  56. data/spec/functional/associations/one_as_proxy_spec.rb +14 -14
  57. data/spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb +9 -9
  58. data/spec/functional/associations/one_embedded_proxy_spec.rb +3 -3
  59. data/spec/functional/associations/one_proxy_spec.rb +14 -14
  60. data/spec/functional/caching_spec.rb +8 -8
  61. data/spec/functional/callbacks_spec.rb +87 -0
  62. data/spec/functional/counter_cache_spec.rb +89 -0
  63. data/spec/functional/dirty_spec.rb +41 -41
  64. data/spec/functional/document_spec.rb +3 -3
  65. data/spec/functional/embedded_document_spec.rb +18 -18
  66. data/spec/functional/identity_map_spec.rb +28 -15
  67. data/spec/functional/indexes_spec.rb +4 -4
  68. data/spec/functional/keys_spec.rb +12 -3
  69. data/spec/functional/logger_spec.rb +1 -1
  70. data/spec/functional/modifiers_spec.rb +2 -2
  71. data/spec/functional/partial_updates_spec.rb +577 -0
  72. data/spec/functional/protected_spec.rb +13 -13
  73. data/spec/functional/querying_spec.rb +11 -10
  74. data/spec/functional/safe_spec.rb +2 -2
  75. data/spec/functional/sci_spec.rb +3 -3
  76. data/spec/functional/scopes_spec.rb +234 -1
  77. data/spec/functional/static_keys_spec.rb +153 -0
  78. data/spec/functional/stats_spec.rb +0 -4
  79. data/spec/functional/touch_spec.rb +1 -1
  80. data/spec/functional/validations_spec.rb +59 -57
  81. data/spec/quality_spec.rb +1 -1
  82. data/spec/spec_helper.rb +7 -3
  83. data/spec/support/matchers.rb +4 -13
  84. data/spec/unit/associations/base_spec.rb +12 -12
  85. data/spec/unit/associations/belongs_to_association_spec.rb +2 -2
  86. data/spec/unit/associations/many_association_spec.rb +2 -2
  87. data/spec/unit/associations/one_association_spec.rb +2 -2
  88. data/spec/unit/associations/proxy_spec.rb +13 -15
  89. data/spec/unit/document_spec.rb +5 -5
  90. data/spec/unit/dynamic_finder_spec.rb +8 -8
  91. data/spec/unit/embedded_document_spec.rb +14 -14
  92. data/spec/unit/extensions_spec.rb +17 -17
  93. data/spec/unit/identity_map_middleware_spec.rb +5 -5
  94. data/spec/unit/key_spec.rb +24 -21
  95. data/spec/unit/keys_spec.rb +5 -5
  96. data/spec/unit/mongo_mapper_spec.rb +26 -26
  97. data/spec/unit/rails_spec.rb +2 -2
  98. data/spec/unit/serialization_spec.rb +1 -1
  99. data/spec/unit/time_zones_spec.rb +2 -2
  100. data/spec/unit/validations_spec.rb +28 -15
  101. metadata +16 -14
  102. data/lib/mongo_mapper/connections/10gen.rb +0 -0
  103. data/lib/mongo_mapper/connections/moped.rb +0 -0
@@ -49,29 +49,48 @@ module MongoMapper
49
49
  raise InvalidCounterCacheError, "You must define an association with name `#{association_name}' on model #{self}"
50
50
  end
51
51
 
52
- association_class = association.klass
53
- key_names = association_class.keys.keys
52
+ # make a define-time check to make sure the counter cache field is defined.
53
+ # note: this can only be done in non-polymorphic classes
54
+ # (since we may not know the class on the other side of the association)
55
+ if !association.polymorphic?
56
+ association_class = association.klass
57
+ key_names = association_class.keys.keys
54
58
 
55
- if !key_names.include?(field.to_s)
56
- raise InvalidCounterCacheError, "Missing `key #{field.to_sym.inspect}, Integer, :default => 0' on model #{association_class}"
59
+ if !key_names.include?(field.to_s)
60
+ _raise_when_missing_counter_cache_key(association_class, field)
61
+ end
57
62
  end
58
63
 
59
64
  after_create do
60
65
  if obj = self.send(association_name)
66
+ if !obj.respond_to?(field)
67
+ self.class._raise_when_missing_counter_cache_key(obj.class, field)
68
+ end
69
+
61
70
  obj.increment(field => 1)
62
71
  obj.write_attribute(field, obj.read_attribute(field) + 1)
63
72
  end
73
+
64
74
  true
65
75
  end
66
76
 
67
77
  after_destroy do
68
78
  if obj = self.send(association_name)
79
+ if !obj.respond_to?(field)
80
+ self.class._raise_when_missing_counter_cache_key(obj.class, field)
81
+ end
82
+
69
83
  obj.decrement(field => 1)
70
84
  obj.write_attribute(field, obj.read_attribute(field) - 1)
71
85
  end
86
+
72
87
  true
73
88
  end
74
89
  end
90
+
91
+ def _raise_when_missing_counter_cache_key(klass, field)
92
+ raise InvalidCounterCacheError, "Missing `key #{field.to_sym.inspect}, Integer, :default => 0' on model #{klass}"
93
+ end
75
94
  end
76
95
  end
77
96
  end
@@ -32,14 +32,14 @@ module MongoMapper
32
32
  end
33
33
  end
34
34
 
35
- protected
35
+ protected
36
36
 
37
37
  # We don't call super here to avoid invoking #attributes, which builds a whole new hash per call.
38
38
  def attribute_method?(attr_name)
39
39
  keys.key?(attr_name) || !embedded_associations.detect {|a| a.name == attr_name }.nil?
40
40
  end
41
41
 
42
- private
42
+ private
43
43
 
44
44
  def write_key(key, value)
45
45
  key = unalias_key(key)
@@ -29,16 +29,17 @@ module MongoMapper
29
29
  end
30
30
  end
31
31
 
32
- protected
33
- def method_missing(method, *args, &block)
34
- finder = DynamicFinder.new(method)
35
-
36
- if finder.found?
37
- dynamic_find(finder, args)
38
- else
39
- super
40
- end
32
+ private
33
+
34
+ def method_missing(method, *args, &block)
35
+ finder = DynamicFinder.new(method)
36
+
37
+ if finder.found?
38
+ dynamic_find(finder, args)
39
+ else
40
+ super
41
41
  end
42
+ end
42
43
  end
43
44
  end
44
45
  end
@@ -20,24 +20,25 @@ module MongoMapper
20
20
  bang == true
21
21
  end
22
22
 
23
- protected
24
- def match
25
- case method.to_s
26
- when /^find_(all_by|by)_([_a-zA-Z]\w*)$/
27
- @finder = :all if $1 == 'all_by'
28
- names = $2
29
- when /^find_by_([_a-zA-Z]\w*)\!$/
30
- @bang = true
31
- names = $1
32
- when /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/
33
- @instantiator = $1 == 'initialize' ? :new : :create
34
- names = $2
35
- else
36
- @finder = nil
37
- end
23
+ protected
38
24
 
39
- @attributes = names && names.split('_and_')
25
+ def match
26
+ case method.to_s
27
+ when /^find_(all_by|by)_([_a-zA-Z]\w*)$/
28
+ @finder = :all if $1 == 'all_by'
29
+ names = $2
30
+ when /^find_by_([_a-zA-Z]\w*)\!$/
31
+ @bang = true
32
+ names = $1
33
+ when /^find_or_(initialize|create)_by_([_a-zA-Z]\w*)$/
34
+ @instantiator = $1 == 'initialize' ? :new : :create
35
+ names = $2
36
+ else
37
+ @finder = nil
40
38
  end
39
+
40
+ @attributes = names && names.split('_and_')
41
+ end
41
42
  end
42
43
  end
43
44
  end
@@ -8,7 +8,7 @@ module MongoMapper
8
8
  extend ::ActiveModel::Callbacks
9
9
 
10
10
  define_model_callbacks :save, :create, :update, :destroy, :only => [:before, :after]
11
- define_model_callbacks :touch, :only => [:after]
11
+ define_model_callbacks :initialize, :find, :touch, :only => [:after]
12
12
 
13
13
  proxy_callbacks(
14
14
  :before => [:save, :create, :update, :destroy],
@@ -69,7 +69,7 @@ module MongoMapper
69
69
  end
70
70
  end
71
71
 
72
- def load(attrs)
72
+ def load(attrs, with_cast = false)
73
73
  return super unless Thread.current[:mongo_mapper_identity_map_enabled]
74
74
  return nil unless attrs
75
75
  document = get_from_identity_map(attrs['_id'])
@@ -21,7 +21,7 @@ module MongoMapper
21
21
  collection.drop_indexes
22
22
  end
23
23
 
24
- private
24
+ private
25
25
 
26
26
  def dealias_options(options)
27
27
  case options
@@ -1,5 +1,6 @@
1
1
  # encoding: UTF-8
2
2
  require 'mongo_mapper/plugins/keys/key'
3
+ require 'mongo_mapper/plugins/keys/static'
3
4
 
4
5
  module MongoMapper
5
6
  module Plugins
@@ -57,7 +58,7 @@ module MongoMapper
57
58
  Key.new(*args).tap do |key|
58
59
  keys[key.name] = key
59
60
  keys[key.abbr] = key if key.abbr
60
- create_accessors_for(key) if key.valid_ruby_name?
61
+ create_accessors_for(key) if key.valid_ruby_name? && !key.reserved_name?
61
62
  create_key_in_descendants(*args)
62
63
  create_indexes_for(key)
63
64
  create_validations_for(key)
@@ -122,144 +123,144 @@ module MongoMapper
122
123
  end.allocate.initialize_from_database(attrs, with_cast)
123
124
  end
124
125
 
125
- private
126
- def key_accessors_module_defined?
127
- # :nocov:
128
- if IS_RUBY_1_9
129
- const_defined?('MongoMapperKeys')
130
- else
131
- const_defined?('MongoMapperKeys', false)
132
- end
133
- # :nocov:
134
- end
126
+ private
135
127
 
136
- def accessors_module
137
- if key_accessors_module_defined?
138
- const_get 'MongoMapperKeys'
139
- else
140
- const_set 'MongoMapperKeys', Module.new
141
- end
128
+ def key_accessors_module_defined?
129
+ # :nocov:
130
+ if IS_RUBY_1_9
131
+ const_defined?('MongoMapperKeys')
132
+ else
133
+ const_defined?('MongoMapperKeys', false)
142
134
  end
135
+ # :nocov:
136
+ end
143
137
 
144
- def create_accessors_for(key)
145
- accessors = ""
146
- if key.read_accessor?
147
- accessors << <<-end_eval
148
- def #{key.name}
149
- read_key(:#{key.name})
150
- end
151
-
152
- def #{key.name}_before_type_cast
153
- read_key_before_type_cast(:#{key.name})
154
- end
155
- end_eval
156
- end
157
-
158
- if key.write_accessor?
159
- accessors << <<-end_eval
160
- def #{key.name}=(value)
161
- write_key(:#{key.name}, value)
162
- end
163
- end_eval
164
- end
165
-
166
- if key.predicate_accessor?
167
- accessors << <<-end_eval
168
- def #{key.name}?
169
- read_key(:#{key.name}).present?
170
- end
171
- end_eval
172
- end
138
+ def accessors_module
139
+ if key_accessors_module_defined?
140
+ const_get 'MongoMapperKeys'
141
+ else
142
+ const_set 'MongoMapperKeys', Module.new
143
+ end
144
+ end
173
145
 
174
- if block_given?
175
- accessors_module.module_eval do
176
- yield
146
+ def create_accessors_for(key)
147
+ accessors = ""
148
+ if key.read_accessor?
149
+ accessors << <<-end_eval
150
+ def #{key.name}
151
+ read_key(:#{key.name})
177
152
  end
178
- end
179
153
 
180
- accessors_module.module_eval accessors
181
- include accessors_module
154
+ def #{key.name}_before_type_cast
155
+ read_key_before_type_cast(:#{key.name})
156
+ end
157
+ end_eval
182
158
  end
183
159
 
184
- def create_key_in_descendants(*args)
185
- descendants.each { |descendant| descendant.key(*args) }
160
+ if key.write_accessor?
161
+ accessors << <<-end_eval
162
+ def #{key.name}=(value)
163
+ write_key(:#{key.name}, value)
164
+ end
165
+ end_eval
186
166
  end
187
167
 
188
- def remove_key_in_descendants(name)
189
- descendants.each { |descendant| descendant.remove_key(name) }
168
+ if key.predicate_accessor?
169
+ accessors << <<-end_eval
170
+ def #{key.name}?
171
+ read_key(:#{key.name}).present?
172
+ end
173
+ end_eval
190
174
  end
191
175
 
192
- def create_indexes_for(key)
193
- if key.options[:index] && !key.embeddable?
194
- warn "[DEPRECATION] :index option when defining key #{key.name.inspect} is deprecated. Put indexes in `db/indexes.rb`"
195
- ensure_index key.name
176
+ if block_given?
177
+ accessors_module.module_eval do
178
+ yield
196
179
  end
197
180
  end
198
181
 
199
- def create_validations_for(key)
200
- attribute = key.name.to_sym
182
+ accessors_module.module_eval accessors
183
+ include accessors_module
184
+ end
201
185
 
202
- if key.options[:required]
203
- if key.type == Boolean
204
- validates_inclusion_of attribute, :in => [true, false]
205
- else
206
- validates_presence_of(attribute)
207
- end
208
- end
186
+ def create_key_in_descendants(*args)
187
+ descendants.each { |descendant| descendant.key(*args) }
188
+ end
209
189
 
210
- if key.options[:unique]
211
- validates_uniqueness_of(attribute)
212
- end
190
+ def remove_key_in_descendants(name)
191
+ descendants.each { |descendant| descendant.remove_key(name) }
192
+ end
213
193
 
214
- if key.options[:numeric]
215
- number_options = key.type == Integer ? {:only_integer => true} : {}
216
- validates_numericality_of(attribute, number_options)
217
- end
194
+ def create_indexes_for(key)
195
+ if key.options[:index] && !key.embeddable?
196
+ warn "[DEPRECATION] :index option when defining key #{key.name.inspect} is deprecated. Put indexes in `db/indexes.rb`"
197
+ ensure_index key.name
198
+ end
199
+ end
218
200
 
219
- if key.options[:format]
220
- validates_format_of(attribute, :with => key.options[:format])
221
- end
201
+ def create_validations_for(key)
202
+ attribute = key.name.to_sym
222
203
 
223
- if key.options[:in]
224
- validates_inclusion_of(attribute, :in => key.options[:in])
204
+ if key.options[:required]
205
+ if key.type == Boolean
206
+ validates_inclusion_of attribute, :in => [true, false]
207
+ else
208
+ validates_presence_of(attribute)
225
209
  end
210
+ end
226
211
 
227
- if key.options[:not_in]
228
- validates_exclusion_of(attribute, :in => key.options[:not_in])
229
- end
212
+ if key.options[:unique]
213
+ validates_uniqueness_of(attribute)
214
+ end
230
215
 
231
- if key.options[:length]
232
- length_options = case key.options[:length]
233
- when Integer
234
- {:minimum => 0, :maximum => key.options[:length]}
235
- when Range
236
- {:within => key.options[:length]}
237
- when Hash
238
- key.options[:length]
239
- end
240
- validates_length_of(attribute, length_options)
241
- end
216
+ if key.options[:numeric]
217
+ number_options = key.type == Integer ? {:only_integer => true} : {}
218
+ validates_numericality_of(attribute, number_options)
242
219
  end
243
220
 
244
- def remove_validations_for(name)
245
- name = name.to_sym
246
- a_name = [name]
221
+ if key.options[:format]
222
+ validates_format_of(attribute, :with => key.options[:format])
223
+ end
247
224
 
248
- _validators.reject!{ |key, _| key == name }
249
- remove_validate_callbacks a_name
225
+ if key.options[:in]
226
+ validates_inclusion_of(attribute, :in => key.options[:in])
250
227
  end
251
228
 
252
- def remove_validate_callbacks(a_name)
253
- chain = _validate_callbacks.dup.reject do |callback|
254
- f = callback.raw_filter
255
- f.respond_to?(:attributes) && f.attributes == a_name
256
- end
257
- reset_callbacks(:validate)
258
- chain.each do |callback|
259
- set_callback 'validate', callback.raw_filter
229
+ if key.options[:not_in]
230
+ validates_exclusion_of(attribute, :in => key.options[:not_in])
231
+ end
232
+
233
+ if key.options[:length]
234
+ length_options = case key.options[:length]
235
+ when Integer
236
+ {:minimum => 0, :maximum => key.options[:length]}
237
+ when Range
238
+ {:within => key.options[:length]}
239
+ when Hash
240
+ key.options[:length]
260
241
  end
242
+ validates_length_of(attribute, length_options)
261
243
  end
244
+ end
262
245
 
246
+ def remove_validations_for(name)
247
+ name = name.to_sym
248
+ a_name = [name]
249
+
250
+ _validators.reject!{ |key, _| key == name }
251
+ remove_validate_callbacks a_name
252
+ end
253
+
254
+ def remove_validate_callbacks(a_name)
255
+ chain = _validate_callbacks.dup.reject do |callback|
256
+ f = callback.raw_filter
257
+ f.respond_to?(:attributes) && f.attributes == a_name
258
+ end
259
+ reset_callbacks(:validate)
260
+ chain.each do |callback|
261
+ set_callback 'validate', callback.raw_filter
262
+ end
263
+ end
263
264
  end
264
265
 
265
266
  def initialize(attrs={})
@@ -372,8 +373,8 @@ module MongoMapper
372
373
  end
373
374
  end
374
375
 
375
- alias_method :[], :read_key
376
- alias_method :attribute, :read_key
376
+ def [](key_name); read_key(key_name); end
377
+ def attribute(key_name); read_key(key_name); end
377
378
 
378
379
  def []=(name, value)
379
380
  write_key(name, value)
@@ -391,7 +392,7 @@ module MongoMapper
391
392
  @embedded_keys ||= keys.values.select(&:embeddable?)
392
393
  end
393
394
 
394
- protected
395
+ protected
395
396
 
396
397
  def unalias_key(name)
397
398
  name = name.to_s
@@ -402,68 +403,67 @@ module MongoMapper
402
403
  end
403
404
  end
404
405
 
405
- private
406
+ private
406
407
 
407
- def init_ivars
408
- @__mm_keys = self.class.keys # Not dumpable
409
- @__mm_default_keys = @__mm_keys.values.select(&:default?) # Not dumpable
410
- @_dynamic_attributes = {} # Dumpable
411
- end
408
+ def init_ivars
409
+ @__mm_keys = self.class.keys # Not dumpable
410
+ @__mm_default_keys = @__mm_keys.values.select(&:default?) # Not dumpable
411
+ @_dynamic_attributes = {} # Dumpable
412
+ end
412
413
 
413
- def load_from_database(attrs, with_cast = false)
414
- return if attrs == nil || attrs.blank?
414
+ def load_from_database(attrs, with_cast = false)
415
+ return if attrs == nil || attrs.blank?
415
416
 
416
- attrs.each do |key, value|
417
- if !@__mm_keys.key?(key) && respond_to?(:"#{key}=")
418
- self.send(:"#{key}=", value)
419
- else
420
- internal_write_key key, value, with_cast
421
- end
417
+ attrs.each do |key, value|
418
+ if !@__mm_keys.key?(key) && respond_to?(:"#{key}=")
419
+ self.send(:"#{key}=", value)
420
+ else
421
+ internal_write_key key, value, with_cast
422
422
  end
423
423
  end
424
+ end
424
425
 
425
- def set_parent_document(key, value)
426
- if key.type and value.instance_of?(key.type) && key.embeddable? && value.respond_to?(:_parent_document)
427
- value._parent_document = self
428
- end
426
+ def set_parent_document(key, value)
427
+ if key.type and value.instance_of?(key.type) && key.embeddable? && value.respond_to?(:_parent_document)
428
+ value._parent_document = self
429
429
  end
430
+ end
430
431
 
431
- # This exists to be patched over by plugins, while letting us still get to the undecorated
432
- # version of the method.
433
- def write_key(name, value)
434
- init_ivars unless @__mm_keys
435
- internal_write_key(name.to_s, value)
436
- end
432
+ # This exists to be patched over by plugins, while letting us still get to the undecorated
433
+ # version of the method.
434
+ def write_key(name, value)
435
+ init_ivars unless @__mm_keys
436
+ internal_write_key(name.to_s, value)
437
+ end
437
438
 
438
- def internal_write_key(name, value, cast = true)
439
- key = @__mm_keys[name] || dynamic_key(name)
440
- as_mongo = cast ? key.set(value) : value
441
- as_typecast = key.get(as_mongo)
442
- if key.ivar
443
- if key.embeddable?
444
- set_parent_document(key, value)
445
- set_parent_document(key, as_typecast)
446
- end
447
- instance_variable_set key.ivar, as_typecast
448
- else
449
- @_dynamic_attributes[key.name.to_sym] = as_typecast
439
+ def internal_write_key(name, value, cast = true)
440
+ key = @__mm_keys[name] || dynamic_key(name)
441
+ as_mongo = cast ? key.set(value) : value
442
+ as_typecast = key.get(as_mongo)
443
+ if key.ivar
444
+ if key.embeddable?
445
+ set_parent_document(key, value)
446
+ set_parent_document(key, as_typecast)
450
447
  end
451
- @attributes = nil
452
- value
448
+ instance_variable_set key.ivar, as_typecast
449
+ else
450
+ @_dynamic_attributes[key.name.to_sym] = as_typecast
453
451
  end
452
+ @attributes = nil
453
+ value
454
+ end
454
455
 
455
- def dynamic_key(name)
456
- self.class.key(name, :__dynamic => true)
457
- end
456
+ def dynamic_key(name)
457
+ self.class.key(name, :__dynamic => true)
458
+ end
458
459
 
459
- def initialize_default_values(except = {})
460
- @__mm_default_keys.each do |key|
461
- if !(except && except.key?(key.name))
462
- internal_write_key key.name, key.default_value, false
463
- end
460
+ def initialize_default_values(except = {})
461
+ @__mm_default_keys.each do |key|
462
+ if !(except && except.key?(key.name))
463
+ internal_write_key key.name, key.default_value, false
464
464
  end
465
465
  end
466
- #end private
466
+ end
467
467
  end
468
468
  end
469
469
  end