class ActionPolicy::Policy::PreCheck::Check
Single pre-check instance.
Implements filtering logic.
Attributes
name[R]
policy_class[R]
Public Class Methods
included(base)
click to toggle source
# File lib/action_policy/policy/pre_check.rb, line 108 def included(base) base.extend ClassMethods end
new(policy, name, except: nil, only: nil)
click to toggle source
# File lib/action_policy/policy/pre_check.rb, line 36 def initialize(policy, name, except: nil, only: nil) if !except.nil? && !only.nil? raise ArgumentError, "Only one of `except` and `only` may be specified for pre-check" end @policy_class = policy @name = name @blacklist = Array(except) unless except.nil? @whitelist = Array(only) unless only.nil? rebuild_filter end
Public Instance Methods
__apply__(rule)
click to toggle source
Calls superclass method
# File lib/action_policy/policy/pre_check.rb, line 122 def __apply__(rule) run_pre_checks(rule) { super } end
applicable?(rule)
click to toggle source
# File lib/action_policy/policy/pre_check.rb, line 50 def applicable?(rule) return true if filter.nil? filter.call(rule) end
call(policy)
click to toggle source
# File lib/action_policy/policy/pre_check.rb, line 55 def call(policy) = policy.send(name) def skip!(except: nil, only: nil) if !except.nil? && !only.nil? raise ArgumentError, "Only one of `except` and `only` may be specified when skipping pre-check" end if except.nil? && only.nil? raise ArgumentError, "At least one of `except` and `only` must be specified when skipping pre-check" end if except @whitelist = Array(except) @whitelist -= blacklist if blacklist @blacklist = nil else # only @blacklist += Array(only) if blacklist @whitelist -= Array(only) if whitelist @blacklist = Array(only) if filter.nil? end rebuild_filter end # rubocop: enable # rubocop: enable def dup self.class.new( policy_class, name, except: blacklist&.dup, only: whitelist&.dup ) end private attr_reader :whitelist, :blacklist, :filter def rebuild_filter @filter = if whitelist proc { |rule| whitelist.include?(rule) } elsif blacklist proc { |rule| !blacklist.include?(rule) } end end end
dup()
click to toggle source
rubocop: enable rubocop: enable
# File lib/action_policy/policy/pre_check.rb, line 84 def dup self.class.new( policy_class, name, except: blacklist&.dup, only: whitelist&.dup ) end
rebuild_filter()
click to toggle source
# File lib/action_policy/policy/pre_check.rb, line 97 def rebuild_filter @filter = if whitelist proc { |rule| whitelist.include?(rule) } elsif blacklist proc { |rule| !blacklist.include?(rule) } end end
run_pre_checks(rule) { || ... }
click to toggle source
# File lib/action_policy/policy/pre_check.rb, line 113 def run_pre_checks(rule) self.class.pre_checks.each do |check| next unless check.applicable?(rule) check.call(self) end yield if block_given? end
skip!(except: nil, only: nil)
click to toggle source
# File lib/action_policy/policy/pre_check.rb, line 57 def skip!(except: nil, only: nil) if !except.nil? && !only.nil? raise ArgumentError, "Only one of `except` and `only` may be specified when skipping pre-check" end if except.nil? && only.nil? raise ArgumentError, "At least one of `except` and `only` must be specified when skipping pre-check" end if except @whitelist = Array(except) @whitelist -= blacklist if blacklist @blacklist = nil else # only @blacklist += Array(only) if blacklist @whitelist -= Array(only) if whitelist @blacklist = Array(only) if filter.nil? end rebuild_filter end