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)
-
- (Object) attribute_change(name)
Gets the changes for a specific field.
-
- (Boolean) attribute_changed?(name)
Determines if a specific field has chaged.
-
- (Object) attribute_was(name)
Gets the old value for a specific field.
-
- (Object) changed
Gets the names of all the fields that have changed in the document.
-
- (Boolean) changed?
Alerts to whether the document has been modified or not.
-
- (Object) changes
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.
-
- (Object) move_changes
Call this method after save, so the changes can be properly switched.
-
- (Object) previous_changes
Gets all the modifications that have happened to the object before the object was saved.
-
- (Object) reset_attribute!(name)
Resets a changed field back to its old value.
-
- (Object) reset_modifications
Reset all modifications for the document.
-
- (Object) setup_modifications
Sets up the modifications hash.
Class Method Details
+ (Object) included(base)
3 4 5 |
# File 'lib/exchanger/dirty.rb', line 3 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
- (Object) attribute_change(name)
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 |
- (Boolean) attribute_changed?(name)
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 |
- (Object) attribute_was(name)
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 |
- (Object) changed
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 |
- (Boolean) changed?
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 |
- (Object) changes
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 |
- (Object) move_changes
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 |
- (Object) previous_changes
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 |
- (Object) reset_attribute!(name)
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 |
- (Object) reset_modifications
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 |
- (Object) setup_modifications
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 |