Module: Readthis::Expanders
- Defined in:
- lib/readthis/expanders.rb
Overview
Expander methods are used to transform an object into a string suitable for use as a cache key.
Class Method Summary collapse
-
.expand_key(key) ⇒ String
Expand an object into a suitable cache key.
-
.namespace_key(key, namespace = nil) ⇒ String
Prepend a namespace to a key after expanding it.
Class Method Details
.expand_key(key) ⇒ String
Expand an object into a suitable cache key.
The behavior of ‘expand_key` is largely modeled on the `expand_cache_key` method from `ActiveSupport::Cache`, with some subtle additions.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/readthis/expanders.rb', line 32 def self.(key) case when key.is_a?(String) key.frozen? ? key.dup : key when key.is_a?(Array) key.flat_map { |elem| (elem) }.join('/') when key.is_a?(Hash) key .sort_by { |hkey, _| hkey.to_s } .map { |hkey, val| "#{hkey}=#{val}" } .join('/') when key.respond_to?(:cache_key) key.cache_key when key.respond_to?(:to_param) key.to_param else key.to_s end end |
.namespace_key(key, namespace = nil) ⇒ String
Prepend a namespace to a key after expanding it.
70 71 72 73 74 75 76 77 78 |
# File 'lib/readthis/expanders.rb', line 70 def self.namespace_key(key, namespace = nil) = (key) if namespace "#{namespace}:#{}" else end.force_encoding(Encoding::BINARY) end |