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

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.

Examples:

String expansion


Readthis::Expanders.expand_key('typical-key')
'typical-key'

Array string expansion


Readthis::Expanders.expand_key(['a', 'b', [:c, 'd'], 1])
'a/b/c/d/1'

Hash expansion


Readthis::Expanders.expand_key(c: 1, 'a' => 3, b: 2)
'a=3/b=2/c=1'

Parameters:

  • key (Object)

    An object to stringify. Arrays, hashes, and objects that respond to either ‘cache_key` or `to_param` have direct support. All other objects will be coerced to a string, and frozen strings will be duplicated.

Returns:

  • (String)

    A cache key string.



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.expand_key(key)
  case
  when key.is_a?(String)
    key.frozen? ? key.dup : key
  when key.is_a?(Array)
    key.flat_map { |elem| expand_key(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.

Examples:

Applying a namespace


Knuckles::Expanders.namespace_key('alpha', 'greek')
'greek:alpha'

Omitting a namespace


Knuckles::Expanders.namespace_key('alpha', nil)
'alpha'

Parameters:

  • key (Object)

    An object to stringify.

  • namespace (String) (defaults to: nil)

    An optional namespace to prepend, if ‘nil` it is ignored.

Returns:

  • (String)

    A binary encoded string combining the namespace and key.



70
71
72
73
74
75
76
77
78
# File 'lib/readthis/expanders.rb', line 70

def self.namespace_key(key, namespace = nil)
  expanded = expand_key(key)

  if namespace
    "#{namespace}:#{expanded}"
  else
    expanded
  end.force_encoding(Encoding::BINARY)
end