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.
-
#add_indexes ⇒ Object
Add the default indexes to the root document if they do not already exist.
-
#collection ⇒ Object
Returns the collection associated with this
Document
. -
#embedded? ⇒ Boolean
return true if the
Document
is embedded in anotherDocumnet
. -
#field(name, options = {}) ⇒ Object
Defines all the fields that are accessable on the Document For each field that is defined, a getter and setter will be added as an instance method to the Document.
-
#human_name ⇒ Object
Returns a human readable version of the class.
-
#index(name, options = { :unique => false }) ⇒ Object
Adds an index on the field specified.
-
#instantiate(attrs = {}, allocating = false) ⇒ Object
Instantiate a new object, only when loaded from the database.
-
#key(*fields) ⇒ Object
Defines the field that will be used for the id of this
Document
. -
#store_in(name) ⇒ Object
Macro for setting the collection name to store in.
Instance Method Details
#_types ⇒ Object
Returns all types to query for when using this class as the base.
122 123 124 |
# File 'lib/mongoid/document.rb', line 122 def _types @_type ||= (self.subclasses + [ self.name ]) end |
#add_indexes ⇒ Object
Add the default indexes to the root document if they do not already exist. Currently this is only _type.
42 43 44 45 46 47 |
# File 'lib/mongoid/document.rb', line 42 def add_indexes unless indexed self._collection.create_index(:_type, false) self.indexed = true end end |
#collection ⇒ Object
Returns the collection associated with this Document
. If the document is embedded, there will be no collection associated with it.
Returns: Mongo::Collection
54 55 56 57 58 |
# File 'lib/mongoid/document.rb', line 54 def collection raise Errors::InvalidCollection.new(self) if self._collection ||= Mongoid.database.collection(self.collection_name) add_indexes; self._collection end |
#embedded? ⇒ Boolean
return true if the Document
is embedded in another Documnet
.
61 62 63 |
# File 'lib/mongoid/document.rb', line 61 def self. == true end |
#field(name, options = {}) ⇒ Object
Defines all the fields that are accessable on the Document For each field that is defined, a getter and setter will be added as an instance method to the Document.
Options:
name: The name of the field, as a Symbol
. options: A Hash
of options to supply to the Field
.
Example:
field :score, :default => 0
77 78 79 80 |
# File 'lib/mongoid/document.rb', line 77 def field(name, = {}) set_field(name, ) set_default(name, ) end |
#human_name ⇒ Object
Returns a human readable version of the class.
83 84 85 |
# File 'lib/mongoid/document.rb', line 83 def human_name name.underscore.humanize end |
#index(name, options = { :unique => false }) ⇒ Object
Adds an index on the field specified. Options can be :unique => true or :unique => false. It will default to the latter.
89 90 91 |
# File 'lib/mongoid/document.rb', line 89 def index(name, = { :unique => false }) collection.create_index(name, [:unique]) end |
#instantiate(attrs = {}, allocating = false) ⇒ Object
Instantiate a new object, only when loaded from the database.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/mongoid/document.rb', line 94 def instantiate(attrs = {}, allocating = false) attributes = attrs.with_indifferent_access if attributes[:_id] || allocating document = allocate document.instance_variable_set(:@attributes, attributes) return document else return new(attributes) 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 and MUST be defined on documents that are embedded in order for proper updates in has_may associations.
110 111 112 113 |
# File 'lib/mongoid/document.rb', line 110 def key(*fields) self.primary_key = fields before_save :generate_key end |