module MongoModel::AttributeMethods::Dirty
Constants
- OPTION_NOT_GIVEN
Public Instance Methods
attribute_changed?(attr, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN)
click to toggle source
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 40 def attribute_changed?(attr, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN) !!changes_include?(attr) && (to == OPTION_NOT_GIVEN || to == __send__(attr)) && (from == OPTION_NOT_GIVEN || from == changed_attributes[attr]) end
attribute_previously_changed?(attr)
click to toggle source
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 50 def attribute_previously_changed?(attr) previous_changes.include?(attr) end
attribute_was(attr)
click to toggle source
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 46 def attribute_was(attr) attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr) end
changed()
click to toggle source
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 24 def changed changed_attributes.keys end
changed?()
click to toggle source
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 20 def changed? changed_attributes.present? end
changed_attributes()
click to toggle source
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 36 def changed_attributes @changed_attributes ||= ActiveSupport::HashWithIndifferentAccess.new end
Also aliased as: attributes_changed_by_setter
changes()
click to toggle source
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 28 def changes ActiveSupport::HashWithIndifferentAccess[changed.map { |attr| [attr, attribute_change(attr)] }] end
original_attributes()
click to toggle source
Returns the attributes as they were before any changes were made to the document.
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 74 def original_attributes {}.with_indifferent_access.merge(attributes).merge(changed_attributes) end
previous_changes()
click to toggle source
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 32 def previous_changes @previously_changed ||= ActiveSupport::HashWithIndifferentAccess.new end
restore_attributes(attributes = changed)
click to toggle source
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 54 def restore_attributes(attributes = changed) attributes.each { |attr| restore_attribute! attr } end
write_attribute(key, value)
click to toggle source
Calls superclass method
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 58 def write_attribute(key, value) attr = key.to_sym # The attribute already has an unsaved change. if changed_attributes.include?(attr) old = changed_attributes[attr] changed_attributes.delete(attr) if value == old else old = clone_attribute_value(attr) changed_attributes[attr] = old unless value == old end super end
Private Instance Methods
attribute_change(attr)
click to toggle source
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 84 def attribute_change(attr) [changed_attributes[attr], __send__(attr)] if attribute_changed?(attr) end
attribute_previous_change(attr)
click to toggle source
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 88 def attribute_previous_change(attr) previous_changes[attr] if attribute_previously_changed?(attr) end
attribute_will_change!(attr)
click to toggle source
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 92 def attribute_will_change!(attr) return if attribute_changed?(attr) begin value = __send__(attr) value = value.duplicable? ? value.clone : value rescue TypeError, NoMethodError end set_attribute_was(attr, value) end
changes_include?(attr_name)
click to toggle source
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 79 def changes_include?(attr_name) attributes_changed_by_setter.include?(attr_name) end
Also aliased as: attribute_changed_by_setter?
clear_attribute_changes(attributes)
click to toggle source
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 117 def clear_attribute_changes(attributes) attributes_changed_by_setter.except!(*attributes) end
clone_attribute_value(attribute_name)
click to toggle source
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 121 def clone_attribute_value(attribute_name) value = self[attribute_name.to_sym] value.duplicable? ? value.clone : value rescue TypeError, NoMethodError value end
restore_attribute!(attr)
click to toggle source
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 104 def restore_attribute!(attr) if attribute_changed?(attr) __send__("#{attr}=", changed_attributes[attr]) clear_attribute_changes([attr]) end end
set_attribute_was(attr, old_value)
click to toggle source
# File lib/mongomodel/concerns/attribute_methods/dirty.rb, line 113 def set_attribute_was(attr, old_value) attributes_changed_by_setter[attr] = old_value end