Sha256: 472351f3e15f0f76bfc5527321fd5d2a327ea16d901d416e42f97b26c6dce83e
Contents?: true
Size: 1.38 KB
Versions: 4
Compression:
Stored size: 1.38 KB
Contents
# encoding: utf-8 module Mongoid #:nodoc: # Instantiates documents that came from the database. module Factory extend self # Builds a new +Document+ from the supplied attributes. # # @example Build the document. # Mongoid::Factory.build(Person, { "name" => "Durran" }) # # @param [ Class ] klass The class to instantiate from if _type is not present. # @param [ Hash ] attributes The document attributes. # @param [ Hash ] optiosn The mass assignment scoping options. # # @return [ Document ] The instantiated document. def build(klass, attributes = {}, options = {}) type = (attributes || {})["_type"] if type && klass._types.include?(type) type.constantize.new(attributes, options) else klass.new(attributes, options) end end # Builds a new +Document+ from the supplied attributes loaded from the # database. # # @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
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
mongoid-2.3.3 | lib/mongoid/factory.rb |
mongoid-2.3.2 | lib/mongoid/factory.rb |
mongoid-2.3.1 | lib/mongoid/factory.rb |
mongoid-2.3.0 | lib/mongoid/factory.rb |