Module: Mongoid::Document::ClassMethods
- Defined in:
- lib/mongoid/document.rb
Instance Method Summary collapse
-
#_types ⇒ Object
Returns all types to query for when using this class as the base.
-
#db ⇒ Object
Return the database associated with this class.
-
#human_name ⇒ Object
Returns a human readable version of the class.
-
#inherited(subclass) ⇒ Object
Perform default behavior but mark the hierarchy as being hereditary.
-
#instantiate(attrs = nil, allocating = false) ⇒ Object
Instantiate a new object, only when loaded from the database or when the attributes have already been typecast.
-
#key(*fields) ⇒ Object
Defines the field that will be used for the id of this
Document
. -
#subclasses_of(*superclasses) ⇒ Object
return the list of subclassses for an object.
Instance Method Details
#_types ⇒ Object
Returns all types to query for when using this class as the base.
73 74 75 |
# File 'lib/mongoid/document.rb', line 73 def _types @_type ||= (self.subclasses + [ self.name ]) end |
#db ⇒ Object
Return the database associated with this class.
19 20 21 |
# File 'lib/mongoid/document.rb', line 19 def db collection.db end |
#human_name ⇒ Object
Returns a human readable version of the class.
Example:
MixedDrink.human_name # returns "Mixed Drink"
34 35 36 |
# File 'lib/mongoid/document.rb', line 34 def human_name name.labelize end |
#inherited(subclass) ⇒ Object
Perform default behavior but mark the hierarchy as being hereditary.
24 25 26 27 |
# File 'lib/mongoid/document.rb', line 24 def inherited(subclass) super(subclass) self.hereditary = true end |
#instantiate(attrs = nil, allocating = false) ⇒ Object
Instantiate a new object, only when loaded from the database or when the attributes have already been typecast.
Example:
Person.instantiate(:title => "Sir", :age => 30)
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/mongoid/document.rb', line 44 def instantiate(attrs = nil, allocating = false) attributes = attrs || {} if attributes["_id"] || allocating document = allocate document.instance_variable_set(:@attributes, attributes) document.setup_modifications return document else return new(attrs) end end |
#key(*fields) ⇒ Object
Defines the field that will be used for the id of this Document
. This set the id of this Document
before save to a parameterized version of the field that was supplied. This is good for use for readable URLS in web applications.
Example:
class Person
include Mongoid::Document
key :first_name, :last_name
end
67 68 69 70 |
# File 'lib/mongoid/document.rb', line 67 def key(*fields) self.primary_key = fields before_save :identify end |
#subclasses_of(*superclasses) ⇒ Object
return the list of subclassses for an object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/mongoid/document.rb', line 78 def subclasses_of(*superclasses) #:nodoc: subclasses = [] superclasses.each do |sup| ObjectSpace.each_object(class << sup; self; end) do |k| if k != sup && (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id")) subclasses << k end end end subclasses end |