Module: CouchbaseOrm::Changeable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Document
- Defined in:
- lib/couchbase-orm/changeable.rb
Overview
Defines behavior for dirty tracking.
Defined Under Namespace
Modules: ClassMethods Classes: Anything
Instance Method Summary collapse
- #_children ⇒ Object
-
#attribute_before_last_save(attr) ⇒ Object
Returns the original value of an attribute before the last save.
-
#changed ⇒ Array<String>
Get the changed attributes for the document.
-
#changed? ⇒ true | false
Has the document changed?.
-
#changed_attributes ⇒ Hash<String, Object>
Get the attribute changes.
-
#changes ⇒ Hash<String, Array<Object, Object> ] The changes.
Get all the changes for the document.
- #changes_applied ⇒ Object
-
#children_changed? ⇒ true | false
Have any children (embedded documents) of this document changed?.
-
#move_changes ⇒ Object
Call this method after save, so the changes can be properly switched.
-
#previous_changes ⇒ Hash<String, Array<Object, Object> ] The previous changes.
Get the previous changes on the document.
- #reset_object! ⇒ Object (also: #clear_changes_information)
-
#saved_change_to_attribute(attr) ⇒ Array<Object> | nil
Returns the change to an attribute during the last save.
-
#saved_change_to_attribute?(attr, from: Utils::PLACEHOLDER, to: Utils::PLACEHOLDER) ⇒ true | false
Returns whether this attribute changed during the last save.
-
#setters ⇒ Hash
Gets all the new values for each of the changed fields, to be passed to a CouchbaseOrm $set modifier.
-
#will_save_change_to_attribute?(attr, **kwargs) ⇒ true | false
Returns whether this attribute change the next time we save.
Instance Method Details
#_children ⇒ Object
30 31 32 |
# File 'lib/couchbase-orm/changeable.rb', line 30 def _children attributes.select { |name, _value| self.class.type_for_attribute(name) == CouchbaseOrm::NestedDocument } end |
#attribute_before_last_save(attr) ⇒ Object
Returns the original value of an attribute before the last save.
This method is useful in after callbacks to get the original value of
an attribute before the save that triggered the callbacks to run.
141 142 143 |
# File 'lib/couchbase-orm/changeable.rb', line 141 def attribute_before_last_save(attr) attributes_before_last_save[attr] end |
#changed ⇒ Array<String>
Get the changed attributes for the document.
16 17 18 |
# File 'lib/couchbase-orm/changeable.rb', line 16 def changed changed_attributes.keys.select { |attr| attribute_change(attr) } end |
#changed? ⇒ true | false
Has the document changed?
26 27 28 |
# File 'lib/couchbase-orm/changeable.rb', line 26 def changed? changes.values.any? { |val| val } || children_changed? end |
#changed_attributes ⇒ Hash<String, Object>
Get the attribute changes.
49 50 51 |
# File 'lib/couchbase-orm/changeable.rb', line 49 def changed_attributes @changed_attributes ||= {} end |
#changes ⇒ Hash<String, Array<Object, Object> ] The changes.
Get all the changes for the document.
59 60 61 62 63 64 |
# File 'lib/couchbase-orm/changeable.rb', line 59 def changes changed.each_with_object({}) do |attr, changes| change = attribute_change(attr) changes[attr] = change if change end.with_indifferent_access end |
#changes_applied ⇒ Object
81 82 83 84 |
# File 'lib/couchbase-orm/changeable.rb', line 81 def changes_applied move_changes super end |
#children_changed? ⇒ true | false
This intentionally only considers children and not descendants.
Have any children (embedded documents) of this document changed?
39 40 41 |
# File 'lib/couchbase-orm/changeable.rb', line 39 def children_changed? _children.any?(&:changed?) end |
#move_changes ⇒ Object
Call this method after save, so the changes can be properly switched.
This will unset the memoized children array, set new record flag to false, set the document as validated, and move the dirty changes.
73 74 75 76 77 78 79 |
# File 'lib/couchbase-orm/changeable.rb', line 73 def move_changes @changes_before_last_save = @previous_changes @previous_changes = changes @attributes_before_last_save = @previous_attributes @previous_attributes = attributes.dup changed_attributes.clear end |
#previous_changes ⇒ Hash<String, Array<Object, Object> ] The previous changes.
Get the previous changes on the document.
103 104 105 |
# File 'lib/couchbase-orm/changeable.rb', line 103 def previous_changes @previous_changes ||= {} end |
#reset_object! ⇒ Object Also known as: clear_changes_information
86 87 88 89 90 91 92 |
# File 'lib/couchbase-orm/changeable.rb', line 86 def reset_object! # @attributes = attributes # @attributes_before_type_cast = @attributes.dup @changed_attributes = {} @previous_changes = {} @previous_attributes = {} end |
#saved_change_to_attribute(attr) ⇒ Array<Object> | nil
Returns the change to an attribute during the last save.
151 152 153 |
# File 'lib/couchbase-orm/changeable.rb', line 151 def saved_change_to_attribute(attr) previous_changes[attr] end |
#saved_change_to_attribute?(attr, from: Utils::PLACEHOLDER, to: Utils::PLACEHOLDER) ⇒ true | false
Returns whether this attribute changed during the last save.
This method is useful in after callbacks, to see the change
in an attribute during the save that triggered the callbacks to run.
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/couchbase-orm/changeable.rb', line 165 def saved_change_to_attribute?(attr, from: Utils::PLACEHOLDER, to: Utils::PLACEHOLDER) changes = saved_change_to_attribute(attr) return false unless changes.is_a?(Array) return true if Utils.placeholder?(from) && Utils.placeholder?(to) return changes.first == from if Utils.placeholder?(to) return changes.last == to if Utils.placeholder?(from) changes.first == from && changes.last == to end |
#setters ⇒ Hash
Gets all the new values for each of the changed fields, to be passed to a CouchbaseOrm $set modifier.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/couchbase-orm/changeable.rb', line 116 def setters mods = {} changes.each_pair do |name, changes| next unless changes old, new = changes field = fields[name] key = atomic_attribute_name(name) if field&.resizable? field.add_atomic_changes(self, name, key, mods, new, old) else mods[key] = new unless atomic_unsets.include?(key) end end mods end |
#will_save_change_to_attribute?(attr, **kwargs) ⇒ true | false
Returns whether this attribute change the next time we save.
This method is useful in validations and before callbacks to determine
if the next call to save will change a particular attribute.
188 189 190 |
# File 'lib/couchbase-orm/changeable.rb', line 188 def will_save_change_to_attribute?(attr, **kwargs) attribute_changed?(attr, **kwargs) end |