module MongoMapper::Plugins::Keys
Constants
- IS_RUBY_1_9
Public Class Methods
new(attrs={}) { |self| ... }
click to toggle source
# 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
Public Instance Methods
[](key_name)
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 394 def [](key_name); read_key(key_name); end
[]=(name, value)
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 397 def []=(name, value) write_key(name, value) end
assign(attrs={})
click to toggle source
# 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)
click to toggle source
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=`
# File lib/mongo_mapper/plugins/keys.rb, line 312 def assign_attributes(new_attributes) self.attributes = new_attributes end
attribute(key_name)
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 395 def attribute(key_name); read_key(key_name); end
attributes()
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 337 def attributes to_mongo(false).with_indifferent_access end
attributes=(attrs)
click to toggle source
# 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()
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 409 def embedded_keys @embedded_keys ||= keys.values.select(&:embeddable?) end
id()
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 361 def id self[:_id] end
id=(value)
click to toggle source
# 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_from_database(attrs={}, with_cast = false)
click to toggle source
# 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()
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 401 def key_names @key_names ||= keys.keys end
keys()
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 373 def keys self.class.keys end
non_embedded_keys()
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 405 def non_embedded_keys @non_embedded_keys ||= keys.values.select { |key| !key.embeddable? } end
persisted?()
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 292 def persisted? !new? && !destroyed? end
read_key(key_name)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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={})
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 346 def update_attributes(attrs={}) self.attributes = attrs save end
update_attributes!(attrs={})
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 351 def update_attributes!(attrs={}) self.attributes = attrs save! end
Protected Instance Methods
unalias_key(name)
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 415 def unalias_key(name) name = name.to_s if key = keys[name] key.name else name end end
Private Instance Methods
dynamic_key(name)
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 473 def dynamic_key(name) self.class.key(name, :__dynamic => true) end
init_ivars()
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 426 def init_ivars @__mm_keys = self.class.keys # Not dumpable @__mm_default_keys = @__mm_keys.values.select(&:default?) # Not dumpable @_dynamic_attributes = {} # Dumpable end
initialize_default_values(except = {})
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 477 def initialize_default_values(except = {}) @__mm_default_keys.each do |key| if !(except && except.key?(key.name)) value = key.default_value value = key.type ? key.type.to_mongo(value) : value internal_write_key key.name, value, false end end end
internal_write_key(name, value, cast = true)
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 457 def internal_write_key(name, value, cast = true) key = @__mm_keys[name] || dynamic_key(name) as_mongo = cast ? key.set(value) : value as_typecast = key.get(as_mongo) if key.ivar if key.embeddable? set_parent_document(key, value) set_parent_document(key, as_typecast) end instance_variable_set key.ivar, as_typecast else @_dynamic_attributes[key.name.to_sym] = as_typecast end value end
load_from_database(attrs, with_cast = false)
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 432 def load_from_database(attrs, with_cast = false) return if attrs == nil || attrs.blank? attrs.each do |key, value| if !@__mm_keys.key?(key) && respond_to?(:"#{key}=") self.send(:"#{key}=", value) else internal_write_key key, value, with_cast end end end
set_parent_document(key, value)
click to toggle source
# File lib/mongo_mapper/plugins/keys.rb, line 444 def set_parent_document(key, value) if key.type and value.kind_of?(key.type) && key.embeddable? && value.respond_to?(:_parent_document) value._parent_document = self end end
write_key(name, value)
click to toggle source
This exists to be patched over by plugins, while letting us still get to the undecorated version of the method.
# File lib/mongo_mapper/plugins/keys.rb, line 452 def write_key(name, value) init_ivars unless @__mm_keys internal_write_key(name.to_s, value) end