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)


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

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.



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

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
32
# 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)
  return nil unless canonical_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.



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

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

.index_key_for(binding) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/garner/strategies/binding/key/binding_index.rb', line 90

def self.index_key_for(binding)
  if binding.respond_to?(:identity_string)
    binding_key = binding.identity_string
  else
    binding_key = binding.to_s
  end

  {
    strategy: self,
    proxied_binding: binding_key
  }
end

.new_cache_key_for(_binding) ⇒ Object



103
104
105
# File 'lib/garner/strategies/binding/key/binding_index.rb', line 103

def self.new_cache_key_for(_binding)
  SecureRandom.hex(RANDOM_KEY_LENGTH)
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.



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

def self.write_cache_key_for(binding)
  canonical_binding = fetch_canonical_binding_for(binding)
  return nil unless canonical_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.



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

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