Class: ActionPolicy::LookupChain::NamespaceCache

Inherits:
Object
  • Object
show all
Defined in:
lib/action_policy/lookup_chain.rb

Overview

Cache namespace resolving result for policies.

See Also:

  • benchmarks/namespaced_lookup_cachebenchmarks/namespaced_lookup_cache.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.storeObject (readonly)

Returns the value of attribute store.



25
26
27
# File 'lib/action_policy/lookup_chain.rb', line 25

def store
  @store
end

Class Method Details

.clearObject



43
44
45
46
# File 'lib/action_policy/lookup_chain.rb', line 43

def clear
  hash = Hash.new { |h, k| h[k] = {} }
  @store = {strict: hash, flexible: hash.clone}
end

.fetch(namespace, policy, strict:, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/action_policy/lookup_chain.rb', line 33

def fetch(namespace, policy, strict:, &block)
  return yield unless LookupChain.namespace_cache_enabled?

  if strict
    put_if_absent(:strict, namespace, policy, &block)
  else
    put_if_absent(:flexible, namespace, policy, &block)
  end
end

.put_if_absent(scope, namespace, policy) ⇒ Object



27
28
29
30
31
# File 'lib/action_policy/lookup_chain.rb', line 27

def put_if_absent(scope, namespace, policy)
  local_store = store[scope][namespace]
  return local_store[policy] if local_store[policy]
  local_store[policy] ||= yield
end