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,
 lib/mongo_mapper/plugins/keys/static.rb
 
Defined Under Namespace
  
    
      Modules: ClassMethods, Static
    
  
    
      Classes: Key
    
  
  
    
      Constant Summary
      collapse
    
    
      
        - IS_RUBY_1_9 =
          
        
- method(:const_defined?).arity == 1 
      Instance Method Summary
      collapse
    
    
  
  
  
    Instance Method Details
    
      
  
  
    #[](key_name)  ⇒ Object 
  
  
  
  
    | 
394 | # File 'lib/mongo_mapper/plugins/keys.rb', line 394
def [](key_name); read_key(key_name); end | 
 
    
      
  
  
    #[]=(name, value)  ⇒ Object 
  
  
  
  
    | 
397
398
399 | # File 'lib/mongo_mapper/plugins/keys.rb', line 397
def []=(name, value)
  write_key(name, value)
end | 
 
    
      
  
  
    #assign(attrs = {})  ⇒ Object 
  
  
  
  
    | 
341
342
343
344 | # File 'lib/mongo_mapper/plugins/keys.rb', line 341
def assign(attrs={})
  warn "[DEPRECATION] #assign is deprecated, use #attributes="
  self.attributes = attrs
end | 
 
    
      
  
  
    #assign_attributes(new_attributes)  ⇒ Object 
  
  
  
  
    
NOTE: We can’t use alias_method here as we need the #attributes= superclass method to get called (for example: MongoMapper::Plugins::Accessible filters non-permitted parameters through ‘attributes=`
   
 
  
  
    | 
312
313
314 | # File 'lib/mongo_mapper/plugins/keys.rb', line 312
def assign_attributes(new_attributes)
  self.attributes = new_attributes
end | 
 
    
      
  
  
    #attribute(key_name)  ⇒ Object 
  
  
  
  
    | 
395 | # File 'lib/mongo_mapper/plugins/keys.rb', line 395
def attribute(key_name); read_key(key_name); end | 
 
    
      
  
  
    #attributes  ⇒ Object 
  
  
  
  
    | 
337
338
339 | # File 'lib/mongo_mapper/plugins/keys.rb', line 337
def attributes
  to_mongo(false).with_indifferent_access
end | 
 
    
      
  
  
    #attributes=(attrs)  ⇒ Object 
  
  
  
  
    | 
296
297
298
299
300
301
302
303
304
305
306 | # File 'lib/mongo_mapper/plugins/keys.rb', line 296
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 | 
 
    
      
  
  
    #embedded_keys  ⇒ Object 
  
  
  
  
    | 
409
410
411 | # File 'lib/mongo_mapper/plugins/keys.rb', line 409
def embedded_keys
  @embedded_keys ||= keys.values.select(&:embeddable?)
end | 
 
    
      
  
  
    | 
361
362
363 | # File 'lib/mongo_mapper/plugins/keys.rb', line 361
def id
  self[:_id]
end | 
 
    
      
  
  
    #id=(value)  ⇒ Object 
  
  
  
  
    | 
365
366
367
368
369
370
371 | # File 'lib/mongo_mapper/plugins/keys.rb', line 365
def id=(value)
  if self.class.using_object_id?
    value = ObjectId.to_mongo(value)
  end
  self[:_id] = value
end | 
 
    
      
  
  
    #initialize(attrs = {}) {|_self| ... } ⇒ Object 
  
  
  
  
    | 
276
277
278
279
280
281
282 | # File 'lib/mongo_mapper/plugins/keys.rb', line 276
def initialize(attrs={})
  @_new = true
  init_ivars
  initialize_default_values(attrs)
  self.attributes = attrs
  yield self if block_given?
end | 
 
    
      
  
  
    #initialize_from_database(attrs = {}, with_cast = false)  ⇒ Object 
  
  
  
  
    | 
284
285
286
287
288
289
290 | # File 'lib/mongo_mapper/plugins/keys.rb', line 284
def initialize_from_database(attrs={}, with_cast = false)
  @_new = false
  init_ivars
  initialize_default_values(attrs)
  load_from_database(attrs, with_cast)
  self
end | 
 
    
      
  
  
    #key_names  ⇒ Object 
  
  
  
  
    | 
401
402
403 | # File 'lib/mongo_mapper/plugins/keys.rb', line 401
def key_names
  @key_names ||= keys.keys
end | 
 
    
      
  
  
    | 
373
374
375 | # File 'lib/mongo_mapper/plugins/keys.rb', line 373
def keys
  self.class.keys
end | 
 
    
      
  
  
    #non_embedded_keys  ⇒ Object 
  
  
  
  
    | 
405
406
407 | # File 'lib/mongo_mapper/plugins/keys.rb', line 405
def non_embedded_keys
  @non_embedded_keys ||= keys.values.select { |key| !key.embeddable? }
end | 
 
    
      
  
  
    | 
292
293
294 | # File 'lib/mongo_mapper/plugins/keys.rb', line 292
def persisted?
  !new? && !destroyed?
end | 
 
    
      
  
  
    #read_key(key_name)  ⇒ Object 
  
  
  
  
    | 
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392 | # File 'lib/mongo_mapper/plugins/keys.rb', line 377
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 | 
 
    
      
  
  
    #to_mongo(include_abbreviatons = true)  ⇒ Object 
  
  
  
  
    | 
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335 | # File 'lib/mongo_mapper/plugins/keys.rb', line 316
def to_mongo(include_abbreviatons = true)
  Hash.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.is_a?(Associations::OneAssociation)
          attrs[association.name] = documents.to_mongo
        else
          attrs[association.name] = documents.map(&:to_mongo)
        end
      end
    end
  end
end | 
 
    
      
  
  
    #update_attribute(name, value)  ⇒ Object 
  
  
  
  
    | 
356
357
358
359 | # File 'lib/mongo_mapper/plugins/keys.rb', line 356
def update_attribute(name, value)
  self.send(:"#{name}=", value)
  save(:validate => false)
end | 
 
    
      
  
  
    #update_attributes(attrs = {})  ⇒ Object 
  
  
  
  
    | 
346
347
348
349 | # File 'lib/mongo_mapper/plugins/keys.rb', line 346
def update_attributes(attrs={})
  self.attributes = attrs
  save
end | 
 
    
      
  
  
    #update_attributes!(attrs = {})  ⇒ Object 
  
  
  
  
    | 
351
352
353
354 | # File 'lib/mongo_mapper/plugins/keys.rb', line 351
def update_attributes!(attrs={})
  self.attributes = attrs
  save!
end |