Module: Exchanger::Dirty
- Included in:
- Element
- Defined in:
- lib/exchanger/dirty.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
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.
-
#setup_modifications ⇒ Object
Sets up the modifications hash.
Class Method Details
.included(base) ⇒ Object
3 4 5 |
# File 'lib/exchanger/dirty.rb', line 3 def self.included(base) base.extend(ClassMethods) end |
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.
18 19 20 |
# File 'lib/exchanger/dirty.rb', line 18 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.
33 34 35 |
# File 'lib/exchanger/dirty.rb', line 33 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.
48 49 50 51 |
# File 'lib/exchanger/dirty.rb', line 48 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.
64 65 66 |
# File 'lib/exchanger/dirty.rb', line 64 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.
79 80 81 |
# File 'lib/exchanger/dirty.rb', line 79 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.
96 97 98 |
# File 'lib/exchanger/dirty.rb', line 96 def changes modifications end |
#move_changes ⇒ Object
Call this method after save, so the changes can be properly switched.
Example:
person.move_changes
105 106 107 108 |
# File 'lib/exchanger/dirty.rb', line 105 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.
123 124 125 |
# File 'lib/exchanger/dirty.rb', line 123 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.
139 140 141 142 143 144 145 |
# File 'lib/exchanger/dirty.rb', line 139 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
165 166 167 168 |
# File 'lib/exchanger/dirty.rb', line 165 def reset_modifications @accessed = {} @modifications = {} end |
#setup_modifications ⇒ Object
Sets up the modifications hash. This occurs just after the document is instantiated.
Example:
document.setup_notifications
153 154 155 156 157 |
# File 'lib/exchanger/dirty.rb', line 153 def setup_modifications @accessed ||= {} @modifications ||= {} @previous_modifications ||= {} end |