Module: Mongoid::Document::InstanceMethods

Defined in:
lib/mongoid/document.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object

Performs equality checking on the attributes. For now we chack against all attributes excluding timestamps on the object.



143
144
145
146
147
# File 'lib/mongoid/document.rb', line 143

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

#_rootObject

Return the root Document in the object graph. If the current Document is the root object in the graph it will return self.



271
272
273
274
275
# File 'lib/mongoid/document.rb', line 271

def _root
  object = self
  while (object._parent) do object = object._parent; end
  object || self
end

#_typeObject

Returns the object type.



288
289
290
# File 'lib/mongoid/document.rb', line 288

def _type
  @attributes[:_type]
end

#_type=(new_type) ⇒ Object

Set the type.



293
294
295
# File 'lib/mongoid/document.rb', line 293

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.



163
164
165
# File 'lib/mongoid/document.rb', line 163

def assimilate(parent, options)
  parentize(parent, options.name); notify; self
end

#cloneObject

Clone the current Document. This will return all attributes with the exception of the document’s id and versions.



169
170
171
# File 'lib/mongoid/document.rb', line 169

def clone
  self.class.instantiate(@attributes.except(:_id).except(:versions).dup, true)
end

#idObject Also known as: _id

Get the id associated with this object. This will pull the _id value out of the attributes Hash.



175
176
177
# File 'lib/mongoid/document.rb', line 175

def id
  @attributes[:_id]
end

#id=(new_id) ⇒ Object Also known as: _id=

Set the id



180
181
182
# File 'lib/mongoid/document.rb', line 180

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)



201
202
203
204
205
206
207
# File 'lib/mongoid/document.rb', line 201

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

#inspectObject

Returns the class name plus its attributes.



210
211
212
213
# File 'lib/mongoid/document.rb', line 210

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.



223
224
225
# File 'lib/mongoid/document.rb', line 223

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.

Returns:



218
219
220
# File 'lib/mongoid/document.rb', line 218

def new_record?
  @new_record == true
end

#notifyObject

Set the changed state of the Document then notify observers that it has changed.

Example:

person.notify



232
233
234
235
# File 'lib/mongoid/document.rb', line 232

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)



249
250
251
252
253
# File 'lib/mongoid/document.rb', line 249

def parentize(object, association_name)
  self._parent = object
  self.association_name = association_name
  add_observer(object)
end

#reloadObject

Reloads the Document attributes from the database.



256
257
258
# File 'lib/mongoid/document.rb', line 256

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.



262
263
264
265
266
267
# File 'lib/mongoid/document.rb', line 262

def remove(child)
  name = child.association_name
  @attributes.remove(name, child.attributes)
  remove_instance_variable("@#{name}")
  notify
end

#to_aObject

Return an array with this Document only in it.



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

def to_a
  [ self ]
end

#to_paramObject

Returns the id of the Document, used in Rails compatibility.



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

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.



312
313
314
315
316
# File 'lib/mongoid/document.rb', line 312

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.

Returns:



320
321
322
323
324
325
# File 'lib/mongoid/document.rb', line 320

def valid?
  run_callbacks(:before_validation)
  result = super
  run_callbacks(:after_validation)
  result
end