Class: Gitlab::Utils::Override::Extension

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/utils/override.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject) ⇒ Extension

Returns a new instance of Extension.


18
19
20
# File 'lib/gitlab/utils/override.rb', line 18

def initialize(subject)
  @subject = subject
end

Instance Attribute Details

#subjectObject (readonly)

Returns the value of attribute subject.


16
17
18
# File 'lib/gitlab/utils/override.rb', line 16

def subject
  @subject
end

Class Method Details

.verify_class!(klass, method_name, arity) ⇒ Object


9
10
11
12
13
14
# File 'lib/gitlab/utils/override.rb', line 9

def self.verify_class!(klass, method_name, arity)
  extension = new(klass)
  parents = extension.parents_for(klass)
  extension.verify_method!(
    klass: klass, parents: parents, method_name: method_name, sub_method_arity: arity)
end

Instance Method Details

#add_class(klass) ⇒ Object


59
60
61
# File 'lib/gitlab/utils/override.rb', line 59

def add_class(klass)
  classes << klass
end

#add_method_name(method_name, arity = nil) ⇒ Object


55
56
57
# File 'lib/gitlab/utils/override.rb', line 55

def add_method_name(method_name, arity = nil)
  method_names[method_name] = arity
end

#parents_for(klass) ⇒ Object


22
23
24
25
# File 'lib/gitlab/utils/override.rb', line 22

def parents_for(klass)
  index = klass.ancestors.index(subject)
  klass.ancestors.drop(index + 1)
end

#verify!Object


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gitlab/utils/override.rb', line 27

def verify!
  classes.each do |klass|
    parents = parents_for(klass)

    method_names.each_pair do |method_name, arity|
      verify_method!(
        klass: klass,
        parents: parents,
        method_name: method_name,
        sub_method_arity: arity)
    end
  end
end

#verify_method!(klass:, parents:, method_name:, sub_method_arity:) ⇒ Object

Raises:

  • (NotImplementedError)

41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/gitlab/utils/override.rb', line 41

def verify_method!(klass:, parents:, method_name:, sub_method_arity:)
  overridden_parent = parents.find do |parent|
    instance_method_defined?(parent, method_name)
  end

  raise NotImplementedError, "#{klass}\##{method_name} doesn't exist!" unless overridden_parent

  super_method_arity = find_direct_method(overridden_parent, method_name).arity

  unless arity_compatible?(sub_method_arity, super_method_arity)
    raise NotImplementedError, "#{subject}\##{method_name} has arity of #{sub_method_arity}, but #{overridden_parent}\##{method_name} has arity of #{super_method_arity}"
  end
end

#verify_override?(method_name) ⇒ Boolean

Returns:

  • (Boolean)

63
64
65
# File 'lib/gitlab/utils/override.rb', line 63

def verify_override?(method_name)
  method_names.has_key?(method_name)
end