module DatabaseCachedAttribute
DatabaseCachedAttribute
adds a method to add a set of easy to use cache invalidation methods/callbacks for specified attributes
On the model where you possibly may want to cache things from this model that may be heavy to create:
include DatabaseCachedAttribute database_cached_attribute :attribute_name, :another_attribute_name
This will create the methods
invalidate_attribute_name(optional_object) invalidate_another_attribute_name(optional_object) cache_attribute_name(optional_object) cache_another_attribute_name(optional_object)
Which means invalidating that attribute is easy, say when you add an associated object
has_many :things, before_add: :invalidate_attribute, before_remove: :invalidate_other_attribute
Constants
- VERSION
Public Instance Methods
invalidate_cache(column_name)
click to toggle source
Cache invalidation by column
# File lib/database_cached_attribute.rb, line 23 def invalidate_cache (column_name) self[column_name.to_sym] = nil update_cache column_name.to_sym end
only_change?(column_name)
click to toggle source
Determines if the provided column name is the only change
# File lib/database_cached_attribute.rb, line 34 def only_change? (column_name) changes.length == 1 && changes.has_key?(column_name) end
only_changes?(*column_names)
click to toggle source
Determines if the provided column names are the only changes
# File lib/database_cached_attribute.rb, line 39 def only_changes? (*column_names) return false if changes.length != column_names.length column_names.each do |column_name| return false if !changes.has_key?(column_name) end true end
only_changes_in?(*column_names)
click to toggle source
Determines if the changes, if any, are only in the list of column names provided
# File lib/database_cached_attribute.rb, line 50 def only_changes_in? (*column_names) return false if changes.length > column_names.length our_changes = changes.slice column_names return false if our_changes.length < changes.length true end
update_cache(column_name)
click to toggle source
Update cache by column
# File lib/database_cached_attribute.rb, line 29 def update_cache (column_name) save if only_change?(column_name) && persisted? end