Module: Mongoid::Stateful
- Included in:
- Composable
- Defined in:
- lib/mongoid/stateful.rb
Overview
Mixin module included into Mongoid::Document which adds behavior for getting the various lifecycle states a document can transition through.
Instance Attribute Summary collapse
-
#destroyed ⇒ Object
writeonly
Sets the attribute destroyed.
-
#flagged_for_destroy ⇒ Object
writeonly
Sets the attribute flagged_for_destroy.
-
#previously_new_record ⇒ Object
writeonly
Sets the attribute previously_new_record.
Instance Method Summary collapse
-
#destroyed? ⇒ true | false
Returns true if the
Document
has been succesfully destroyed, and false if it hasn’t. -
#flagged_for_destroy? ⇒ true | false
(also: #marked_for_destruction?, #_destroy)
Returns whether or not the document has been flagged for deletion, but not destroyed yet.
-
#new_record=(new_value) ⇒ true | false
Sets whether the document has been persisted to the database.
-
#new_record? ⇒ true | false
Returns true if the document has not been persisted to the database, false if it has.
-
#persisted? ⇒ true | false
Checks if the document has been saved to the database.
-
#previously_new_record? ⇒ true | false
Returns true if this document was just created – that is, prior to the last save, the object didn’t exist in the database and new_record? would have returned true.
-
#previously_persisted? ⇒ true | false
Checks if the document was previously saved to the database but now it has been deleted.
-
#pushable? ⇒ true | false
Determine if the document can be pushed.
-
#readonly! ⇒ true | false
Flags the document as readonly.
-
#readonly? ⇒ true | false
Is the document readonly?.
-
#settable? ⇒ true | false
Determine if the document can be set.
-
#updateable? ⇒ true | false
Is the document updateable?.
Instance Attribute Details
#destroyed=(value) ⇒ Object (writeonly)
Sets the attribute destroyed
10 11 12 |
# File 'lib/mongoid/stateful.rb', line 10 def destroyed=(value) @destroyed = value end |
#flagged_for_destroy=(value) ⇒ Object (writeonly)
Sets the attribute flagged_for_destroy
10 11 12 |
# File 'lib/mongoid/stateful.rb', line 10 def flagged_for_destroy=(value) @flagged_for_destroy = value end |
#previously_new_record=(value) ⇒ Object (writeonly)
Sets the attribute previously_new_record
10 11 12 |
# File 'lib/mongoid/stateful.rb', line 10 def previously_new_record=(value) @previously_new_record = value end |
Instance Method Details
#destroyed? ⇒ true | false
Returns true if the Document
has been succesfully destroyed, and false if it hasn’t. This is determined by the variable @destroyed and NOT by checking the database.
87 88 89 |
# File 'lib/mongoid/stateful.rb', line 87 def destroyed? @destroyed ||= false end |
#flagged_for_destroy? ⇒ true | false Also known as: marked_for_destruction?, _destroy
Returns whether or not the document has been flagged for deletion, but not destroyed yet. Used for atomic pulls of child documents.
73 74 75 |
# File 'lib/mongoid/stateful.rb', line 73 def flagged_for_destroy? @flagged_for_destroy ||= false end |
#new_record=(new_value) ⇒ true | false
Sets whether the document has been persisted to the database.
17 18 19 20 21 22 23 |
# File 'lib/mongoid/stateful.rb', line 17 def new_record=(new_value) @new_record ||= false if @new_record && !new_value @previously_new_record = true end @new_record = new_value end |
#new_record? ⇒ true | false
Returns true if 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.
33 34 35 |
# File 'lib/mongoid/stateful.rb', line 33 def new_record? @new_record ||= false end |
#persisted? ⇒ true | false
Checks if the document has been saved to the database. Returns false if the document has been destroyed.
53 54 55 |
# File 'lib/mongoid/stateful.rb', line 53 def persisted? !new_record? && !destroyed? end |
#previously_new_record? ⇒ true | false
Returns true if this document was just created – that is, prior to the last save, the object didn’t exist in the database and new_record? would have returned true.
42 43 44 |
# File 'lib/mongoid/stateful.rb', line 42 def previously_new_record? @previously_new_record ||= false end |
#previously_persisted? ⇒ true | false
Checks if the document was previously saved to the database but now it has been deleted.
62 63 64 |
# File 'lib/mongoid/stateful.rb', line 62 def previously_persisted? !new_record? && destroyed? end |
#pushable? ⇒ true | false
Determine if the document can be pushed.
97 98 99 100 101 102 |
# File 'lib/mongoid/stateful.rb', line 97 def pushable? new_record? && && _parent.persisted? && !_parent.delayed_atomic_sets[atomic_path] end |
#readonly! ⇒ true | false
Flags the document as readonly. Will cause a ReadonlyDocument error to be raised if the document is attempted to be saved, updated or destroyed.
112 113 114 115 116 117 118 119 |
# File 'lib/mongoid/stateful.rb', line 112 def readonly! if Mongoid.legacy_readonly Mongoid::Warnings.warn_legacy_readonly false else @readonly = true end end |
#readonly? ⇒ true | false
Is the document readonly?
127 128 129 130 131 132 133 |
# File 'lib/mongoid/stateful.rb', line 127 def readonly? if Mongoid.legacy_readonly __selected_fields != nil else @readonly ||= false end end |
#settable? ⇒ true | false
Determine if the document can be set.
141 142 143 |
# File 'lib/mongoid/stateful.rb', line 141 def settable? new_record? && && _parent.persisted? end |
#updateable? ⇒ true | false
Is the document updateable?
151 152 153 |
# File 'lib/mongoid/stateful.rb', line 151 def updateable? persisted? && changed? end |