Module: Mongoid::Document::ClassMethods

Defined in:
lib/mongoid/document.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#===(other) ⇒ true, false

Performs class equality checking.

Examples:

Compare the classes.

document === other

Parameters:

Returns:

  • (true, false)

    True if the classes are equal, false if not.

Since:

  • 2.0.0.rc.4



244
245
246
# File 'lib/mongoid/document.rb', line 244

def ===(other)
  self == (other.is_a?(Class) ? other : other.class)
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.



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

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

#i18n_scopeObject

Set the i18n scope to overwrite ActiveModel.



282
283
284
# File 'lib/mongoid/document.rb', line 282

def i18n_scope
  :mongoid
end

#instantiate(attrs = nil) ⇒ 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.

Returns:



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/mongoid/document.rb', line 257

def instantiate(attrs = nil)
  attributes = attrs || {}
  if attributes["_id"]
    allocate.tap do |doc|
      doc.instance_variable_set(:@attributes, attributes)
      doc.send(:apply_default_attributes)
      doc.setup_modifications
      doc.run_callbacks(:initialize) { doc }
    end
  else
    new(attrs)
  end
end