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.
-
#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.
-
#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.
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 |
#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 |
#notify ⇒ Object
Set the changed state of the Document
then notify observers that it has changed.
Example:
person.notify
183 184 185 186 |
# File 'lib/mongoid/document.rb', line 183 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)
200 201 202 203 204 |
# File 'lib/mongoid/document.rb', line 200 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.
207 208 209 |
# File 'lib/mongoid/document.rb', line 207 def raw_attributes @attributes end |
#reload ⇒ Object
Reloads the Document
attributes from the database.
212 213 214 215 |
# File 'lib/mongoid/document.rb', line 212 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.
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.
250 251 252 |
# File 'lib/mongoid/document.rb', line 250 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.
265 266 267 268 269 270 |
# File 'lib/mongoid/document.rb', line 265 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 |