This is the base module for all domain objects that need to be persisted to the database as documents.
Default comparison is via the string version of the id.
@example Compare two documents.
person <=> other_person
@param [ Document ] other The document to compare with.
@return [ Integer ] -1, 0, 1.
# File lib/mongoid/document.rb, line 19 def <=>(other) attributes["_id"].to_s <=> other.attributes["_id"].to_s end
Performs equality checking on the document ids. For more robust equality checking please override this method.
@example Compare for equality.
document == other
@param [ Document, Object ] other The other object to compare with.
@return [ true, false ] True if the ids are equal, false if not.
# File lib/mongoid/document.rb, line 32 def ==(other) self.class == other.class && attributes["_id"] == other.attributes["_id"] end
Performs class equality checking.
@example Compare the classes.
document === other
@param [ Document, Object ] other The other object to compare with.
@return [ true, false ] True if the classes are equal, false if not.
# File lib/mongoid/document.rb, line 45 def ===(other) other.is_a?(self.class) end
Used in conjunction with fields_for to build a form element for the destruction of this association. Always returns false because Mongoid only supports immediate deletion of associations.
See ActionView::Helpers::FormHelper::fields_for for more info.
# File lib/mongoid/railties/document.rb, line 8 def _destroy false end
Return a hash of the entire document hierarchy from this document and below. Used when the attributes are needed for everything and not just the current document.
@example Get the full hierarchy.
person.as_document
@return [ Hash ] A hash of all attributes in the hierarchy.
# File lib/mongoid/document.rb, line 157 def as_document attributes.tap do |attrs| relations.each_pair do |name, meta| if meta.embedded? relation = send(name) attrs[name] = relation.as_document unless relation.blank? end end end end
Returns an instance of the specified class with the attributes and errors of the current document.
@example Return a subclass document as a superclass instance.
manager.becomes(Person)
@raise [ ArgumentError ] If the class doesn’t include Mongoid::Document
@param [ Class ] klass The class to become.
@return [ Document ] An instance of the specified class.
# File lib/mongoid/document.rb, line 179 def becomes(klass) unless klass.include?(Mongoid::Document) raise ArgumentError, "A class which includes Mongoid::Document is expected" end klass.instantiate(frozen? ? attributes.dup : attributes).tap do |became| became.instance_variable_set(:@errors, errors) became.instance_variable_set(:@new_record, new_record?) became.instance_variable_set(:@destroyed, destroyed?) became._type = klass.to_s end end
Delegates to ==. Used when needing checks in hashes.
@example Perform equality checking.
document.eql?(other)
@param [ Document, Object ] other The object to check against.
@return [ true, false ] True if equal, false if not.
# File lib/mongoid/document.rb, line 57 def eql?(other) self == (other) end
Freezes the internal attributes of the document.
@example Freeze the document
document.freeze
@return [ Document ] The document.
@since 2.0.0
# File lib/mongoid/document.rb, line 69 def freeze attributes.freeze self end
Checks if the document is frozen
@example Check if frozen
document.frozen?
@return [ true, false ] True if frozen, else false.
@since 2.0.0
# File lib/mongoid/document.rb, line 82 def frozen? raw_attributes.frozen? end
Delegates to id in order to allow two records of the same type and id to work with something like:
[ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
@example Get the hash.
document.hash
@return [ Integer ] The hash of the document’s id.
# File lib/mongoid/document.rb, line 96 def hash raw_attributes["_id"].hash end
Generate an id for this Document.
@example Create the id.
person.identify
@return [ BSON::ObjectId, String ] A newly created id.
# File lib/mongoid/document.rb, line 106 def identify Identity.new(self).create end
Instantiate a new Document, setting the Document’s attributes if given. If no attributes are provided, they will be initialized with an empty Hash.
If a primary key is defined, the document’s id will be set to that key, otherwise it will be set to a fresh +BSON::ObjectId+ string.
@example Create a new document.
Person.new(:title => "Sir")
@param [ Hash ] attrs The attributes to set up the document with. @param [ Hash ] options A mass-assignment protection options. Supports
:as and :without_protection
@return [ Document ] A new document.
# File lib/mongoid/document.rb, line 125 def initialize(attrs = nil, options = nil) _building do @new_record = true @attributes ||= {} options ||= {} process(attrs, options[:as] || :default, !options[:without_protection]) do identify apply_defaults yield(self) if block_given? end run_callbacks(:initialize) { self } end end
Generated with the Darkfish Rdoc Generator 2.