lib/mongo_mapper/plugins/keys.rb in mongo_mapper-0.7.6 vs lib/mongo_mapper/plugins/keys.rb in mongo_mapper-0.8.0
- old
+ new
@@ -1,17 +1,15 @@
+# encoding: UTF-8
module MongoMapper
module Plugins
module Keys
- autoload :Key, 'mongo_mapper/plugins/keys/key'
-
def self.configure(model)
model.key :_id, ObjectId
end
module ClassMethods
def inherited(descendant)
- key :_type, String unless keys.keys.include?(:_type)
descendant.instance_variable_set(:@keys, keys.dup)
super
end
def keys
@@ -157,18 +155,16 @@
module InstanceMethods
def initialize(attrs={}, from_database=false)
default_id_value(attrs)
if from_database
- @new = false
+ @_new = false
load_from_database(attrs)
else
- @new = true
+ @_new = true
assign(attrs)
end
-
- assign_type
end
def persisted?
!new? && !destroyed?
end
@@ -184,28 +180,26 @@
end
end
end
def attributes
- attrs = HashWithIndifferentAccess.new
+ HashWithIndifferentAccess.new.tap do |attrs|
+ keys.each_pair do |name, key|
+ value = key.set(self[key.name])
+ attrs[name] = value
+ end
- keys.each_pair do |name, key|
- value = key.set(self[key.name])
- attrs[name] = value
- end
-
- embedded_associations.each do |association|
- if documents = instance_variable_get(association.ivar)
- if association.one?
- attrs[association.name] = documents.to_mongo
- else
- attrs[association.name] = documents.map { |document| document.to_mongo }
+ embedded_associations.each do |association|
+ if documents = instance_variable_get(association.ivar)
+ if association.one?
+ attrs[association.name] = documents.to_mongo
+ else
+ attrs[association.name] = documents.map { |document| document.to_mongo }
+ end
end
end
end
-
- attrs
end
alias :to_mongo :attributes
def assign(attrs={})
self.attributes = attrs
@@ -277,14 +271,10 @@
write_key :_id, BSON::ObjectID.new
end
end
end
- def assign_type
- self._type = self.class.name if respond_to?(:_type=)
- end
-
def ensure_key_exists(name)
self.class.key(name) unless respond_to?("#{name}=")
end
def set_parent_document(key, value)
@@ -312,10 +302,8 @@
set_parent_document(key, value)
instance_variable_set :"@#{name}_before_typecast", value
instance_variable_set :"@#{name}", key.set(value)
end
end
-
-
end
end
end