Module: Kasket::WriteMixin::InstanceMethods
- Defined in:
- lib/kasket/write_mixin.rb
Instance Method Summary collapse
- #clear_kasket_indices(options = {}) ⇒ Object
- #committed!(*args, **kwargs) ⇒ Object
- #default_kasket_cacheable? ⇒ Boolean
- #kasket_after_commit ⇒ Object
- #kasket_after_commit_dummy ⇒ Object
- #kasket_after_destroy ⇒ Object
- #kasket_after_save ⇒ Object
- #kasket_key ⇒ Object
- #kasket_keys(options = {}) ⇒ Object
- #reload ⇒ Object
- #rolledback!(*args, **kwargs) ⇒ Object
- #store_in_kasket(key = kasket_key) ⇒ Object
- #update_columns(new_attributes) ⇒ Object
Instance Method Details
#clear_kasket_indices(options = {}) ⇒ Object
81 82 83 84 85 |
# File 'lib/kasket/write_mixin.rb', line 81 def clear_kasket_indices( = {}) kasket_keys().each do |key| Kasket.cache.delete(key) end end |
#committed!(*args, **kwargs) ⇒ Object
114 115 116 117 118 |
# File 'lib/kasket/write_mixin.rb', line 114 def committed!(*args, **kwargs) Kasket.clear_pending_records kasket_after_commit if persisted? || destroyed? super end |
#default_kasket_cacheable? ⇒ Boolean
28 29 30 |
# File 'lib/kasket/write_mixin.rb', line 28 def default_kasket_cacheable? true end |
#kasket_after_commit ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/kasket/write_mixin.rb', line 68 def kasket_after_commit keys = kasket_keys if persisted? && Kasket::CONFIGURATION[:write_through] key = store_in_kasket keys.delete(key) end keys.each do |key| Kasket.cache.delete(key) end end |
#kasket_after_commit_dummy ⇒ Object
102 103 104 |
# File 'lib/kasket/write_mixin.rb', line 102 def kasket_after_commit_dummy # This is here to force committed! to be invoked. end |
#kasket_after_destroy ⇒ Object
110 111 112 |
# File 'lib/kasket/write_mixin.rb', line 110 def kasket_after_destroy Kasket.add_pending_record(self, _destroyed = true) end |
#kasket_after_save ⇒ Object
106 107 108 |
# File 'lib/kasket/write_mixin.rb', line 106 def kasket_after_save Kasket.add_pending_record(self) end |
#kasket_key ⇒ Object
24 25 26 |
# File 'lib/kasket/write_mixin.rb', line 24 def kasket_key @kasket_key ||= new_record? ? nil : self.class.kasket_key_for_id(id) end |
#kasket_keys(options = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/kasket/write_mixin.rb', line 40 def kasket_keys( = {}) attribute_sets = [attributes] previous_changes = [:previous_changes] previous_changes ||= saved_changes.transform_values(&:first) if previous_changes.present? old_attributes = previous_changes.each_with_object({}) do |(attribute, (old, _)), all| all[attribute.to_s] = old end attribute_sets << old_attributes.reverse_merge(attribute_sets[0]) attribute_sets << attribute_sets[0].merge(old_attributes) end keys = [] self.class.kasket_indices.each do |index| keys.concat( attribute_sets.map do |attribute_set| key = self.class.kasket_key_for(index.map { |attribute| [attribute, attribute_set[attribute.to_s]] }) index.include?(:id) ? key : [key, key + '/first'] end ) end keys.flatten! keys.uniq! keys end |
#reload ⇒ Object
87 88 89 90 |
# File 'lib/kasket/write_mixin.rb', line 87 def reload(*) clear_kasket_indices super end |
#rolledback!(*args, **kwargs) ⇒ Object
120 121 122 123 |
# File 'lib/kasket/write_mixin.rb', line 120 def rolledback!(*args, **kwargs) Kasket.clear_pending_records super end |
#store_in_kasket(key = kasket_key) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/kasket/write_mixin.rb', line 32 def store_in_kasket(key = kasket_key) if kasket_cacheable? && key = { expires_in: self.class.kasket_ttl } if self.class.kasket_ttl Kasket.cache.write(key, attributes_before_type_cast.dup, ) key end end |
#update_columns(new_attributes) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/kasket/write_mixin.rb', line 92 def update_columns(new_attributes) previous_attributes = attributes previous_changes = new_attributes.each_with_object({}) do |(k, v), all| all[k] = [previous_attributes[k.to_s], v] end result = super clear_kasket_indices(previous_changes: previous_changes) result end |