Methods

Class/Module Index [+]

Quicksearch

Mongoid::Factory

Instantiates documents that came from the database.

Public Instance Methods

build(klass, attributes = {}, options = {}) click to toggle source

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.

# File lib/mongoid/factory.rb, line 17
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
from_db(klass, attributes = {}) click to toggle source

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.

# File lib/mongoid/factory.rb, line 36
def from_db(klass, attributes = {})
  type = attributes["_type"]
  type.blank? ? klass.instantiate(attributes) : type.constantize.instantiate(attributes)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.