blob: 45fac0ed4a7b31444c220b0babbe9b0298d57779 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# frozen_string_literal: true
# rubocop:disable Gitlab/ModuleWithInstanceVariables
module Gitlab
module Patch
module Prependable
class MultiplePrependedBlocks < StandardError
def initialize
super "Cannot define multiple 'prepended' blocks for a Concern"
end
end
module MetaConcern
def extended(base)
super
base.instance_variable_set(:@_prepend_dependencies, [])
end
end
def self.prepended(base)
super
base.singleton_class.prepend MetaConcern
end
def append_features(base)
super
prepend_features(base)
end
def prepend_features(base)
if base.instance_variable_defined?(:@_prepend_dependencies)
base.instance_variable_get(:@_prepend_dependencies) << self
false
else
return false if prepended?(base)
@_prepend_dependencies.each { |dep| base.prepend(dep) }
super
if const_defined?(:ClassMethods)
base.singleton_class.prepend const_get(:ClassMethods)
end
if instance_variable_defined?(:@_prepended_block)
base.class_eval(&@_prepended_block)
end
end
end
def prepended(base = nil, &block)
if base.nil?
raise MultiplePrependedBlocks if
instance_variable_defined?(:@_prepended_block)
@_prepended_block = block
else
super
end
end
def prepended?(base)
index = base.ancestors.index(base)
@_prepend_dependencies.index(self) ||
base.ancestors[0...index].index(self)
end
end
end
end
|