Class: ActionPolicy::Policy::PreCheck::Check
- Inherits:
-
Object
- Object
- ActionPolicy::Policy::PreCheck::Check
- Defined in:
- lib/action_policy/policy/pre_check.rb
Overview
Single pre-check instance.
Implements filtering logic.
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#policy_class ⇒ Object
readonly
Returns the value of attribute policy_class.
Instance Method Summary collapse
- #applicable?(rule) ⇒ Boolean
- #call(policy) ⇒ Object
-
#dup ⇒ Object
rubocop: enable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop: enable Metrics/PerceivedComplexity, Metrics/MethodLength.
-
#initialize(policy, name, except: nil, only: nil) ⇒ Check
constructor
A new instance of Check.
-
#skip!(except: nil, only: nil) ⇒ Object
rubocop: disable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop: disable Metrics/PerceivedComplexity, Metrics/MethodLength.
Constructor Details
#initialize(policy, name, except: nil, only: nil) ⇒ Check
Returns a new instance of Check.
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/action_policy/policy/pre_check.rb', line 64 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 |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
62 63 64 |
# File 'lib/action_policy/policy/pre_check.rb', line 62 def name @name end |
#policy_class ⇒ Object (readonly)
Returns the value of attribute policy_class.
62 63 64 |
# File 'lib/action_policy/policy/pre_check.rb', line 62 def policy_class @policy_class end |
Instance Method Details
#applicable?(rule) ⇒ Boolean
78 79 80 81 |
# File 'lib/action_policy/policy/pre_check.rb', line 78 def applicable?(rule) return true if filter.nil? filter.call(rule) end |
#call(policy) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/action_policy/policy/pre_check.rb', line 83 def call(policy) Result.new(policy.send(name)).tap do |res| # add denial reason if Reasons included policy.result.reasons.add(policy, name) if res.denied? && policy.result.respond_to?(:reasons) end end |
#dup ⇒ Object
rubocop: enable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop: enable Metrics/PerceivedComplexity, Metrics/MethodLength
120 121 122 |
# File 'lib/action_policy/policy/pre_check.rb', line 120 def dup self.class.new(policy_class, name, except: blacklist&.dup, only: whitelist&.dup) end |
#skip!(except: nil, only: nil) ⇒ Object
rubocop: disable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop: disable Metrics/PerceivedComplexity, Metrics/MethodLength
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/action_policy/policy/pre_check.rb', line 93 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 |