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. -
#assimilate(parent, options) ⇒ Object
Introduces a child object into the
Document
object graph. -
#attributes ⇒ Object
Return the attributes hash with indifferent access.
-
#clone ⇒ Object
Clone the current
Document
. -
#eql?(comparison_object) ⇒ Boolean
Delegates to ==.
-
#hash ⇒ Object
Delegates to id in order to allow two records of the same type and id to work with something like: [ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ].
-
#identify ⇒ Object
Generate an id for this
Document
. -
#initialize(attrs = nil) ⇒ 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.
-
#persisted? ⇒ Boolean
Checks if the document has been saved to the database.
-
#raw_attributes ⇒ Object
Return the attributes hash.
-
#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_json(options = nil) ⇒ Object
Return this document as a JSON string.
-
#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
.
Instance Method Details
#==(other) ⇒ Object
Performs equality checking on the attributes. For now we chack against all attributes excluding timestamps on the object.
108 109 110 111 112 |
# File 'lib/mongoid/document.rb', line 108 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.
244 245 246 247 248 |
# File 'lib/mongoid/document.rb', line 244 def _root object = self while (object._parent) do object = object._parent; end object || self 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.
133 134 135 |
# File 'lib/mongoid/document.rb', line 133 def assimilate(parent, ) parentize(parent, .name); notify; self end |
#attributes ⇒ Object
Return the attributes hash with indifferent access.
138 139 140 |
# File 'lib/mongoid/document.rb', line 138 def attributes @attributes.with_indifferent_access end |
#clone ⇒ Object
Clone the current Document
. This will return all attributes with the exception of the document’s id and versions.
144 145 146 |
# File 'lib/mongoid/document.rb', line 144 def clone self.class.instantiate(@attributes.except("_id").except("versions").dup, true) end |
#eql?(comparison_object) ⇒ Boolean
Delegates to ==
115 116 117 |
# File 'lib/mongoid/document.rb', line 115 def eql?(comparison_object) self == (comparison_object) end |
#hash ⇒ Object
Delegates to id in order to allow two records of the same type and id to work with something like:
[ Person.find(1), Person.find(2), Person.find(3) ] & [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
121 122 123 |
# File 'lib/mongoid/document.rb', line 121 def hash id.hash end |
#identify ⇒ Object
Generate an id for this Document
.
149 150 151 |
# File 'lib/mongoid/document.rb', line 149 def identify Identity.create(self) end |
#initialize(attrs = nil) ⇒ 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.
163 164 165 166 167 168 169 170 |
# File 'lib/mongoid/document.rb', line 163 def initialize(attrs = nil) @attributes = {} process(attrs) @attributes = attributes_with_defaults(@attributes) @new_record = true if id.nil? document = yield self if block_given? identify end |
#inspect ⇒ Object
Returns the class name plus its attributes.
173 174 175 176 |
# File 'lib/mongoid/document.rb', line 173 def inspect attrs = fields.map { |name, field| "#{name}: #{@attributes[name].inspect}" } * ", " "#<#{self.class.name} _id: #{id}, #{attrs}>" end |
#new_record=(saved) ⇒ Object
Sets the new_record boolean - used after document is saved.
186 187 188 |
# File 'lib/mongoid/document.rb', line 186 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.
181 182 183 |
# File 'lib/mongoid/document.rb', line 181 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
200 201 202 203 |
# File 'lib/mongoid/document.rb', line 200 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)
217 218 219 220 221 |
# File 'lib/mongoid/document.rb', line 217 def parentize(object, association_name) self._parent = object self.association_name = association_name.to_s add_observer(object) end |
#persisted? ⇒ Boolean
Checks if the document has been saved to the database.
191 192 193 |
# File 'lib/mongoid/document.rb', line 191 def persisted? !new_record? end |
#raw_attributes ⇒ Object
Return the attributes hash.
224 225 226 |
# File 'lib/mongoid/document.rb', line 224 def raw_attributes @attributes end |
#reload ⇒ Object
Reloads the Document
attributes from the database.
229 230 231 232 |
# File 'lib/mongoid/document.rb', line 229 def reload @attributes = collection.find_one(:_id => id) self end |
#remove(child) ⇒ Object
Remove a child document from this parent Document
. Will reset the memoized association and notify the parent of the change.
236 237 238 239 240 |
# File 'lib/mongoid/document.rb', line 236 def remove(child) name = child.association_name reset(name) { @attributes.remove(name, child.raw_attributes) } notify end |
#to_a ⇒ Object
Return an array with this Document
only in it.
251 252 253 |
# File 'lib/mongoid/document.rb', line 251 def to_a [ self ] end |
#to_json(options = nil) ⇒ Object
Return this document as a JSON string. Nothing special is required here since Mongoid bubbles up all the child associations to the parent attribute Hash
using observers throughout the Document
lifecycle.
Example:
person.to_json
262 263 264 |
# File 'lib/mongoid/document.rb', line 262 def to_json( = nil) attributes.to_json() end |
#to_param ⇒ Object
Returns the id of the Document, used in Rails compatibility.
267 268 269 |
# File 'lib/mongoid/document.rb', line 267 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.
This will also cause the observing Document
to notify it’s parent if there is any.
282 283 284 285 286 287 |
# File 'lib/mongoid/document.rb', line 282 def update(child, clear = false) name = child.association_name attrs = child.instance_variable_get(:@attributes) clear ? @attributes.delete(name) : @attributes.insert(name, attrs) notify end |