Module: ActionPolicy::PerThreadCache

Defined in:
lib/action_policy/behaviours/thread_memoized.rb

Overview

:nodoc:

Constant Summary collapse

CACHE_KEY =
"action_policy.per_thread_cache"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.enabled=(value) ⇒ Object (writeonly)

Sets the attribute enabled

Parameters:

  • value

    the value to set the attribute enabled to.



8
9
10
# File 'lib/action_policy/behaviours/thread_memoized.rb', line 8

def enabled=(value)
  @enabled = value
end

Class Method Details

.clear_allObject



22
23
24
# File 'lib/action_policy/behaviours/thread_memoized.rb', line 22

def clear_all
  Thread.current[CACHE_KEY] = {}
end

.enabled?Boolean

Returns:

  • (Boolean)


10
# File 'lib/action_policy/behaviours/thread_memoized.rb', line 10

def enabled?() = @enabled == true

.fetch(key) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/action_policy/behaviours/thread_memoized.rb', line 12

def fetch(key)
  return yield unless enabled?

  store = (Thread.current[CACHE_KEY] ||= {})

  return store[key] if store.key?(key)

  store[key] = yield
end