Module: Gitlab::Utils::DelegatorOverride

Included in:
View::Presenter::Delegated
Defined in:
lib/gitlab/utils/delegator_override.rb,
lib/gitlab/utils/delegator_override/error.rb,
lib/gitlab/utils/delegator_override/validator.rb

Overview

This module is to validate that delegator classes (`SimpleDelegator`) do not accidentally override important logic on the fabricated object.

Defined Under Namespace

Classes: Error, Validator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.validator(delegator_class) ⇒ Object


32
33
34
# File 'lib/gitlab/utils/delegator_override.rb', line 32

def self.validator(delegator_class)
  validators[delegator_class] ||= Validator.new(delegator_class)
end

.validatorsObject


36
37
38
# File 'lib/gitlab/utils/delegator_override.rb', line 36

def self.validators
  @validators ||= {}
end

.verify!Object


40
41
42
43
44
45
# File 'lib/gitlab/utils/delegator_override.rb', line 40

def self.verify!
  validators.each_value do |validator|
    validator.expand_on_ancestors(validators)
    validator.validate_overrides!
  end
end

Instance Method Details

#delegator_override(*names) ⇒ Object

Raises:

  • (TypeError)

18
19
20
21
22
23
# File 'lib/gitlab/utils/delegator_override.rb', line 18

def delegator_override(*names)
  return unless ENV['STATIC_VERIFICATION']
  raise TypeError unless names.all? { |n| n.is_a?(Symbol) }

  DelegatorOverride.validator(self).add_allowlist(names)
end

#delegator_override_with(mod) ⇒ Object

Raises:

  • (TypeError)

25
26
27
28
29
30
# File 'lib/gitlab/utils/delegator_override.rb', line 25

def delegator_override_with(mod)
  return unless ENV['STATIC_VERIFICATION']
  raise TypeError unless mod.is_a?(Module)

  DelegatorOverride.validator(self).add_allowlist(mod.instance_methods)
end

#delegator_target(target_class) ⇒ Object


8
9
10
11
12
13
14
15
16
# File 'lib/gitlab/utils/delegator_override.rb', line 8

def delegator_target(target_class)
  return unless ENV['STATIC_VERIFICATION']

  unless self < ::SimpleDelegator
    raise ArgumentError, "'#{self}' is not a subclass of 'SimpleDelegator' class."
  end

  DelegatorOverride.validator(self).add_target(target_class)
end