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



172
173
174
175
176
177
178
179
180
# File 'lib/action_policy/policy/pre_check.rb', line 172

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

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

#pre_checksObject

rubocop: enable Metrics/AbcSize



197
198
199
200
201
202
203
204
205
206
# File 'lib/action_policy/policy/pre_check.rb', line 197

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

rubocop: disable Metrics/AbcSize



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/action_policy/policy/pre_check.rb', line 183

def skip_pre_check(*names, **options)
  names.each do |name|
    check = pre_checks.find { |c| c.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 { |c| c.skip! options }
  end
end