From 57f0e3d1300d01444d2a311560c055d26968dc3f Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Mon, 6 Nov 2017 11:08:12 +0200 Subject: Remove code duplication in ActiveSupport::Cache --- activesupport/lib/active_support/cache.rb | 37 +++++++++---------------------- 1 file changed, 11 insertions(+), 26 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index 976104e0c4..9f91f9b576 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -90,11 +90,16 @@ module ActiveSupport private def retrieve_cache_key(key) case - when key.respond_to?(:cache_key_with_version) then key.cache_key_with_version - when key.respond_to?(:cache_key) then key.cache_key - when key.is_a?(Array) then key.map { |element| retrieve_cache_key(element) }.to_param - when key.respond_to?(:to_a) then retrieve_cache_key(key.to_a) - else key.to_param + when key.respond_to?(:cache_key_with_version) + key.cache_key_with_version + when key.respond_to?(:cache_key) + key.cache_key + when key.is_a?(Hash) + key.sort_by { |k, _| k.to_s }.collect { |k, v| "#{k}=#{v}" }.to_param + when key.respond_to?(:to_a) + key.to_a.collect { |element| retrieve_cache_key(element) }.to_param + else + key.to_param end.to_s end @@ -565,33 +570,13 @@ module ActiveSupport # Prefixes a key with the namespace. Namespace and key will be delimited # with a colon. def normalize_key(key, options) - key = expanded_key(key) + key = Cache.expand_cache_key(key) namespace = options[:namespace] if options prefix = namespace.is_a?(Proc) ? namespace.call : namespace key = "#{prefix}:#{key}" if prefix key end - # Expands key to be a consistent string value. Invokes +cache_key+ if - # object responds to +cache_key+. Otherwise, +to_param+ method will be - # called. If the key is a Hash, then keys will be sorted alphabetically. - def expanded_key(key) - return key.cache_key.to_s if key.respond_to?(:cache_key) - - case key - when Array - if key.size > 1 - key = key.collect { |element| expanded_key(element) } - else - key = key.first - end - when Hash - key = key.sort_by { |k, _| k.to_s }.collect { |k, v| "#{k}=#{v}" } - end - - key.to_param - end - def normalize_version(key, options = nil) (options && options[:version].try(:to_param)) || expanded_version(key) end -- cgit v1.2.3