Module: MongoMapper::EmbeddedDocument::InstanceMethods

Defined in:
lib/mongo_mapper/embedded_document.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



279
280
281
# File 'lib/mongo_mapper/embedded_document.rb', line 279

def ==(other)
  other.is_a?(self.class) && _id == other._id
end

#[](name) ⇒ Object



270
271
272
# File 'lib/mongo_mapper/embedded_document.rb', line 270

def [](name)
  read_attribute(name)
end

#[]=(name, value) ⇒ Object



274
275
276
277
# File 'lib/mongo_mapper/embedded_document.rb', line 274

def []=(name, value)
  ensure_key_exists(name)
  write_attribute(name, value)
end

#attributesObject



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/mongo_mapper/embedded_document.rb', line 227

def attributes
  attrs = HashWithIndifferentAccess.new
  
  embedded_keys.each do |key|
    attrs[key.name] = read_attribute(key.name).try(:attributes)
  end
  
  non_embedded_keys.each do |key|
    attrs[key.name] = read_attribute(key.name)
  end
  
  embedded_associations.each do |association|
    documents = instance_variable_get(association.ivar)
    next if documents.nil?
    attrs[association.name] = documents.collect { |doc| doc.attributes }
  end
  
  attrs
end

#attributes=(attrs) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/mongo_mapper/embedded_document.rb', line 214

def attributes=(attrs)
  return if attrs.blank?
  attrs.each_pair do |name, value|
    writer_method = "#{name}="

    if respond_to?(writer_method)
      self.send(writer_method, value)
    else
      self[name.to_s] = value
    end
  end
end

#cloneObject



264
265
266
267
268
# File 'lib/mongo_mapper/embedded_document.rb', line 264

def clone
  clone_attributes = self.attributes
  clone_attributes.delete("_id")
  self.class.new(clone_attributes)
end

#idObject



283
284
285
# File 'lib/mongo_mapper/embedded_document.rb', line 283

def id
  self[:_id]
end

#id=(value) ⇒ Object



287
288
289
290
291
292
293
294
295
# File 'lib/mongo_mapper/embedded_document.rb', line 287

def id=(value)
  if self.class.using_object_id?
    value = MongoMapper.normalize_object_id(value)
  else
    @using_custom_id = true
  end
  
  self[:_id] = value
end

#initialize(attrs = {}) ⇒ Object



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
201
202
203
204
# File 'lib/mongo_mapper/embedded_document.rb', line 175

def initialize(attrs={})
  unless attrs.nil?
    associations.each do |name, association|
      if collection = attrs.delete(name)
        if association.many? && association.klass.embeddable?
          root_document = attrs[:_root_document] || self
          collection.each do |doc|
            doc[:_root_document] = root_document
          end
        end
        send("#{association.name}=", collection)
      end
    end

    self.attributes = attrs
    
    if respond_to?(:_type=) && self['_type'].blank?
      self._type = self.class.name
    end
  end

  if self.class.embeddable?
    if read_attribute(:_id).blank?
      write_attribute :_id, Mongo::ObjectID.new
      @new_document = true
    else
      @new_document = false
    end
  end
end

#inspectObject



301
302
303
304
305
306
# File 'lib/mongo_mapper/embedded_document.rb', line 301

def inspect
  attributes_as_nice_string = key_names.collect do |name|
    "#{name}: #{read_attribute(name).inspect}"
  end.join(", ")
  "#<#{self.class} #{attributes_as_nice_string}>"
end

#loggerObject



326
327
328
# File 'lib/mongo_mapper/embedded_document.rb', line 326

def logger
  self.class.logger
end

#new?Boolean

Returns:



206
207
208
# File 'lib/mongo_mapper/embedded_document.rb', line 206

def new?
  !!@new_document
end

#save(options = {}) ⇒ Object



308
309
310
# File 'lib/mongo_mapper/embedded_document.rb', line 308

def save(options={})
  _root_document.try(:save, options)
end

#save!(options = {}) ⇒ Object



312
313
314
# File 'lib/mongo_mapper/embedded_document.rb', line 312

def save!(options={})
  _root_document.try(:save!, options)
end

#to_mongoObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/mongo_mapper/embedded_document.rb', line 247

def to_mongo
  attrs = HashWithIndifferentAccess.new
  
  _keys.each_pair do |name, key|
    value = key.set(read_attribute(key.name))
    attrs[name] = value unless value.nil?
  end
  
  embedded_associations.each do |association|
    if documents = instance_variable_get(association.ivar)
      attrs[association.name] = documents.map { |document| document.to_mongo }
    end
  end
  
  attrs
end

#to_paramObject



210
211
212
# File 'lib/mongo_mapper/embedded_document.rb', line 210

def to_param
  id.to_s
end

#update_attributes(attrs = {}) ⇒ Object



316
317
318
319
# File 'lib/mongo_mapper/embedded_document.rb', line 316

def update_attributes(attrs={})
  self.attributes = attrs
  save
end

#update_attributes!(attrs = {}) ⇒ Object



321
322
323
324
# File 'lib/mongo_mapper/embedded_document.rb', line 321

def update_attributes!(attrs={})
  self.attributes = attrs
  save!
end

#using_custom_id?Boolean

Returns:



297
298
299
# File 'lib/mongo_mapper/embedded_document.rb', line 297

def using_custom_id?
  !!@using_custom_id
end