Module: DeclarativePolicy
- Extended by:
- PreferredScope
- Defined in:
- lib/declarative_policy.rb,
lib/declarative_policy/base.rb,
lib/declarative_policy/rule.rb,
lib/declarative_policy/step.rb,
lib/declarative_policy/cache.rb,
lib/declarative_policy/runner.rb,
lib/declarative_policy/version.rb,
lib/declarative_policy/rule_dsl.rb,
lib/declarative_policy/condition.rb,
lib/declarative_policy/nil_policy.rb,
lib/declarative_policy/policy_dsl.rb,
lib/declarative_policy/delegate_dsl.rb,
lib/declarative_policy/configuration.rb,
lib/declarative_policy/preferred_scope.rb
Overview
Default policy definition for nil values
Defined Under Namespace
Modules: Cache, PreferredScope, Rule Classes: Base, Condition, Configuration, DelegateDsl, ManifestCondition, NilPolicy, PolicyDsl, RuleDsl, Runner, Step
Constant Summary collapse
- VERSION =
'1.1.0'
Constants included from PreferredScope
PreferredScope::PREFERRED_SCOPE_KEY
Class Method Summary collapse
- .class_for(subject) ⇒ Object
- .configure(&block) ⇒ Object
-
.configure!(&block) ⇒ Object
Reset configuration.
-
.invalidate(cache, invalidated_keys) ⇒ Object
Find the list of runners with now invalidated keys, and invalidate the runners.
- .policy?(subject) ⇒ Boolean (also: has_policy?)
- .policy_for(user, subject, opts = {}) ⇒ Object
Methods included from PreferredScope
preferred_scope, preferred_scope=, subject_scope, user_scope, with_preferred_scope
Class Method Details
.class_for(subject) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/declarative_policy.rb', line 49 def class_for(subject) return configuration.nil_policy if subject.nil? return configuration.named_policy(subject) if subject.is_a?(Symbol) subject = find_delegate(subject) policy_class = class_for_class(subject.class) raise "no policy for #{subject.class.name}" if policy_class.nil? policy_class end |
.configure(&block) ⇒ Object
61 62 63 64 65 |
# File 'lib/declarative_policy.rb', line 61 def configure(&block) configuration.instance_eval(&block) nil end |
.configure!(&block) ⇒ Object
Reset configuration
68 69 70 71 |
# File 'lib/declarative_policy.rb', line 68 def configure!(&block) @configuration = DeclarativePolicy::Configuration.new configure(&block) if block end |
.invalidate(cache, invalidated_keys) ⇒ Object
Find the list of runners with now invalidated keys, and invalidate the runners
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/declarative_policy.rb', line 30 def invalidate(cache, invalidated_keys) return unless cache&.any? return unless invalidated_keys&.any? keys = invalidated_keys.to_set policies = cache.select { |k, _| k.is_a?(String) && k.start_with?('/dp/policy/') } policies.each_value do |policy| policy.runners.each do |runner| runner.uncache! if keys.intersect?(runner.dependencies) end end invalidated_keys.each { |k| cache.delete(k) } nil end |
.policy?(subject) ⇒ Boolean Also known as: has_policy?
73 74 75 |
# File 'lib/declarative_policy.rb', line 73 def policy?(subject) !class_for_class(subject.class).nil? end |
.policy_for(user, subject, opts = {}) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/declarative_policy.rb', line 22 def policy_for(user, subject, opts = {}) cache = opts[:cache] || {} key = Cache.policy_key(user, subject) cache[key] ||= class_for(subject).new(user, subject, opts) end |