Module: Mongoid::Document::InstanceMethods
- Defined in:
- lib/mongoid/document.rb
Instance Method Summary collapse
-
#==(other) ⇒ Object
Performs equality checking on the document ids.
-
#_root ⇒ Object
Return the root
Document
in the object graph. -
#as_json(options = nil) ⇒ Object
Return an object to be encoded into a JSON string.
-
#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
. -
#encode_json(encoder) ⇒ Object
Return this document as an object to be encoded as JSON, with any particular items modified on a per-encoder basis.
-
#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) ].
-
#hereditary? ⇒ Boolean
Is inheritance in play here?.
-
#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.
-
#notify ⇒ Object
Notify observers of an update.
-
#observe(child, clear = false) ⇒ Object
Observe a notify call from a child
Document
. -
#parentize(object, association_name) ⇒ Object
Sets up a child/parent association.
-
#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.
Instance Method Details
#==(other) ⇒ Object
Performs equality checking on the document ids. For more robust equality checking please override this method.
94 95 96 97 |
# File 'lib/mongoid/document.rb', line 94 def ==(other) return false unless other.is_a?(Document) id == other.id 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.
227 228 229 230 231 |
# File 'lib/mongoid/document.rb', line 227 def _root object = self while (object._parent) do object = object._parent; end object || self end |
#as_json(options = nil) ⇒ Object
Return an object to be encoded into a JSON string. Used by Rails 3’s object->JSON chain to create JSON in a backend-agnostic way
Example:
person.as_json
256 257 258 |
# File 'lib/mongoid/document.rb', line 256 def as_json( = nil) attributes 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.
127 128 129 |
# File 'lib/mongoid/document.rb', line 127 def assimilate(parent, ) parentize(parent, .name); notify; self end |
#attributes ⇒ Object
Return the attributes hash with indifferent access.
132 133 134 |
# File 'lib/mongoid/document.rb', line 132 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.
138 139 140 |
# File 'lib/mongoid/document.rb', line 138 def clone self.class.instantiate(@attributes.except("_id").except("versions").dup, true) end |
#encode_json(encoder) ⇒ Object
Return this document as an object to be encoded as JSON, with any particular items modified on a per-encoder basis. 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.encode_json(encoder)
269 270 271 |
# File 'lib/mongoid/document.rb', line 269 def encode_json(encoder) attributes end |
#eql?(comparison_object) ⇒ Boolean
Delegates to ==
100 101 102 |
# File 'lib/mongoid/document.rb', line 100 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) ]
106 107 108 |
# File 'lib/mongoid/document.rb', line 106 def hash id.hash end |
#hereditary? ⇒ Boolean
Is inheritance in play here?
Returns:
true
if inheritance used, false
if not.
115 116 117 |
# File 'lib/mongoid/document.rb', line 115 def hereditary? !!self.hereditary end |
#identify ⇒ Object
Generate an id for this Document
.
143 144 145 |
# File 'lib/mongoid/document.rb', line 143 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 BSON::ObjectId
string.
Options:
attrs: The attributes Hash
to set up the document with.
157 158 159 160 161 162 163 |
# File 'lib/mongoid/document.rb', line 157 def initialize(attrs = nil) @attributes = default_attributes process(attrs) @new_record = true document = yield self if block_given? identify end |
#inspect ⇒ Object
Returns the class name plus its attributes.
166 167 168 169 170 171 172 173 |
# File 'lib/mongoid/document.rb', line 166 def inspect attrs = fields.map { |name, field| "#{name}: #{@attributes[name].inspect}" } if Mongoid.allow_dynamic_fields dynamic_keys = @attributes.keys - fields.keys - associations.keys - ["_id", "_type"] attrs += dynamic_keys.map { |name| "#{name}: #{@attributes[name].inspect}" } end "#<#{self.class.name} _id: #{id}, #{attrs * ', '}>" end |
#notify ⇒ Object
Notify observers of an update.
Example:
person.notify
180 181 182 |
# File 'lib/mongoid/document.rb', line 180 def notify notify_observers(self) end |
#observe(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.
289 290 291 292 293 294 295 296 297 298 |
# File 'lib/mongoid/document.rb', line 289 def observe(child, clear = false) name = child.association_name attrs = child.instance_variable_get(:@attributes) if clear @attributes.delete(name) else @attributes.insert(name, attrs) unless @attributes[name] && @attributes[name].include?(attrs) end notify 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)
196 197 198 199 200 |
# File 'lib/mongoid/document.rb', line 196 def parentize(object, association_name) self._parent = object self.association_name = association_name.to_s add_observer(object) end |
#raw_attributes ⇒ Object
Return the attributes hash.
203 204 205 |
# File 'lib/mongoid/document.rb', line 203 def raw_attributes @attributes end |
#reload ⇒ Object
Reloads the Document
attributes from the database.
208 209 210 211 212 213 214 215 |
# File 'lib/mongoid/document.rb', line 208 def reload reloaded = collection.find_one(:_id => id) if Mongoid.raise_not_found_error raise Errors::DocumentNotFound.new(self.class, id) if reloaded.nil? end @attributes = {}.merge(reloaded || {}) self.associations.keys.each { |association_name| unmemoize(association_name) }; 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.
219 220 221 222 223 |
# File 'lib/mongoid/document.rb', line 219 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.
234 235 236 |
# File 'lib/mongoid/document.rb', line 234 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
245 246 247 |
# File 'lib/mongoid/document.rb', line 245 def to_json( = nil) attributes.to_json() end |
#to_param ⇒ Object
Returns the id of the Document, used in Rails compatibility.
274 275 276 |
# File 'lib/mongoid/document.rb', line 274 def to_param id end |