Module: ActionPolicy::Policy::PreCheck::ClassMethods

Defined in:
lib/action_policy/policy/pre_check.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#pre_check(*names, **options) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/action_policy/policy/pre_check.rb', line 125

def pre_check(*names, **options)
  names.each do |name|
    # do not allow pre-check override
    check = pre_checks.find { it.name == name }
    raise "Pre-check already defined: #{name}" unless check.nil?

    pre_checks << Check.new(self, name, **options)
  end
end

#pre_checksObject



148
149
150
151
152
153
154
155
156
# File 'lib/action_policy/policy/pre_check.rb', line 148

def pre_checks
  return @pre_checks if instance_variable_defined?(:@pre_checks)

  @pre_checks = if superclass.respond_to?(:pre_checks)
    superclass.pre_checks.dup
  else
    []
  end
end

#skip_pre_check(*names, **options) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/action_policy/policy/pre_check.rb', line 135

def skip_pre_check(*names, **options)
  names.each do |name|
    check = pre_checks.find { it.name == name }
    raise "Pre-check not found: #{name}" if check.nil?

    # when no options provided we remove this check completely
    next pre_checks.delete(check) if options.empty?

    # otherwise duplicate and apply skip options
    pre_checks[pre_checks.index(check)] = check.dup.tap { it.skip!(**options) }
  end
end