Module: MongoMapper::Plugins::Keys

Extended by:
ActiveSupport::Concern
Included in:
Document, EmbeddedDocument
Defined in:
lib/mongo_mapper/plugins/keys.rb,
lib/mongo_mapper/plugins/keys/key.rb

Defined Under Namespace

Modules: ClassMethods Classes: Key

Constant Summary

IS_RUBY_1_9 =
method(:const_defined?).arity == 1

Instance Method Summary (collapse)

Instance Method Details

- (Object) []=(name, value)



366
367
368
# File 'lib/mongo_mapper/plugins/keys.rb', line 366

def []=(name, value)
  write_key(name, value)
end

- (Object) assign(attrs = {})



310
311
312
313
# File 'lib/mongo_mapper/plugins/keys.rb', line 310

def assign(attrs={})
  warn "[DEPRECATION] #assign is deprecated, use #attributes="
  self.attributes = attrs
end

- (Object) attributes



306
307
308
# File 'lib/mongo_mapper/plugins/keys.rb', line 306

def attributes
  to_mongo(false).with_indifferent_access
end

- (Object) attributes=(attrs)



273
274
275
276
277
278
279
280
281
282
283
# File 'lib/mongo_mapper/plugins/keys.rb', line 273

def attributes=(attrs)
  return if attrs == nil || attrs.blank?

  attrs.each_pair do |key, value|
    if respond_to?(:#{key}=")
      self.send(:#{key}=", value)
    else
      self[key] = value
    end
  end
end

- (Object) embedded_keys



378
379
380
# File 'lib/mongo_mapper/plugins/keys.rb', line 378

def embedded_keys
  @embedded_keys ||= keys.values.select(&:embeddable?)
end

- (Object) id



330
331
332
# File 'lib/mongo_mapper/plugins/keys.rb', line 330

def id
  self[:_id]
end

- (Object) id=(value)



334
335
336
337
338
339
340
# File 'lib/mongo_mapper/plugins/keys.rb', line 334

def id=(value)
  if self.class.using_object_id?
    value = ObjectId.to_mongo(value)
  end

  self[:_id] = value
end

- (Object) initialize(attrs = {}) {|_self| ... }

Yields:

  • (_self)

Yield Parameters:



253
254
255
256
257
258
259
# File 'lib/mongo_mapper/plugins/keys.rb', line 253

def initialize(attrs={})
  @_new = true
  init_ivars
  initialize_default_values(attrs)
  self.attributes = attrs
  yield self if block_given?
end

- (Object) initialize_from_database(attrs = {}, with_cast = false)



261
262
263
264
265
266
267
# File 'lib/mongo_mapper/plugins/keys.rb', line 261

def initialize_from_database(attrs={}, with_cast = false)
  @_new = false
  init_ivars
  initialize_default_values(attrs)
  load_from_database(attrs, with_cast)
  self
end

- (Object) key_names



370
371
372
# File 'lib/mongo_mapper/plugins/keys.rb', line 370

def key_names
  @key_names ||= keys.keys
end

- (Object) keys



342
343
344
# File 'lib/mongo_mapper/plugins/keys.rb', line 342

def keys
  self.class.keys
end

- (Object) non_embedded_keys



374
375
376
# File 'lib/mongo_mapper/plugins/keys.rb', line 374

def non_embedded_keys
  @non_embedded_keys ||= keys.values.select { |key| !key.embeddable? }
end

- (Boolean) persisted?

Returns:



269
270
271
# File 'lib/mongo_mapper/plugins/keys.rb', line 269

def persisted?
  !new? && !destroyed?
end

- (Object) read_key(key_name) Also known as: [], attribute



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/mongo_mapper/plugins/keys.rb', line 346

def read_key(key_name)
  key_name_sym = key_name.to_sym
  if @_dynamic_attributes && @_dynamic_attributes.key?(key_name_sym)
    @_dynamic_attributes[key_name_sym]
  elsif key = keys[key_name.to_s]
    if key.ivar && instance_variable_defined?(key.ivar)
      value = instance_variable_get(key.ivar)
    else
      if key.ivar
        instance_variable_set key.ivar, key.get(nil)
      else
        @_dynamic_attributes[key_name_sym] = key.get(nil)
      end
    end
  end
end

- (Object) to_mongo(include_abbreviatons = true)



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/mongo_mapper/plugins/keys.rb', line 285

def to_mongo(include_abbreviatons = true)
  BSON::OrderedHash.new.tap do |attrs|
    self.class.unaliased_keys.each do |name, key|
      value = self.read_key(key.name)
      if key.type == ObjectId || !value.nil?
        attrs[include_abbreviatons && key.persisted_name || name] = key.set(value)
      end
    end

    embedded_associations.each do |association|
      if documents = instance_variable_get(association.ivar)
        if association.instance_of?(Associations::OneAssociation)
          attrs[association.name] = documents.to_mongo
        else
          attrs[association.name] = documents.map(&:to_mongo)
        end
      end
    end
  end
end

- (Object) update_attribute(name, value)



325
326
327
328
# File 'lib/mongo_mapper/plugins/keys.rb', line 325

def update_attribute(name, value)
  self.send(:#{name}=", value)
  save(:validate => false)
end

- (Object) update_attributes(attrs = {})



315
316
317
318
# File 'lib/mongo_mapper/plugins/keys.rb', line 315

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

- (Object) update_attributes!(attrs = {})



320
321
322
323
# File 'lib/mongo_mapper/plugins/keys.rb', line 320

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