Module: Mongoid::Document::ClassMethods

Defined in:
build/mongoid-master/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



305
306
307
# File 'build/mongoid-master/lib/mongoid/document.rb', line 305

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

#_mongoid_clear_typesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Clear the @_type cache. This is generally called when changing the discriminator key/value on a class.

Examples:

Get the types.

document._mongoid_clear_types


353
354
355
356
# File 'build/mongoid-master/lib/mongoid/document.rb', line 353

def _mongoid_clear_types
  @_type = nil
  superclass._mongoid_clear_types if hereditary?
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



342
343
344
# File 'build/mongoid-master/lib/mongoid/document.rb', line 342

def _types
  @_type ||= (descendants + [ self ]).uniq.map(&:discriminator_value)
end

#i18n_scopeSymbol

Set the i18n scope to overwrite ActiveModel.

Returns:

  • (Symbol)

    :mongoid

Since:

  • 2.0.0



363
364
365
# File 'build/mongoid-master/lib/mongoid/document.rb', line 363

def i18n_scope
  :mongoid
end

#instantiate(attrs = nil, selected_fields = 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.

  • selected_fields (Integer) (defaults to: nil)

    The selected fields from the criteria.

Yields:

  • (doc)

Returns:

Since:

  • 1.0.0



322
323
324
325
326
327
328
329
330
331
332
# File 'build/mongoid-master/lib/mongoid/document.rb', line 322

def instantiate(attrs = nil, selected_fields = nil)
  attributes = attrs || {}
  doc = allocate
  doc.__selected_fields = selected_fields
  doc.instance_variable_set(:@attributes, attributes)
  doc.apply_defaults
  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



375
376
377
# File 'build/mongoid-master/lib/mongoid/document.rb', line 375

def logger
  Mongoid.logger
end