Class: Garner::Strategies::Binding::Key::BindingIndex

Inherits:
Base
  • Object
show all
Defined in:
lib/garner/strategies/binding/key/binding_index.rb

Constant Summary collapse

RANDOM_KEY_LENGTH =

In bytes.

12

Class Method Summary collapse

Class Method Details

.apply(binding) ⇒ String

Compute a cache key as follows:

  1. Determine whether the binding is canonical.

  2. If canonical, fetch the cache key stored for binding.class, binding.id

  3. If not canonical, determine the canonical ID from a proxy binding. Proxy bindings must implement ‘proxy_binding` and `handle`.

Parameters:

  • binding (Object)

    The object from which to compute a key.

Returns:

  • (String)

    A cache key string.



18
19
20
# File 'lib/garner/strategies/binding/key/binding_index.rb', line 18

def self.apply(binding)
  fetch_cache_key_for(binding)
end

.canonical?(binding) ⇒ Boolean

Determine whether the given binding is canonical.

Returns:

  • (Boolean)


82
83
84
85
86
# File 'lib/garner/strategies/binding/key/binding_index.rb', line 82

def self.canonical?(binding)
  # TODO: Implement real logic for determining canonicity.
  binding.is_a?(Mongoid::Document) ||
  (binding.is_a?(Class) && binding.include?(Mongoid::Document))
end

.canonical_binding_for(binding) ⇒ Object

Return canonical binding for the given binding.

Parameters:

  • binding (Object)

    The (possibly) non-canonical binding.



69
70
71
72
73
74
75
76
77
# File 'lib/garner/strategies/binding/key/binding_index.rb', line 69

def self.canonical_binding_for(binding)
  if canonical?(binding)
    binding
  elsif binding.respond_to?(:proxy_binding)
    canonical_binding_for(binding.proxy_binding)
  else
    nil
  end
end

.fetch_cache_key_for(binding) ⇒ String

Fetch cache key, from cache, for the given binding. Generate a random key, if not already extant.

Parameters:

  • binding (Object)

    The object from which to compute a key.

Returns:

  • (String)

    A cache key string.



27
28
29
30
31
# File 'lib/garner/strategies/binding/key/binding_index.rb', line 27

def self.fetch_cache_key_for(binding)
  canonical_binding = fetch_canonical_binding_for(binding)
  key = index_key_for(canonical_binding)
  Garner.config.cache.fetch(key) { new_cache_key_for(canonical_binding) }
end

.fetch_canonical_binding_for(binding) ⇒ Object

Fetch canonical binding for the given binding.

Parameters:

  • binding (Object)

    The object from which to compute a key.

Returns:

  • (Object)

    A class, ID pair.



48
49
50
51
52
# File 'lib/garner/strategies/binding/key/binding_index.rb', line 48

def self.fetch_canonical_binding_for(binding)
  return binding if canonical?(binding)
  key = index_key_for(binding)
  Garner.config.cache.fetch(key) { canonical_binding_for(binding) }
end

.write_cache_key_for(binding) ⇒ String

Overwrite cache key for the given binding.

Parameters:

  • binding (Object)

    The object from which to compute a key.

Returns:

  • (String)

    A cache key string.



37
38
39
40
41
42
# File 'lib/garner/strategies/binding/key/binding_index.rb', line 37

def self.write_cache_key_for(binding)
  canonical_binding = fetch_canonical_binding_for(binding)
  key = index_key_for(canonical_binding)
  value = new_cache_key_for(canonical_binding)
  value.tap { |v| Garner.config.cache.write(key, v) }
end

.write_canonical_binding_for(binding) ⇒ Object

Overwrite canonical binding for the given binding.

Parameters:

  • binding (Object)

    The object from which to compute a key.

Returns:

  • (Object)

    A class, ID pair.



58
59
60
61
62
63
# File 'lib/garner/strategies/binding/key/binding_index.rb', line 58

def self.write_canonical_binding_for(binding)
  return binding if canonical?(binding)
  key = index_key_for(binding)
  value = canonical_binding_for(binding)
  value.tap { |v| Garner.config.cache.write(key, v) }
end