Module: Mongoid::Document::ClassMethods

Defined in:
lib/mongoid/document.rb

Instance Method Summary collapse

Instance Method Details

#===(other) ⇒ true, false

Performs class equality checking.

Examples:

Compare the classes.

document === other

Parameters:

  • other (Document, Object)

    The other object to compare with.

Returns:

  • (true, false)

    True if the classes are equal, false if not.

Since:

  • 2.0.0.rc.4



276
277
278
# File 'lib/mongoid/document.rb', line 276

def ===(other)
  other.class == Class ? self <= other : other.is_a?(self)
end

#_typesArray<Class>

Returns all types to query for when using this class as the base.

Examples:

Get the types.

document._types

Returns:

  • (Array<Class>)

    All subclasses of the current document.

Since:

  • 1.0.0



314
315
316
# File 'lib/mongoid/document.rb', line 314

def _types
  @_type ||= (descendants + [ self ]).uniq.map { |t| t.to_s }
end

#i18n_scopeSymbol

Set the i18n scope to overwrite ActiveModel.

Returns:

Since:

  • 2.0.0



323
324
325
# File 'lib/mongoid/document.rb', line 323

def i18n_scope
  :mongoid
end

#instantiate(attrs = nil, criteria_instance_id = nil) {|doc| ... } ⇒ Document

Instantiate a new object, only when loaded from the database or when the attributes have already been typecast.

Examples:

Create the document.

Person.instantiate(:title => "Sir", :age => 30)

Parameters:

  • attrs (Hash) (defaults to: nil)

    The hash of attributes to instantiate with.

  • criteria_instance_id (Integer) (defaults to: nil)

    The criteria id that instantiated the document.

Yields:

  • (doc)

Returns:

Since:

  • 1.0.0



293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/mongoid/document.rb', line 293

def instantiate(attrs = nil, criteria_instance_id = nil)
  attributes = attrs || {}
  doc = allocate
  doc.criteria_instance_id = criteria_instance_id
  doc.instance_variable_set(:@attributes, attributes)
  doc.apply_defaults
  IdentityMap.set(doc) unless _loading_revision?
  yield(doc) if block_given?
  doc.run_callbacks(:find) unless doc._find_callbacks.empty?
  doc.run_callbacks(:initialize) unless doc._initialize_callbacks.empty?
  doc
end

#loggerLogger

Returns the logger

Examples:

Get the logger.

Person.logger

Returns:

  • (Logger)

    The configured logger or a default Logger instance.

Since:

  • 2.2.0



335
336
337
# File 'lib/mongoid/document.rb', line 335

def logger
  Mongoid.logger
end