Module: Mongoid::Document::InstanceMethods
- Defined in:
- lib/mongoid/document.rb
Instance Method Summary collapse
-
#==(other) ⇒ Object
Performs equality checking on the attributes.
-
#_root ⇒ Object
Return the root
Document
in the object graph. -
#_type ⇒ Object
Returns the object type.
-
#_type=(new_type) ⇒ Object
Set the type.
-
#assimilate(parent, options) ⇒ Object
Introduces a child object into the
Document
object graph. -
#clone ⇒ Object
Clone the current
Document
. -
#id ⇒ Object
(also: #_id)
Get the id associated with this object.
-
#id=(new_id) ⇒ Object
(also: #_id=)
Set the id.
-
#initialize(attrs = {}) ⇒ Object
Instantiate a new
Document
, setting the Document’s attributes if given. -
#inspect ⇒ Object
Returns the class name plus its attributes.
-
#new_record=(saved) ⇒ Object
Sets the new_record boolean - used after document is saved.
-
#new_record? ⇒ Boolean
Returns true is the
Document
has not been persisted to the database, false if it has. -
#notify ⇒ Object
Set the changed state of the
Document
then notify observers that it has changed. -
#parentize(object, association_name) ⇒ Object
Sets up a child/parent association.
-
#reload ⇒ Object
Reloads the
Document
attributes from the database. -
#remove(child) ⇒ Object
Remove a child document from this parent
Document
. -
#to_a ⇒ Object
Return an array with this
Document
only in it. -
#to_param ⇒ Object
Returns the id of the Document, used in Rails compatibility.
-
#update(child, clear = false) ⇒ Object
Observe a notify call from a child
Document
. -
#valid? ⇒ Boolean
Needs to run the appropriate callbacks the delegate up to the validatable gem.
Instance Method Details
#==(other) ⇒ Object
Performs equality checking on the attributes. For now we chack against all attributes excluding timestamps on the object.
153 154 155 156 157 |
# File 'lib/mongoid/document.rb', line 153 def ==(other) return false unless other.is_a?(Document) @attributes.except(:modified_at).except(:created_at) == other.attributes.except(:modified_at).except(:created_at) end |
#_root ⇒ Object
Return the root Document
in the object graph. If the current Document
is the root object in the graph it will return self.
281 282 283 284 285 |
# File 'lib/mongoid/document.rb', line 281 def _root object = self while (object._parent) do object = object._parent; end object || self end |
#_type ⇒ Object
Returns the object type.
298 299 300 |
# File 'lib/mongoid/document.rb', line 298 def _type @attributes[:_type] end |
#_type=(new_type) ⇒ Object
Set the type.
303 304 305 |
# File 'lib/mongoid/document.rb', line 303 def _type=(new_type) @attributes[:_type] = new_type end |
#assimilate(parent, options) ⇒ Object
Introduces a child object into the Document
object graph. This will set up the relationships between the parent and child and update the attributes of the parent Document
.
Options:
parent: The Document
to assimilate with. options: The association Options
for the child.
Example:
name.assimilate(person, options)
Returns: The child Document
.
173 174 175 |
# File 'lib/mongoid/document.rb', line 173 def assimilate(parent, ) parentize(parent, .name); notify; self end |
#clone ⇒ Object
Clone the current Document
. This will return all attributes with the exception of the document’s id and versions.
179 180 181 |
# File 'lib/mongoid/document.rb', line 179 def clone self.class.instantiate(@attributes.except(:_id).except(:versions).dup, true) end |
#id ⇒ Object Also known as: _id
Get the id associated with this object. This will pull the _id value out of the attributes Hash
.
185 186 187 |
# File 'lib/mongoid/document.rb', line 185 def id @attributes[:_id] end |
#id=(new_id) ⇒ Object Also known as: _id=
Set the id
190 191 192 |
# File 'lib/mongoid/document.rb', line 190 def id=(new_id) @attributes[:_id] = new_id end |
#initialize(attrs = {}) ⇒ Object
Instantiate a new Document
, setting the Document’s attributes if given. If no attributes are provided, they will be initialized with an empty Hash
.
If a primary key is defined, the document’s id will be set to that key, otherwise it will be set to a fresh Mongo::ObjectID
string.
Options:
attrs: The attributes Hash
to set up the document with.
Example:
Person.new(:title => "Mr", :age => 30)
211 212 213 214 215 216 217 |
# File 'lib/mongoid/document.rb', line 211 def initialize(attrs = {}) @attributes = {}.with_indifferent_access process(defaults.merge(attrs)) @new_record = true if id.nil? document = yield self if block_given? generate_key; generate_type; document end |
#inspect ⇒ Object
Returns the class name plus its attributes.
220 221 222 223 |
# File 'lib/mongoid/document.rb', line 220 def inspect attrs = fields.map { |name, field| "#{name}: #{@attributes[name] || 'nil'}" } * ", " "#<#{self.class.name} _id: #{id}, #{attrs}>" end |
#new_record=(saved) ⇒ Object
Sets the new_record boolean - used after document is saved.
233 234 235 |
# File 'lib/mongoid/document.rb', line 233 def new_record=(saved) @new_record = saved end |
#new_record? ⇒ Boolean
Returns true is the Document
has not been persisted to the database, false if it has. This is determined by the variable @new_record and NOT if the object has an id.
228 229 230 |
# File 'lib/mongoid/document.rb', line 228 def new_record? @new_record == true end |
#notify ⇒ Object
Set the changed state of the Document
then notify observers that it has changed.
Example:
person.notify
242 243 244 245 |
# File 'lib/mongoid/document.rb', line 242 def notify changed(true) notify_observers(self) end |
#parentize(object, association_name) ⇒ Object
Sets up a child/parent association. This is used for newly created objects so they can be properly added to the graph and have the parent observers set up properly.
Options:
abject: The parent object that needs to be set for the child. association_name: The name of the association for the child.
Example:
address.parentize(person, :addresses)
259 260 261 262 263 |
# File 'lib/mongoid/document.rb', line 259 def parentize(object, association_name) self._parent = object self.association_name = association_name add_observer(object) end |
#reload ⇒ Object
Reloads the Document
attributes from the database.
266 267 268 |
# File 'lib/mongoid/document.rb', line 266 def reload @attributes = collection.find_one(:_id => id).with_indifferent_access end |
#remove(child) ⇒ Object
Remove a child document from this parent Document
. Will reset the memoized association and notify the parent of the change.
272 273 274 275 276 277 |
# File 'lib/mongoid/document.rb', line 272 def remove(child) name = child.association_name @attributes.remove(name, child.attributes) remove_instance_variable("@#{name}") notify end |
#to_a ⇒ Object
Return an array with this Document
only in it.
288 289 290 |
# File 'lib/mongoid/document.rb', line 288 def to_a [ self ] end |
#to_param ⇒ Object
Returns the id of the Document, used in Rails compatibility.
293 294 295 |
# File 'lib/mongoid/document.rb', line 293 def to_param id end |
#update(child, clear = false) ⇒ Object
Observe a notify call from a child Document
. This will either update existing attributes on the Document
or clear them out for the child if the clear boolean is provided.
Options:
child: The child Document
that sent the notification. clear: Will clear out the child’s attributes if set to true.
Example:
person.notify_observers(self)
will cause this method to execute.
This will also cause the observing Document
to notify it’s parent if there is any.
322 323 324 325 326 |
# File 'lib/mongoid/document.rb', line 322 def update(child, clear = false) name = child.association_name clear ? @attributes.delete(name) : @attributes.insert(name, child.attributes) notify end |
#valid? ⇒ Boolean
Needs to run the appropriate callbacks the delegate up to the validatable gem.
330 331 332 333 334 335 |
# File 'lib/mongoid/document.rb', line 330 def valid? run_callbacks(:before_validation) result = super run_callbacks(:after_validation) result end |