Module: CommonDataCaching::ClassMethods
- Defined in:
- lib/common_data_caching/class_methods.rb
Instance Method Summary collapse
- #common_data_cache_collection ⇒ Object
- #common_data_cache_keys ⇒ Object
- #common_data_caching? ⇒ Boolean
- #prepare_common_data_collection(attrs) ⇒ Object
- #update_common_data_cache ⇒ Object
- #update_common_data_cache_versions ⇒ Object
Instance Method Details
#common_data_cache_collection ⇒ Object
16 17 18 |
# File 'lib/common_data_caching/class_methods.rb', line 16 def common_data_cache_collection Rails.cache.fetch("common-data-caching_#{to_s}") || [] end |
#common_data_cache_keys ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/common_data_caching/class_methods.rb', line 8 def common_data_cache_keys if cache_opts[:collections] cache_opts[:collections].map { |i| "common-data-caching_#{i[:key]}" } else ["common-data-caching_#{to_s}"] end end |
#common_data_caching? ⇒ Boolean
4 5 6 |
# File 'lib/common_data_caching/class_methods.rb', line 4 def common_data_caching? true end |
#prepare_common_data_collection(attrs) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/common_data_caching/class_methods.rb', line 37 def prepare_common_data_collection(attrs) # колонка ID - по умолчанию hash = { id: :id } attrs.flatten.map do |key| key.is_a?(Hash) ? hash.merge!(key) : hash.merge!(key => key) end keys = hash.keys.unshift(:id) all.map do |object| keys.to_h { |title| [title, object.send(hash[title])] } end end |
#update_common_data_cache ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/common_data_caching/class_methods.rb', line 20 def update_common_data_cache if cache_opts[:collections] handle_common_data_cache_collections else handle_common_data_collection(cache_opts) end end |
#update_common_data_cache_versions ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/common_data_caching/class_methods.rb', line 28 def update_common_data_cache_versions versions = Rails.cache.read('common-data-caching-versions') || {} common_data_cache_keys.each do |key| versions[key] = Time.now.to_i Rails.cache.write('common-data-caching-versions', versions) end end |