Module: Mongoid::Dirty::InstanceMethods
- Defined in:
- lib/mongoid/dirty.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#attribute_change(name) ⇒ Object
Gets the changes for a specific field.
-
#attribute_changed?(name) ⇒ Boolean
Determines if a specific field has chaged.
-
#attribute_was(name) ⇒ Object
Gets the old value for a specific field.
-
#changed ⇒ Object
Gets the names of all the fields that have changed in the document.
-
#changed? ⇒ Boolean
Alerts to whether the document has been modified or not.
-
#changes ⇒ Object
Gets all the modifications that have happened to the object as a
Hash
with the keys being the names of the fields, and the values being anArray
with the old value and new value. -
#move_changes ⇒ Object
Call this method after save, so the changes can be properly switched.
-
#previous_changes ⇒ Object
Gets all the modifications that have happened to the object before the object was saved.
-
#reset_attribute!(name) ⇒ Object
Resets a changed field back to its old value.
-
#reset_modifications ⇒ Object
Reset all modifications for the document.
-
#setters ⇒ Object
Gets all the new values for each of the changed fields, to be passed to a MongoDB $set modifier.
-
#setup_modifications ⇒ Object
Sets up the modifications hash.
Instance Method Details
#attribute_change(name) ⇒ Object
Gets the changes for a specific field.
Example:
person = Person.new(:title => "Sir")
person.title = "Madam"
person.attribute_change("title") # [ "Sir", "Madam" ]
Returns:
An Array
containing the old and new values.
17 18 19 |
# File 'lib/mongoid/dirty.rb', line 17 def attribute_change(name) modifications[name] end |
#attribute_changed?(name) ⇒ Boolean
Determines if a specific field has chaged.
Example:
person = Person.new(:title => "Sir")
person.title = "Madam"
person.attribute_changed?("title") # true
Returns:
true
if changed, false
if not.
32 33 34 |
# File 'lib/mongoid/dirty.rb', line 32 def attribute_changed?(name) modifications.include?(name) end |
#attribute_was(name) ⇒ Object
Gets the old value for a specific field.
Example:
person = Person.new(:title => "Sir")
person.title = "Madam"
person.attribute_was("title") # "Sir"
Returns:
The old field value.
47 48 49 50 |
# File 'lib/mongoid/dirty.rb', line 47 def attribute_was(name) change = modifications[name] change ? change[0] : nil end |
#changed ⇒ Object
Gets the names of all the fields that have changed in the document.
Example:
person = Person.new(:title => "Sir")
person.title = "Madam"
person.changed # returns [ "title" ]
Returns:
An Array
of changed field names.
63 64 65 |
# File 'lib/mongoid/dirty.rb', line 63 def changed modifications.keys end |
#changed? ⇒ Boolean
Alerts to whether the document has been modified or not.
Example:
person = Person.new(:title => "Sir")
person.title = "Madam"
person.changed? # returns true
Returns:
true
if changed, false
if not.
78 79 80 |
# File 'lib/mongoid/dirty.rb', line 78 def changed? !modifications.empty? end |
#changes ⇒ Object
Gets all the modifications that have happened to the object as a Hash
with the keys being the names of the fields, and the values being an Array
with the old value and new value.
Example:
person = Person.new(:title => "Sir")
person.title = "Madam"
person.changes # returns { "title" => [ "Sir", "Madam" ] }
Returns:
A Hash
of changes.
95 96 97 |
# File 'lib/mongoid/dirty.rb', line 95 def changes modifications end |
#move_changes ⇒ Object
Call this method after save, so the changes can be properly switched.
Example:
person.move_changes
104 105 106 107 |
# File 'lib/mongoid/dirty.rb', line 104 def move_changes @previous_modifications = modifications.dup @modifications = {} end |
#previous_changes ⇒ Object
Gets all the modifications that have happened to the object before the object was saved.
Example:
person = Person.new(:title => "Sir")
person.title = "Madam"
person.save!
person.previous_changes # returns { "title" => [ "Sir", "Madam" ] }
Returns:
A Hash
of changes before save.
141 142 143 |
# File 'lib/mongoid/dirty.rb', line 141 def previous_changes @previous_modifications end |
#reset_attribute!(name) ⇒ Object
Resets a changed field back to its old value.
Example:
person = Person.new(:title => "Sir")
person.title = "Madam"
person.reset_attribute!("title")
person.title # "Sir"
Returns:
The old field value.
157 158 159 160 161 162 163 |
# File 'lib/mongoid/dirty.rb', line 157 def reset_attribute!(name) value = attribute_was(name) if value @attributes[name] = value modifications.delete(name) end end |
#reset_modifications ⇒ Object
Reset all modifications for the document. This will wipe all the marked changes, but not reset the values.
Example:
document.reset_modifications
183 184 185 186 |
# File 'lib/mongoid/dirty.rb', line 183 def reset_modifications @accessed = {} @modifications = {} end |
#setters ⇒ Object
Gets all the new values for each of the changed fields, to be passed to a MongoDB $set modifier.
Example:
person = Person.new(:title => "Sir")
person.title = "Madam"
person.setters # returns { "title" => "Madam" }
Returns:
A Hash
of new values.
121 122 123 124 125 126 |
# File 'lib/mongoid/dirty.rb', line 121 def setters modifications.inject({}) do |sets, (field, changes)| key = ? "#{_position}.#{field}" : field sets[key] = changes[1]; sets end end |
#setup_modifications ⇒ Object
Sets up the modifications hash. This occurs just after the document is instantiated.
Example:
document.setup_notifications
171 172 173 174 175 |
# File 'lib/mongoid/dirty.rb', line 171 def setup_modifications @accessed ||= {} @modifications ||= {} @previous_modifications ||= {} end |