lib/mongoid/factory.rb in mongoid-2.0.1 vs lib/mongoid/factory.rb in mongoid-2.0.2
- old
+ new
@@ -1,20 +1,37 @@
# encoding: utf-8
module Mongoid #:nodoc:
- class Factory #:nodoc:
+
+ # Instantiates documents that came from the database.
+ module Factory
+ extend self
+
# Builds a new +Document+ from the supplied attributes.
#
- # Example:
+ # @example Build the document.
+ # Mongoid::Factory.build(Person, { "name" => "Durran" })
#
- # <tt>Mongoid::Factory.build(Person, {})</tt>
+ # @param [ Class ] klass The class to instantiate from if _type is not present.
+ # @param [ Hash ] attributes The document attributes.
#
- # Options:
+ # @return [ Document ] The instantiated document.
+ def build(klass, attributes = {})
+ type = (attributes || {})["_type"]
+ type.blank? ? klass.new(attributes) : type.constantize.new(attributes)
+ end
+
+ # Builds a new +Document+ from the supplied attributes loaded from the
+ # database.
#
- # klass: The class to instantiate from if _type is not present.
- # attributes: The +Document+ attributes.
- def self.build(klass, attributes)
- attrs = {}.merge(attributes)
- type = attrs["_type"]
- type.present? ? type.constantize.instantiate(attrs) : klass.instantiate(attrs)
+ # @example Build the document.
+ # Mongoid::Factory.from_db(Person, { "name" => "Durran" })
+ #
+ # @param [ Class ] klass The class to instantiate from if _type is not present.
+ # @param [ Hash ] attributes The document attributes.
+ #
+ # @return [ Document ] The instantiated document.
+ def from_db(klass, attributes = {})
+ type = attributes["_type"]
+ type.blank? ? klass.instantiate(attributes) : type.constantize.instantiate(attributes)
end
end
end