Sha256: 64276a3193581d2cca6b665645b17d287b5e396fc447f53fbd338548b69eaf3e
Contents?: true
Size: 1.47 KB
Versions: 6
Compression:
Stored size: 1.47 KB
Contents
# encoding: utf-8 module Mongoid # 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 ] options The mass assignment scoping options. # # @return [ Document ] The instantiated document. def build(klass, attributes = nil) type = (attributes || {})["_type"] if type && klass._types.include?(type) type.constantize.new(attributes) else klass.new(attributes) 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 = nil, criteria_instance_id = nil) type = (attributes || {})["_type"] if type.blank? klass.instantiate(attributes, criteria_instance_id) else type.camelize.constantize.instantiate(attributes, criteria_instance_id) end end end end
Version data entries
6 entries across 6 versions & 4 rubygems