Module: ActionPolicy::Behaviours::Memoized
- Included in:
- Controller
- Defined in:
- lib/action_policy/behaviours/memoized.rb
Overview
Per-instance memoization for policies.
Used by ‘policy_for` to re-use policy object for records.
Example:
include ActionPolicy::Behaviour
include ActionPolicy::Memoized
record = User.first
policy = policy_for(record)
policy2 = policy_for(record)
policy.equal?(policy) #=> true
policy.equal?(policy_for(record, with: CustomPolicy)) #=> false
Defined Under Namespace
Modules: InstanceMethods
Class Method Summary collapse
- .prepended(base) ⇒ Object (also: included)
Instance Method Summary collapse
Class Method Details
.prepended(base) ⇒ Object Also known as: included
26 27 28 |
# File 'lib/action_policy/behaviours/memoized.rb', line 26 def prepended(base) base.prepend InstanceMethods end |
Instance Method Details
#__policies_cache__ ⇒ Object
51 52 53 |
# File 'lib/action_policy/behaviours/memoized.rb', line 51 def __policies_cache__ @__policies_cache__ ||= {} end |
#__policy_memoize__(record, with: nil, namespace: nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/action_policy/behaviours/memoized.rb', line 39 def __policy_memoize__(record, with: nil, namespace: nil) record_key = record._policy_cache_key(use_object_id: true) cache_key = "#{namespace}/#{with}/#{record_key}" return __policies_cache__[cache_key] if __policies_cache__.key?(cache_key) policy = yield __policies_cache__[cache_key] = policy end |